From 5cff187c62215207c99f3be12479b1de1f2a70b1 Mon Sep 17 00:00:00 2001 From: Pablo Brasero Date: Fri, 13 May 2022 07:33:20 +0100 Subject: [PATCH] Correct string for translation key --- app/views/fields/has_one/_show.html.erb | 18 +++--- lib/administrate/field/associative.rb | 12 ++-- .../views/fields/has_one/_show_spec.rb | 56 +++++++++++++++++++ 3 files changed, 71 insertions(+), 15 deletions(-) diff --git a/app/views/fields/has_one/_show.html.erb b/app/views/fields/has_one/_show.html.erb index a4b3dc5fe8..b335d82b6e 100644 --- a/app/views/fields/has_one/_show.html.erb +++ b/app/views/fields/has_one/_show.html.erb @@ -25,15 +25,15 @@ All show page attributes of has_one relationship would be rendered <% field.nested_show.attributes.each do |attribute| -%>
-
- <%= t( - "helpers.label.#{resource_name}.#{attribute.name}", - default: attribute.name.titleize, - ) %> -
-
- <%= render_field attribute, { page: page } %> -
+
+ <%= t( + "helpers.label.#{field.associated_class_name.underscore}.#{attribute.name}", + default: attribute.name.titleize, + ) %> +
+
+ <%= render_field attribute, { page: page } %> +
<% end -%> diff --git a/lib/administrate/field/associative.rb b/lib/administrate/field/associative.rb index b24bee1054..1504707f3d 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 412d57f879..3d728f9f9e 100644 --- a/spec/administrate/views/fields/has_one/_show_spec.rb +++ b/spec/administrate/views/fields/has_one/_show_spec.rb @@ -58,6 +58,61 @@ def view.namespace link = "The Nested Resource" expect(rendered.strip).to include(link) end + + it "uses the correct labels for fields" 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", + ) + + 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") + + I18n.backend.translations(do_init: true)[:en].deep_merge!( + helpers: { + label: { + nested_has_one: { + simple_string_field: "Just a Simple String" + } + } + } + ) + + render( + partial: "fields/has_one/show", + locals: { + field: has_one_field, + page: page_double, + resource_name: "parent_resource", + }, + ) + + expect(rendered.strip).to include("Just a Simple String") + end end context "when there are nested associations" do @@ -113,6 +168,7 @@ def view.namespace data: field_resource, linkable?: true, nested_show: nested_show_page_for_top_has_one, + associated_class_name: "NameOfAssociatedClass", ) page_double = instance_double("Administrate::Page::Show")