Skip to content

Commit

Permalink
Correct string for translation key
Browse files Browse the repository at this point in the history
  • Loading branch information
pablobm committed May 18, 2022
1 parent d59869c commit 5cff187
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 15 deletions.
18 changes: 9 additions & 9 deletions app/views/fields/has_one/_show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ All show page attributes of has_one relationship would be rendered
</legend>
<% field.nested_show.attributes.each do |attribute| -%>
<div>
<dt class="attribute-label">
<%= t(
"helpers.label.#{resource_name}.#{attribute.name}",
default: attribute.name.titleize,
) %>
</dt>
<dd class="attribute-data attribute-data--<%= attribute.html_class %>">
<%= render_field attribute, { page: page } %>
</dd>
<dt class="attribute-label">
<%= t(
"helpers.label.#{field.associated_class_name.underscore}.#{attribute.name}",
default: attribute.name.titleize,
) %>
</dt>
<dd class="attribute-data attribute-data--<%= attribute.html_class %>">
<%= render_field attribute, { page: page } %>
</dd>
</div>
<% end -%>
</fieldset>
Expand Down
12 changes: 6 additions & 6 deletions lib/administrate/field/associative.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
56 changes: 56 additions & 0 deletions spec/administrate/views/fields/has_one/_show_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,61 @@ def view.namespace
link = "<a href=\"#{path_to_field_resource}\">The Nested Resource</a>"
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
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 5cff187

Please sign in to comment.