diff --git a/app/views/fields/has_one/_show.html.erb b/app/views/fields/has_one/_show.html.erb index 542af5e09d..6e9497cc8c 100644 --- a/app/views/fields/has_one/_show.html.erb +++ b/app/views/fields/has_one/_show.html.erb @@ -28,7 +28,7 @@ All show page attributes of has_one relationship would be rendered
<%= t( - "helpers.label.#{resource_name}.#{attribute.name}", + "helpers.label.#{field.associated_class_name.underscore}.#{attribute.name}", default: attribute.name.titleize, ) %>
diff --git a/lib/administrate/field/associative.rb b/lib/administrate/field/associative.rb index b23be7500a..989fb6c899 100644 --- a/lib/administrate/field/associative.rb +++ b/lib/administrate/field/associative.rb @@ -31,12 +31,6 @@ def associated_class end end - private - - def associated_dashboard - "#{associated_class_name}Dashboard".constantize.new - end - def associated_class_name if option_given?(:class_name) deprecated_option(:class_name) @@ -48,6 +42,12 @@ def associated_class_name end end + private + + def associated_dashboard + "#{associated_class_name}Dashboard".constantize.new + end + def primary_key if option_given?(:primary_key) deprecated_option(:primary_key) diff --git a/spec/administrate/views/fields/has_one/_show_spec.rb b/spec/administrate/views/fields/has_one/_show_spec.rb index 76b7aeb986..9bbee8c5ce 100644 --- a/spec/administrate/views/fields/has_one/_show_spec.rb +++ b/spec/administrate/views/fields/has_one/_show_spec.rb @@ -3,10 +3,12 @@ describe "fields/has_one/_show", type: :view do before do + view.extend Administrate::ApplicationHelper allow(view).to receive(:accessible_action?).and_return(true) + allow(view).to receive(:namespace).and_return(:admin) end - context "without an associated record" do + context "without a persisted record" do it "displays nothing" do has_one = Administrate::Field::HasOne.new( :product_meta_tag, @@ -23,124 +25,156 @@ end end - context "with an associated record" do - 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", - }, - ) + context "with a persisted record" do + before do + field_resource = create(:product_meta_tag) + @path_to_field_resource = polymorphic_path([:admin, field_resource]) + + nested_simple_field = instance_double( + "Administrate::Field::String", + name: "simple_string_field", + truncate: "string value", + html_class: "string", + to_partial_path: "fields/string/index", + ) - link = "#{product.name}" - expect(rendered.strip).to include(link) - end + nested_show_page_for_has_one = instance_double( + "Administrate::Page::Show", + resource: double( + class: ProductMetaTag, + ), + attributes: [ + nested_simple_field, + ], + ) + + @has_one_field = instance_double( + "Administrate::Field::HasOne", + display_associated_resource: "The Nested Resource", + data: field_resource, + linkable?: true, + nested_show: nested_show_page_for_has_one, + associated_class_name: "NestedHasOne", + ) + + @page_double = instance_double("Administrate::Page::Show") + 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: {}, - ) - - nested_show = instance_double( - "Administrate::Page::Show", - resource: double( - class: ProductMetaTag, - ), - attributes: [nested_has_many], - resource_name: "Product 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", + def render_field + render( + partial: "fields/has_one/show", + locals: { + field: @has_one_field, + page: @page_double, + resource_name: "parent_resource", + }, + ) + end + + it "uses the correct labels for fields" do + I18n.backend.translations(do_init: true)[:en].deep_merge!( + helpers: { + label: { + nested_has_one: { + simple_string_field: "Just a Simple String", + }, }, - ) + }, + ) + + render_field + expect(rendered.strip).to include("Just a Simple String") + end - has_many_count = "1 page" - expect(rendered.strip).to include(has_many_count) + context "when linking the record is allowed" do + it "renders a link to the record" do + render_field + link = "The Nested Resource" + expect(rendered.strip).to include(link) end end context "when linking the record is not allowed" do it "displays the record without a link" do allow(view).to receive(:accessible_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", - }, - ) - - expect(rendered.strip).to include(product.name) - expect(rendered.strip).not_to include("