Skip to content

Commit

Permalink
Translate model name label on Dashboard index (thoughtbot#1777)
Browse files Browse the repository at this point in the history
Previously, the "New [model]" button on the index page wasn't
translated, this uses `display_resource_name` to translate that
correctly.

Fixes thoughtbot#1677, thoughtbot#1072
Closes thoughtbot#1627

This isn't perfect for all languages, so thoughtbot#1783 will cover incorrect
downcasing.

Co-authored-by: Pablo Brasero <pablo@pablobm.com>
Co-authored-by: Rachael Ewins <rachaelewins@hotmail.com>
  • Loading branch information
3 people authored Oct 6, 2020
1 parent a03a450 commit 06a5a1f
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 9 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ gem "administrate-field-image"
gem "faker"
gem "front_matter_parser"
gem "globalid"
gem "kaminari-i18n"
gem "pg"
gem "redcarpet"
gem "sentry-raven"
Expand Down
59 changes: 59 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,55 @@ PATH
GEM
remote: https://rubygems.org/
specs:
actioncable (6.0.3.3)
actionpack (= 6.0.3.3)
nio4r (~> 2.0)
websocket-driver (>= 0.6.1)
actionmailbox (6.0.3.3)
actionpack (= 6.0.3.3)
activejob (= 6.0.3.3)
activerecord (= 6.0.3.3)
activestorage (= 6.0.3.3)
activesupport (= 6.0.3.3)
mail (>= 2.7.1)
actionmailer (6.0.3.3)
actionpack (= 6.0.3.3)
actionview (= 6.0.3.3)
activejob (= 6.0.3.3)
mail (~> 2.5, >= 2.5.4)
rails-dom-testing (~> 2.0)
actionpack (6.0.3.3)
actionview (= 6.0.3.3)
activesupport (= 6.0.3.3)
rack (~> 2.0, >= 2.0.8)
rack-test (>= 0.6.3)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.0, >= 1.2.0)
actiontext (6.0.3.3)
actionpack (= 6.0.3.3)
activerecord (= 6.0.3.3)
activestorage (= 6.0.3.3)
activesupport (= 6.0.3.3)
nokogiri (>= 1.8.5)
actionview (6.0.3.3)
activesupport (= 6.0.3.3)
builder (~> 3.1)
erubi (~> 1.4)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.1, >= 1.2.0)
activejob (6.0.3.3)
activesupport (= 6.0.3.3)
globalid (>= 0.3.6)
activemodel (6.0.3.3)
activesupport (= 6.0.3.3)
activerecord (6.0.3.3)
activemodel (= 6.0.3.3)
activesupport (= 6.0.3.3)
activestorage (6.0.3.3)
actionpack (= 6.0.3.3)
activejob (= 6.0.3.3)
activerecord (= 6.0.3.3)
marcel (~> 0.3.1)
activesupport (6.0.3.3)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 0.7, < 2)
Expand Down Expand Up @@ -129,19 +160,28 @@ GEM
activerecord
kaminari-core (= 1.2.1)
kaminari-core (1.2.1)
kaminari-i18n (0.5.0)
kaminari
rails
kgio (2.11.3)
launchy (2.5.0)
addressable (~> 2.7)
loofah (2.7.0)
crass (~> 1.0.2)
nokogiri (>= 1.5.9)
mail (2.7.1)
mini_mime (>= 0.1.1)
marcel (0.3.3)
mimemagic (~> 0.3.2)
method_source (0.9.2)
mimemagic (0.3.5)
mini_mime (1.0.2)
mini_portile2 (2.4.0)
minitest (5.14.2)
momentjs-rails (2.20.1)
railties (>= 3.1)
multipart-post (2.1.1)
nio4r (2.5.4)
nokogiri (1.10.10)
mini_portile2 (~> 2.4.0)
parser (2.7.0.4)
Expand All @@ -159,6 +199,21 @@ GEM
rack-test (1.1.0)
rack (>= 1.0, < 3)
rack-timeout (0.6.0)
rails (6.0.3.3)
actioncable (= 6.0.3.3)
actionmailbox (= 6.0.3.3)
actionmailer (= 6.0.3.3)
actionpack (= 6.0.3.3)
actiontext (= 6.0.3.3)
actionview (= 6.0.3.3)
activejob (= 6.0.3.3)
activemodel (= 6.0.3.3)
activerecord (= 6.0.3.3)
activestorage (= 6.0.3.3)
activesupport (= 6.0.3.3)
bundler (>= 1.3.0)
railties (= 6.0.3.3)
sprockets-rails (>= 2.0.0)
rails-dom-testing (2.0.3)
activesupport (>= 4.2.0)
nokogiri (>= 1.6)
Expand Down Expand Up @@ -238,6 +293,9 @@ GEM
addressable (>= 2.3.6)
crack (>= 0.3.2)
hashdiff (>= 0.4.0, < 2.0.0)
websocket-driver (0.7.3)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.5)
xpath (3.2.0)
nokogiri (~> 1.8)
yard (0.9.25)
Expand All @@ -263,6 +321,7 @@ DEPENDENCIES
front_matter_parser
globalid
i18n-tasks (= 0.9.31)
kaminari-i18n
launchy
pg
pry-rails
Expand Down
12 changes: 7 additions & 5 deletions app/helpers/administrate/application_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module Administrate
module ApplicationHelper
PLURAL_MANY_COUNT = 2.1
SINGULAR_COUNT = 1

def application_title
if Rails::VERSION::MAJOR <= 5
Expand Down Expand Up @@ -43,10 +44,10 @@ def model_from_resource(resource_name)
dashboard.try(:model) || resource_name.to_sym
end

def display_resource_name(resource_name)
def display_resource_name(resource_name, opts = {})
dashboard_from_resource(resource_name).resource_name(
count: PLURAL_MANY_COUNT,
default: default_resource_name(resource_name),
count: opts[:singular] ? SINGULAR_COUNT : PLURAL_MANY_COUNT,
default: default_resource_name(resource_name, opts),
)
end

Expand Down Expand Up @@ -81,8 +82,9 @@ def clear_search_params

private

def default_resource_name(resource_name)
resource_name.to_s.pluralize.gsub("/", "_").titleize
def default_resource_name(name, opts = {})
resource_name = (opts[:singular] ? name.to_s : name.to_s.pluralize)
resource_name.gsub("/", "_").titleize
end
end
end
2 changes: 1 addition & 1 deletion app/views/administrate/application/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ and renders all form fields for a resource's editable attributes.
<%= t(
"administrate.form.errors",
pluralized_errors: pluralize(page.resource.errors.count, t("administrate.form.error")),
resource_name: display_resource_name(page.resource_name).singularize
resource_name: display_resource_name(page.resource_name, singular: true)
) %>
</h2>

Expand Down
2 changes: 1 addition & 1 deletion app/views/administrate/application/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ It renders the `_table` partial to display details about the resources.
<%= link_to(
t(
"administrate.actions.new_resource",
name: page.resource_name.titleize.downcase
name: display_resource_name(page.resource_name, singular: true).downcase
),
[:new, namespace, page.resource_path],
class: "button",
Expand Down
20 changes: 18 additions & 2 deletions spec/helpers/administrate/application_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
expect(displayed).to eq("Customers")
end

it "can return singular of the model name" do
displayed = display_resource_name(:customer, singular: true)

expect(displayed).to eq("Customer")
end

it "handles string arguments" do
displayed = display_resource_name("customer")

Expand All @@ -27,8 +33,8 @@
end

context "when translations are defined" do
it "uses the plural of the defined translation" do
translations = {
let(:translations) do
{
activerecord: {
models: {
customer: {
Expand All @@ -38,13 +44,23 @@
},
},
}
end

it "uses the plural of the defined translation as default" do
with_translations(:en, translations) do
displayed = display_resource_name(:customer)

expect(displayed).to eq("Users")
end
end

it "uses the singular of the defined translation" do
with_translations(:en, translations) do
displayed = display_resource_name(:customer, singular: true)

expect(displayed).to eq("User")
end
end
end

context "using custom dashboards" do
Expand Down

0 comments on commit 06a5a1f

Please sign in to comment.