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 9bda868
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 52 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
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
67 changes: 46 additions & 21 deletions spec/administrate/views/fields/polymorphic/_index_spec.rb
Original file line number Diff line number Diff line change
@@ -1,36 +1,61 @@
require "rails_helper"

describe "fields/polymorphic/_index", type: :view do
context "without an associated records" do
it "displays nothing" do
polymorphic = double(data: nil)
let(:product) { create(:product) }
let(:product_path) { polymorphic_path([:admin, product]) }
let(:link) { "<a href=\"#{product_path}\">#{product.name}</a>" }
let(:polymorphic) do
instance_double(
"Administrate::Field::Polymorphic",
data: product,
display_associated_resource: product.name,
)
end

render(
partial: "fields/polymorphic/index",
locals: { field: polymorphic },
context "without an associated record" do
let(:polymorphic) do
instance_double(
"Administrate::Field::Polymorphic",
data: nil,
)
end

it "displays nothing" do
render_polymorphic_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])
polymorphic = instance_double(
"Administrate::Field::Polymorphic",
data: product,
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_polymorphic_index
expect(rendered.strip).to include(link)
end
end

render(
partial: "fields/polymorphic/index",
locals: { field: polymorphic, 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_polymorphic_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_polymorphic_index
expect(rendered.strip).to_not include(link)
end
end

def render_polymorphic_index
render(
partial: "fields/polymorphic/index",
locals: { field: polymorphic, namespace: :admin },
)
end
end

0 comments on commit 9bda868

Please sign in to comment.