Skip to content

Commit

Permalink
Added missing checks and specs
Browse files Browse the repository at this point in the history
  • Loading branch information
pablobm committed Apr 17, 2022
1 parent d15b3bf commit 8756286
Show file tree
Hide file tree
Showing 8 changed files with 235 additions and 128 deletions.
3 changes: 2 additions & 1 deletion app/views/fields/has_one/_index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ By default, the relationship is rendered as a link to the associated object.
%>

<% if field.linkable? %>
<%= link_to(
<%= link_to_if(
administrate_valid_action?(field.data, :show),
field.display_associated_resource,
[namespace, field.data],
) %>
Expand Down
3 changes: 2 additions & 1 deletion app/views/fields/has_one/_show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ All show page attributes of has_one relationship would be rendered
<% if field.linkable? %>
<fieldset class="attribute--nested">
<legend>
<%= link_to(
<%= link_to_if(
administrate_valid_action?(field.data, :show),
field.display_associated_resource,
[namespace, field.data],
) %>
Expand Down
12 changes: 8 additions & 4 deletions app/views/fields/polymorphic/_index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@ By default, the relationship is rendered as a link to the associated object.
%>

<% if field.data %>
<%= link_to(
field.display_associated_resource,
[namespace, field.data]
) %>
<% if administrate_valid_action?(field.data, :show) %>
<%= link_to(
field.display_associated_resource,
[namespace, field.data]
) %>
<% else %>
<%= field.display_associated_resource %>
<% end %>
<% end %>
15 changes: 15 additions & 0 deletions spec/administrate/views/fields/belongs_to/_index_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,21 @@
)
end

context "without an associated record" do
let(:belongs_to) do
instance_double(
"Administrate::Field::BelongsTo",
associated_class: associated_class,
data: nil,
)
end

it "displays nothing" do
render_belongs_to_index
expect(rendered.strip).to eq("")
end
end

context "if associated resource has a show route" do
context "and the user has permission to access it" do
it "displays link" do
Expand Down
74 changes: 48 additions & 26 deletions spec/administrate/views/fields/has_one/_index_spec.rb
Original file line number Diff line number Diff line change
@@ -1,41 +1,63 @@
require "rails_helper"

describe "fields/has_one/_index", type: :view do
context "without an associated record" do
it "displays nothing" do
has_one = Administrate::Field::HasOne.new(
:product_meta_tag,
build(:product_meta_tag),
:index,
)
let(:product) { create(:product) }
let(:product_path) { polymorphic_path([:admin, product]) }
let(:link) { "<a href=\"#{product_path}\">#{product.name}</a>" }
let(:has_one) do
instance_double(
"Administrate::Field::HasOne",
data: product,
linkable?: true,
display_associated_resource: product.name,
)
end

render(
partial: "fields/has_one/index",
locals: { field: has_one },
context "without an associated record" do
let(:has_one) do
instance_double(
"Administrate::Field::HasOne",
linkable?: false,
)
end

it "displays nothing" do
allow(view).to receive(:administrate_valid_action?).and_return(true)
render_has_one_index
expect(rendered.strip).to eq("")
end
end

context "with an associated record" do
it "renders a link to the record" do
product = create(:product)
product_path = polymorphic_path([:admin, product])
has_one = instance_double(
"Administrate::Field::HasOne",
data: product,
linkable?: true,
display_associated_resource: product.name,
)
context "if associated resource has a show route" do
context "and the user has permission to access it" do
it "displays link" do
allow(view).to receive(:administrate_valid_action?).and_return(true)
render_has_one_index
expect(rendered.strip).to include(link)
end
end

render(
partial: "fields/has_one/index",
locals: { field: has_one, namespace: :admin },
)
context "and the user does not have permission to access it" do
it "hides link" do
allow(view).to receive(:administrate_valid_action?).and_return(false)
render_has_one_index
expect(rendered.strip).to_not include(link)
end
end
end

expected = "<a href=\"#{product_path}\">#{product.name}</a>"
expect(rendered.strip).to eq(expected)
context "if associated resource has no show route" do
it "hides link" do
allow(view).to receive(:administrate_valid_action?).and_return(false)
render_has_one_index
expect(rendered.strip).to_not include(link)
end
end

def render_has_one_index
render(
partial: "fields/has_one/index",
locals: { field: has_one, namespace: :admin },
)
end
end
186 changes: 113 additions & 73 deletions spec/administrate/views/fields/has_one/_show_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
require "administrate/field/has_one"

describe "fields/has_one/_show", type: :view do
before do
allow(view).to receive(:administrate_valid_action?).and_return(true)
end

context "without an associated record" do
it "displays nothing" do
has_one = Administrate::Field::HasOne.new(
Expand All @@ -20,87 +24,123 @@
end

context "with an associated record" do
it "renders a link to the record" do
product = create(:product)
product_path = polymorphic_path([:admin, product])
nested_show = instance_double(
"Administrate::Page::Show",
resource: double(
class: ProductMetaTag,
),
attributes: [],
resource_name: "Product Tag",
)
has_one = instance_double(
"Administrate::Field::HasOne",
display_associated_resource: product.name,
data: product,
linkable?: true,
nested_show: nested_show,
)
context "when linking the record is allowed" do
it "renders a link to the record" do
product = create(:product)
product_path = polymorphic_path([:admin, product])
nested_show = instance_double(
"Administrate::Page::Show",
resource: double(
class: ProductMetaTag,
),
attributes: [],
resource_name: "Product Tag",
)
has_one = instance_double(
"Administrate::Field::HasOne",
display_associated_resource: product.name,
data: product,
linkable?: true,
nested_show: nested_show,
)

render(
partial: "fields/has_one/show",
locals: {
field: has_one,
namespace: :admin,
resource_name: "product_meta_tag",
},
)
render(
partial: "fields/has_one/show",
locals: {
field: has_one,
namespace: :admin,
resource_name: "product_meta_tag",
},
)

link = "<a href=\"#{product_path}\">#{product.name}</a>"
expect(rendered.strip).to include(link)
end
link = "<a href=\"#{product_path}\">#{product.name}</a>"
expect(rendered.strip).to include(link)
end

it "renders nested attribute relationships" do
view.extend Administrate::ApplicationHelper

product = create(:product)
page = create(:page, product: product)

nested_has_many = instance_double(
"Administrate::Field::HasMany",
associated_collection: [page],
attribute: :page,
data: [page],
resources: [page],
html_class: "has-many",
name: "Page",
to_partial_path: "fields/has_many/index",
order_from_params: {},
)
it "renders nested attribute relationships" do
view.extend Administrate::ApplicationHelper

nested_show = instance_double(
"Administrate::Page::Show",
resource: double(
class: ProductMetaTag,
),
attributes: [nested_has_many],
resource_name: "Product Tag",
)
product = create(:product)
page = create(:page, product: product)

has_one = instance_double(
"Administrate::Field::HasOne",
display_associated_resource: product.name,
data: product,
linkable?: true,
nested_show: nested_show,
)
nested_has_many = instance_double(
"Administrate::Field::HasMany",
associated_collection: [page],
attribute: :page,
data: [page],
resources: [page],
html_class: "has-many",
name: "Page",
to_partial_path: "fields/has_many/index",
order_from_params: {},
)

page_double = instance_double("Administrate::Page::Show")
nested_show = instance_double(
"Administrate::Page::Show",
resource: double(
class: ProductMetaTag,
),
attributes: [nested_has_many],
resource_name: "Product Tag",
)

render(
partial: "fields/has_one/show",
locals: {
field: has_one,
namespace: :admin,
page: page_double,
resource_name: "product_meta_tag",
},
)
has_one = instance_double(
"Administrate::Field::HasOne",
display_associated_resource: product.name,
data: product,
linkable?: true,
nested_show: nested_show,
)

page_double = instance_double("Administrate::Page::Show")

render(
partial: "fields/has_one/show",
locals: {
field: has_one,
namespace: :admin,
page: page_double,
resource_name: "product_meta_tag",
},
)

has_many_count = "1 page"
expect(rendered.strip).to include(has_many_count)
end
end

context "when linking the record is not allowed" do
it "displays the record without a link" do
allow(view).to receive(:administrate_valid_action?).and_return(false)
product = create(:product)
nested_show = instance_double(
"Administrate::Page::Show",
resource: double(
class: ProductMetaTag,
),
attributes: [],
resource_name: "Product Tag",
)
has_one = instance_double(
"Administrate::Field::HasOne",
display_associated_resource: product.name,
data: product,
linkable?: true,
nested_show: nested_show,
)

render(
partial: "fields/has_one/show",
locals: {
field: has_one,
namespace: :admin,
resource_name: "product_meta_tag",
},
)

has_many_count = "1 page"
expect(rendered.strip).to include(has_many_count)
expect(rendered.strip).to include(product.name)
expect(rendered.strip).not_to include("<a ")
end
end
end
end
Loading

0 comments on commit 8756286

Please sign in to comment.