-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support translations for ActiveRecord models #298
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,5 +4,17 @@ def render_field(field, locals = {}) | |
locals.merge!(field: field) | ||
render locals: locals, partial: field.to_partial_path | ||
end | ||
|
||
def display_resource_name(resource_name) | ||
resource_name. | ||
to_s. | ||
classify. | ||
constantize. | ||
model_name. | ||
human( | ||
count: 0, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this always 0? how do we handle plurals? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @JoelQ this dictates how the resource names show up in the sidebar, and in the header on the index page. I think in both of those cases, we always want the displayed name to be in the plural form. I started to code out an argument to this function that would let you specify a custom count, but I think YAGNI applies to that. If we need to display the singular form in the future, we can adjust this method accordingly. |
||
default: resource_name.to_s.pluralize.titleize, | ||
) | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,7 @@ as defined by the DashboardManifest. | |
<% DashboardManifest::DASHBOARDS.each do |resource| %> | ||
<li> | ||
<%= link_to( | ||
resource.to_s.titleize, | ||
display_resource_name(resource), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I honestly think that the mapping from a resource name to a model class shouldn't be inferred and should always be explicit. This would obviate the need to do There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
[Administrate::NAMESPACE, resource], | ||
class: "sidebar__link sidebar__link--#{nav_link_state(resource)}" | ||
) %> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,4 +8,25 @@ | |
|
||
expect(active_link.text).to eq "Customers" | ||
end | ||
|
||
it "displays translated name of model" do | ||
translations = { | ||
activerecord: { | ||
models: { | ||
customer: { | ||
one: "User", | ||
other: "Users", | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
with_translations(:en, translations) do | ||
visit admin_customers_path | ||
|
||
sidebar = find(".sidebar__list") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any reason this has double underscores? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We're using BEM syntax for our CSS. In this case, the CSS class would read as: "The list element inside the sidebar block", or something like that. CSS selector formats are something that we could have a whole other discussion on - I'm still not sure what the best solution is for Administrate. I chose BEM because many designers at thoughtbot have had pleasant experiences working with it, and it makes it a bit easier to write maintainable (S)CSS. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
expect(sidebar).to have_link("Users") | ||
expect(page).to have_header("Users") | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
require "rails_helper" | ||
|
||
RSpec.describe Administrate::ApplicationHelper do | ||
describe "#display_resource_name" do | ||
it "defaults to the plural of the model name" do | ||
displayed = display_resource_name(:customer) | ||
|
||
expect(displayed).to eq("Customers") | ||
end | ||
|
||
it "handles string arguments" do | ||
displayed = display_resource_name("customer") | ||
|
||
expect(displayed).to eq("Customers") | ||
end | ||
|
||
it "handles plural arguments" do | ||
displayed = display_resource_name(:customers) | ||
|
||
expect(displayed).to eq("Customers") | ||
end | ||
|
||
context "when translations are defined" do | ||
it "uses the plural of the defined translation" do | ||
translations = { | ||
activerecord: { | ||
models: { | ||
customer: { | ||
one: "User", | ||
other: "Users", | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
with_translations(:en, translations) do | ||
displayed = display_resource_name(:customer) | ||
|
||
expect(displayed).to eq("Users") | ||
end | ||
end | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,14 @@ | ||
RSpec.configure do |config| | ||
config.include AbstractController::Translation | ||
|
||
def with_translations(locale, translations) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice method here, love that it takes a block. |
||
original_backend = I18n.backend | ||
|
||
I18n.backend = I18n::Backend::KeyValue.new({}, true) | ||
I18n.backend.store_translations(locale, translations) | ||
|
||
yield | ||
ensure | ||
I18n.backend = original_backend | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this just be
resource
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would think that "resource" would refer to the whole model object, but that's just me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I'm siding with @mcmire on this. I've been using
resource
to refer to instances of the ActiveRecord models. Here, we're referring to the class names of the ActiveRecord models.I think if we end up going with @mcmire's suggestion in #298 (comment), then we should rename this to
resource_class
or something.