From 875628695055dd7796bda68691dae5c7c1fc5ab4 Mon Sep 17 00:00:00 2001 From: Pablo Brasero Date: Thu, 14 Apr 2022 21:10:18 +0100 Subject: [PATCH] Added missing checks and specs --- app/views/fields/has_one/_index.html.erb | 3 +- app/views/fields/has_one/_show.html.erb | 3 +- app/views/fields/polymorphic/_index.html.erb | 12 +- .../views/fields/belongs_to/_index_spec.rb | 15 ++ .../views/fields/has_one/_index_spec.rb | 74 ++++--- .../views/fields/has_one/_show_spec.rb | 186 +++++++++++------- .../views/fields/polymorphic/_index_spec.rb | 67 +++++-- .../admin/customers/_index_header.html.erb | 3 +- 8 files changed, 235 insertions(+), 128 deletions(-) diff --git a/app/views/fields/has_one/_index.html.erb b/app/views/fields/has_one/_index.html.erb index e6fc01a44f..e8f32fa95c 100644 --- a/app/views/fields/has_one/_index.html.erb +++ b/app/views/fields/has_one/_index.html.erb @@ -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], ) %> diff --git a/app/views/fields/has_one/_show.html.erb b/app/views/fields/has_one/_show.html.erb index a4b3dc5fe8..9e0a09989f 100644 --- a/app/views/fields/has_one/_show.html.erb +++ b/app/views/fields/has_one/_show.html.erb @@ -18,7 +18,8 @@ All show page attributes of has_one relationship would be rendered <% if field.linkable? %>
- <%= link_to( + <%= link_to_if( + administrate_valid_action?(field.data, :show), field.display_associated_resource, [namespace, field.data], ) %> diff --git a/app/views/fields/polymorphic/_index.html.erb b/app/views/fields/polymorphic/_index.html.erb index 7c27a3b379..35ec725312 100644 --- a/app/views/fields/polymorphic/_index.html.erb +++ b/app/views/fields/polymorphic/_index.html.erb @@ -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 %> diff --git a/spec/administrate/views/fields/belongs_to/_index_spec.rb b/spec/administrate/views/fields/belongs_to/_index_spec.rb index 587bf13c30..ccce07d260 100644 --- a/spec/administrate/views/fields/belongs_to/_index_spec.rb +++ b/spec/administrate/views/fields/belongs_to/_index_spec.rb @@ -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 diff --git a/spec/administrate/views/fields/has_one/_index_spec.rb b/spec/administrate/views/fields/has_one/_index_spec.rb index 56a234ee9e..4fee947257 100644 --- a/spec/administrate/views/fields/has_one/_index_spec.rb +++ b/spec/administrate/views/fields/has_one/_index_spec.rb @@ -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) { "#{product.name}" } + 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 = "#{product.name}" - 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 diff --git a/spec/administrate/views/fields/has_one/_show_spec.rb b/spec/administrate/views/fields/has_one/_show_spec.rb index 61cdcad0b2..a7fd2b3d02 100644 --- a/spec/administrate/views/fields/has_one/_show_spec.rb +++ b/spec/administrate/views/fields/has_one/_show_spec.rb @@ -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( @@ -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 = "#{product.name}" - expect(rendered.strip).to include(link) - end + link = "#{product.name}" + 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("#{product.name}" } + 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 = "#{product.name}" - 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 diff --git a/spec/example_app/app/views/admin/customers/_index_header.html.erb b/spec/example_app/app/views/admin/customers/_index_header.html.erb index 8b54cc84d0..7301b620ad 100644 --- a/spec/example_app/app/views/admin/customers/_index_header.html.erb +++ b/spec/example_app/app/views/admin/customers/_index_header.html.erb @@ -28,7 +28,6 @@ ), [:new, namespace, page.resource_path.to_sym], class: "button", - ) if valid_action?(:new) && show_action?(:new, new_resource) %> + ) if administrate_valid_action?(new_resource, :new) %> -