Skip to content

Commit

Permalink
Make translated labels work on index, show pages
Browse files Browse the repository at this point in the history
Problem:

At the moment, developers can customize a field's label on form pages
with the following translation structure:

```
en:
  helpers:
    label:
      customer:
        name: Full Name
```

Many developers expect this label change to take effect
on the `show` and `index` pages, as well as the form page.

Solution:

Translate attribute labels with I18n before displaying them
on `show` and `index` pages.
  • Loading branch information
c-lliope committed Apr 12, 2016
1 parent 47e2fc4 commit 68cba4d
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 6 deletions.
1 change: 1 addition & 0 deletions app/controllers/administrate/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ def permitted_attributes

delegate :resource_class, :resource_name, :namespace, to: :resource_resolver
helper_method :namespace
helper_method :resource_name

def resource_resolver
@_resource_resolver ||=
Expand Down
10 changes: 7 additions & 3 deletions app/views/administrate/application/_collection.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,17 @@ to display a collection of resources in an HTML table.
<thead>
<tr>
<% collection_presenter.attribute_types.each do |attr_name, attr_type| %>
<th class="cell-label cell-label--<%= attr_type.html_class %>
cell-label--<%= collection_presenter.ordered_html_class(attr_name) %>
<th class="cell-label
cell-label--<%= attr_type.html_class %>
cell-label--<%= collection_presenter.ordered_html_class(attr_name) %>
" scope="col">
<%= link_to(params.merge(
collection_presenter.order_params_for(attr_name)
)) do %>
<%= attr_name.to_s.titleize %>
<%= t(
"helpers.label.#{resource_name}.#{attr_name}",
default: attr_name.to_s,
).titleize %>

<% if collection_presenter.ordered_by?(attr_name) %>
<span class="cell-label__sort-indicator cell-label__sort-indicator--<%= collection_presenter.ordered_html_class(attr_name) %>">
Expand Down
7 changes: 6 additions & 1 deletion app/views/administrate/application/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ as well as a link to its edit page.

<dl>
<% page.attributes.each do |attribute| %>
<dt class="attribute-label"><%= attribute.name.titleize %></dt>
<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 %></dd>
Expand Down
2 changes: 1 addition & 1 deletion app/views/fields/date_time/_show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ as a localized date & time string.
%>

<% if field.data %>
<%= l field.data %>
<%= l(field.data, default: field.data) %>
<% end %>
2 changes: 1 addition & 1 deletion app/views/fields/has_many/_show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ from the associated resource class's dashboard.
<% end %>

<% else %>
<%= t("administrate.fields.has_many.none") %>
<%= t("administrate.fields.has_many.none", default: "–") %>
<% end %>
20 changes: 20 additions & 0 deletions spec/features/index_page_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,26 @@
expect(current_path).to eq(new_admin_customer_path)
end

it "displays translated labels" do
custom_label = "Newsletter Subscriber"

translations = {
helpers: {
label: {
customer: {
email_subscriber: custom_label,
},
},
},
}

with_translations(:en, translations) do
visit admin_customers_path

expect(page).to have_table_header(custom_label)
end
end

it "paginates records based on a constant" do
customers = create_list(:customer, 2)

Expand Down
21 changes: 21 additions & 0 deletions spec/features/show_page_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,25 @@

expect(page).to have_header("Edit #{displayed(customer)}")
end

it "displays translated labels" do
custom_label = "Newsletter Subscriber"
customer = create(:customer)

translations = {
helpers: {
label: {
customer: {
email_subscriber: custom_label,
},
},
},
}

with_translations(:en, translations) do
visit admin_customer_path(customer)

expect(page).to have_css(".attribute-label", text: custom_label)
end
end
end
4 changes: 4 additions & 0 deletions spec/support/features/page_elements.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ def have_header(title)
def have_label(title)
have_css("label", text: title)
end

def have_table_header(title)
have_css("th", text: title)
end
end

0 comments on commit 68cba4d

Please sign in to comment.