Permalink
Browse files

Refactor Resource::Naming and use translations only in the html (fixes

…#820)

* Resource#resource_name now returns a ActiveModel::Name
* Add Resource#resource_label and #plural_resource_label to return a (human readable) label
* ActiveAdmin.register Post, :as => "Super Post" would change #resource_label to to "Super Post" and #resource_name to "SuperPost"
* The routes are longer translated
* Use resource_label instead of resource_name in the html
* Add feature
  • Loading branch information...
1 parent 9e4fd8f commit 28dc6ba4fcf77f0f7c11490a6459a0b9b23356b2 @ahx ahx committed with gregbell Dec 7, 2011
View
@@ -0,0 +1,19 @@
+Feature: Internationalization
+
+ ActiveAdmin should use the translations provided by the host app.
+
+ Scenario: Store's model name was translated to "Bookstore"
+ Given I am logged in
+ And a store named "Hello words" exists
+ When I go to the dashboard
+ Then I should see "Bookstores"
+ When I follow "Bookstores"
+ Then I should see "Hello words"
+ When I follow "View"
+ Then I should see "Bookstore Details"
+ And I should see "Bookstore #"
+ And I should see a link to "Delete Bookstore"
+ When I follow "Edit Bookstore"
+ Then I should see "Edit Bookstore"
+ When I press "Update Bookstore"
+ Then I should see a flash with "Bookstore was successfully updated."
@@ -35,3 +35,7 @@
fill_in :title, :with => title
click_button "Create Post"
end
+
+Given /^a store named "([^"]*)" exists$/ do |name|
+ Store.create! :name => name
+end
View
@@ -35,24 +35,17 @@ def initialize(namespace, name, options)
include Resource::Menu
include Resource::Naming
- # plural_resource_name is singular
- def resource_name
- name
- end
-
- def safe_resource_name
+ # label is singular
+ def plural_resource_label
name
end
-
- # Force plural to be singular
- def plural_resource_name
- name
+ def resource_name
+ @resource_name ||= ActiveModel::Name.new(nil, nil, name)
end
- # Force plural to be singular
- def plural_safe_resource_name
- name
+ def controller_name
+ [namespace.module_name, resource_name + "Controller"].compact.join('::')
end
def belongs_to?
@@ -54,21 +54,21 @@ def add_default_action_items
# New Link on all actions except :new and :show
add_action_item :except => [:new, :show] do
if controller.action_methods.include?('new')
- link_to(I18n.t('active_admin.new_model', :model => active_admin_config.resource_name), new_resource_path)
+ link_to(I18n.t('active_admin.new_model', :model => active_admin_config.resource_label), new_resource_path)
end
end
# Edit link on show
add_action_item :only => :show do
if controller.action_methods.include?('edit')
- link_to(I18n.t('active_admin.edit_model', :model => active_admin_config.resource_name), edit_resource_path(resource))
+ link_to(I18n.t('active_admin.edit_model', :model => active_admin_config.resource_label), edit_resource_path(resource))
end
end
# Destroy link on show
add_action_item :only => :show do
if controller.action_methods.include?("destroy")
- link_to(I18n.t('active_admin.delete_model', :model => active_admin_config.resource_name),
+ link_to(I18n.t('active_admin.delete_model', :model => active_admin_config.resource_label),
resource_path(resource),
:method => :delete, :confirm => I18n.t('active_admin.delete_confirmation'))
end
@@ -5,7 +5,7 @@ module Controllers
# Returns a properly formatted controller name for this
# config within its namespace
def controller_name
- [namespace.module_name, plural_camelized_resource_name + "Controller"].compact.join('::')
+ [namespace.module_name, resource_name.plural.camelize + "Controller"].compact.join('::')
end
# Returns the controller for this config
@@ -21,7 +21,7 @@ def parent_menu_item_name
# Returns the name to be displayed in the menu for this resource
def menu_item_name
- menu_options[:label] || plural_resource_name
+ menu_options[:label] || plural_resource_label
end
# Returns the items priority for altering the default sort order
@@ -1,71 +1,37 @@
module ActiveAdmin
class Resource
module Naming
- # Returns the user facing name. Example: "Bank Account"
- def resource_name
- @resource_name ||= @options[:as]
- @resource_name ||= singular_human_name
- @resource_name ||= safe_resource_name
- end
- # Returns the plural version of the user facing name. Example: "Bank Accounts"
- def plural_resource_name
- @plural_resource_name ||= @options[:as].pluralize if @options[:as]
- @plural_resource_name ||= plural_human_name
- @plural_resource_name ||= resource_name.pluralize
+ def resource_name
+ custom_name = @options[:as] && @options[:as].gsub(/\s/,'')
+ @resource_name ||= if custom_name || !resource_class.respond_to?(:model_name)
+ ActiveModel::Name.new(resource_class, nil, custom_name)
+ else
+ resource_class.model_name
+ end
+ end
+
+ # Returns the name to call this resource such as "Bank Account"
+ def resource_label
+ if @options[:as]
+ @options[:as]
+ else
+ (@resource_label ||= {})[::I18n.locale] ||= resource_name.human(:default => resource_name.gsub('::', ' ')).titleize
+ end
+ end
+
+ # Returns the plural version of this resource such as "Bank Accounts"
+ def plural_resource_label
+ if @options[:as]
+ @options[:as].pluralize
+ else
+ (@plural_resource_label ||= {})[::I18n.locale] ||= resource_name.human(:count => 3, :default => resource_label.pluralize).titleize
+ end
end
# A name used internally to uniquely identify this resource
def resource_key
- camelized_resource_name
- end
-
- def safe_resource_name
- @safe_resource_name ||= @options[:as]
- @safe_resource_name ||= resource_class.name.gsub('::',' ')
- end
-
- def plural_safe_resource_name
- safe_resource_name.pluralize
- end
-
- # A camelized safe representation for this resource
- def camelized_resource_name
- safe_resource_name.titleize.gsub(' ', '')
- end
-
- def plural_camelized_resource_name
- plural_safe_resource_name.titleize.gsub(' ', '')
- end
-
- # An underscored safe representation internally for this resource
- def underscored_resource_name
- camelized_resource_name.underscore
- end
-
- # Returns the plural and underscored version of this resource. Useful for element id's.
- def plural_underscored_resource_name
- plural_camelized_resource_name.underscore
- end
-
-
- private
-
- # @return [String] Titleized human name via ActiveRecord I18n or nil
- def singular_human_name
- return nil unless resource_class.respond_to?(:model_name)
- resource_class.model_name.human.titleize
- end
-
- # @return [String] Titleized plural human name via ActiveRecord I18n or nil
- def plural_human_name
- return nil unless resource_class.respond_to?(:model_name)
-
- begin
- I18n.translate!("activerecord.models.#{resource_class.model_name.underscore}.other").titleize
- rescue I18n::MissingTranslationData
- nil
- end
+ resource_name
end
end
end
@@ -32,7 +32,7 @@ def active_admin_config=(config)
defaults :resource_class => config.resource_class,
:route_prefix => config.route_prefix,
- :instance_name => config.underscored_resource_name
+ :instance_name => config.resource_name.singular
end
# Inherited Resources uses the inherited(base) hook method to
@@ -35,7 +35,7 @@ def apply(router)
route_definition_block = Proc.new do
case config
when Resource
- resources config.underscored_resource_name.pluralize do
+ resources config.resource_name.route_key do
# Define any member actions
member do
config.member_actions.each do |action|
@@ -52,7 +52,8 @@ def apply(router)
end
end
when Page
- match "/#{config.underscored_resource_name}" => "#{config.underscored_resource_name}#index"
+
+ match "/#{config.resource_name.singular}" => "#{config.resource_name.singular}#index"
config.page_actions.each do |action|
match "/#{config.underscored_resource_name}/#{action.name}" => "#{config.underscored_resource_name}##{action.name}", :via => action.http_verb
end
@@ -70,7 +71,7 @@ def apply(router)
# Make the nested belongs_to routes
# :only is set to nothing so that we don't clobber any existing routes on the resource
- resources config.belongs_to_config.target.underscored_resource_name.pluralize, :only => [] do
+ resources config.belongs_to_config.target.resource_name.plural, :only => [] do
instance_eval &routes_for_belongs_to
end
end
@@ -94,7 +94,7 @@ class IndexAsTable < ActiveAdmin::Component
def build(page_presenter, collection)
table_options = {
- :id => active_admin_config.plural_underscored_resource_name,
+ :id => active_admin_config.resource_name.plural,
:sortable => true,
:class => "index_table",
:i18n => active_admin_config.resource_class,
@@ -6,7 +6,7 @@ class Form < Base
def title
I18n.t("active_admin.#{params[:action]}_model",
- :model => active_admin_config.resource_name)
+ :model => active_admin_config.resource_label)
end
def form_presenter
@@ -30,7 +30,7 @@ def main_content
def default_form_options
{
:url => default_form_path,
- :as => active_admin_config.underscored_resource_name
+ :as => active_admin_config.resource_name.singular
}
end
@@ -5,7 +5,7 @@ module Pages
class Index < Base
def title
- active_admin_config.plural_resource_name
+ active_admin_config.plural_resource_label
end
def config
@@ -81,7 +81,7 @@ def find_index_renderer_class(symbol_or_class)
end
def render_blank_slate
- blank_slate_content = I18n.t("active_admin.blank_slate.content", :resource_name => active_admin_config.plural_resource_name)
+ blank_slate_content = I18n.t("active_admin.blank_slate.content", :resource_name => active_admin_config.plural_resource_label)
if controller.action_methods.include?('new')
blank_slate_content += " " + link_to(I18n.t("active_admin.blank_slate.link"), new_resource_path)
end
@@ -99,7 +99,7 @@ def render_index
download_links = config[:download_links].nil? ? true : config[:download_links]
paginated_collection(collection, :entry_name => active_admin_config.resource_name,
- :entries_name => active_admin_config.plural_resource_name,
+ :entries_name => active_admin_config.plural_resource_label,
:download_links => download_links,
:paginator => paginator) do
div :class => 'index_content' do
@@ -28,15 +28,15 @@ def main_content
end
def attributes_table(*args, &block)
- panel(I18n.t('active_admin.details', :model => active_admin_config.resource_name)) do
+ panel(I18n.t('active_admin.details', :model => active_admin_config.resource_label)) do
attributes_table_for resource, *args, &block
end
end
protected
def default_title
- "#{active_admin_config.resource_name} ##{resource.id}"
+ "#{active_admin_config.resource_label} ##{resource.id}"
end
module DefaultMainContent
@@ -16,6 +16,7 @@
generate :model, "publisher --migration=false --parent=User"
generate :model, 'category name:string description:text'
inject_into_file 'app/models/category.rb', " has_many :posts\n", :after => "class Category < ActiveRecord::Base\n"
+generate :model, 'store name:string'
# Generate a model with string ids
generate :model, "tag name:string"
@@ -38,6 +39,12 @@ def set_id
# Add our local Active Admin to the load path
inject_into_file "config/environment.rb", "\n$LOAD_PATH.unshift('#{File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'lib'))}')\nrequire \"active_admin\"\n", :after => "require File.expand_path('../application', __FILE__)"
+# Add some translations
+append_file "config/locales/en.yml", File.read(File.expand_path('../templates/en.yml', __FILE__))
+
+# Add predefined admin resources
+directory File.expand_path('../templates/admin', __FILE__), "app/admin"
+
run "rm Gemfile"
run "rm -r test"
run "rm -r spec"
@@ -0,0 +1 @@
+ActiveAdmin.register Store
@@ -0,0 +1,5 @@
+# Sample translations used to test ActiveAdmin's I18n integration.
+ activerecord:
+ models:
+ one: Bookstore
+ other: Bookstores
@@ -21,51 +21,19 @@
it { respond_to :sidebar_sections? }
describe "Naming" do
- it "implements #resource_name" do
- expect { config.resource_name }.should_not raise_error
+ it "implements #resource_label" do
+ expect { config.resource_label }.should_not raise_error
end
- it "implements #plural_resource_name" do
- expect { config.plural_resource_name }.should_not raise_error
- end
-
- it "implements #safe_resource_name" do
- expect { config.safe_resource_name }.should_not raise_error
- end
-
- describe "#camelized_resource_name" do
- it "returns a camelized version of the safe_resource_name" do
- config.should_receive(:safe_resource_name).and_return "My resource"
- config.camelized_resource_name.should == "MyResource"
- end
- end
-
- describe "#plural_camelized_resource_name" do
- it "returns a camelized version of the plural_safe_resource_name" do
- config.should_receive(:safe_resource_name).and_return "My resource"
- config.camelized_resource_name.should == "MyResource"
- end
- end
-
- describe "#underscored_resource_name" do
- it "returns the camelized_resource_name underscored" do
- config.should_receive(:camelized_resource_name).and_return "MyResource"
- config.underscored_resource_name.should == "my_resource"
- end
- end
-
- describe "#plural_underscored_resource_name" do
- it "returns the plural_camelized_resource_name underscored" do
- config.should_receive(:plural_camelized_resource_name).and_return "MyResources"
- config.plural_underscored_resource_name.should == "my_resources"
- end
+ it "implements #plural_resource_label" do
+ expect { config.plural_resource_label }.should_not raise_error
end
end
describe "Menu" do
describe "menu item name" do
- it "should be the plural resource name when not set" do
- config.menu_item_name.should == config.plural_resource_name
+ it "should be the plural resource label when not set" do
+ config.menu_item_name.should == config.plural_resource_label
end
it "should be settable" do
config.menu :label => "My Label"
Oops, something went wrong.

0 comments on commit 28dc6ba

Please sign in to comment.