Skip to content

Commit e940baf

Browse files
pcreuxgregbell
authored andcommitted
Resource names are now titleized.
1 parent 2515a82 commit e940baf

File tree

2 files changed

+29
-24
lines changed

2 files changed

+29
-24
lines changed

lib/active_admin/resource/naming.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
module ActiveAdmin
22
class Resource < Config
33
module Naming
4-
# Returns the name to call this resource.
4+
# Returns the name to call this resource such as "Bank Account"
55
def resource_name
66
@resource_name ||= @options[:as]
77
@resource_name ||= singular_human_name
88
@resource_name ||= resource.name.gsub('::',' ')
99
end
1010

11-
# Returns the plural version of this resource
11+
# Returns the plural version of this resource such as "Bank Accounts"
1212
def plural_resource_name
1313
@plural_resource_name ||= @options[:as].pluralize if @options[:as]
1414
@plural_resource_name ||= plural_human_name
@@ -17,18 +17,18 @@ def plural_resource_name
1717

1818
private
1919

20-
# @return [String] Human name via ActiveRecord I18n or nil
20+
# @return [String] Titleized human name via ActiveRecord I18n or nil
2121
def singular_human_name
2222
return nil unless resource.respond_to?(:model_name)
23-
resource.model_name.human
23+
resource.model_name.human.titleize
2424
end
2525

26-
# @return [String] Plural human name via ActiveRecord I18n or nil
26+
# @return [String] Titleized plural human name via ActiveRecord I18n or nil
2727
def plural_human_name
2828
return nil unless resource.respond_to?(:model_name)
29+
2930
begin
30-
I18n.translate!("activerecord.models.#{resource.model_name.i18n_key}")
31-
resource.model_name.human(:count => 3)
31+
I18n.translate!("activerecord.models.#{resource.model_name.underscore}.other").titleize
3232
rescue I18n::MissingTranslationData
3333
nil
3434
end

spec/unit/resource/naming_spec.rb

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,40 +34,45 @@ module ::Mock; class Resource; end; end
3434

3535
describe "camelized resource name" do
3636
it "should return a camelized version of the underscored resource name" do
37-
config(:as => "Blog Category").camelized_resource_name.should == "BlogCategory"
37+
config(:as => "Blog category").camelized_resource_name.should == "BlogCategory"
3838
end
3939
end
4040

41-
describe "plural underscored resource name" do
42-
before(:all) do
43-
class ::CandyCane; end
44-
@config = Resource.new(namespace, CandyCane, {})
45-
end
46-
47-
it "should return an underscored and pluralized resource name" do
48-
config.plural_underscored_resource_name.should == "candy_canes"
49-
end
50-
end
51-
5241
describe "resource name" do
5342
it "should return a pretty name" do
5443
config.resource_name.should == "Category"
5544
end
45+
5646
it "should return the plural version" do
5747
config.plural_resource_name.should == "Categories"
5848
end
49+
5950
context "when the :as option is given" do
6051
it "should return the custom name" do
6152
config(:as => "My Category").resource_name.should == "My Category"
6253
end
6354
end
64-
context "I18n" do
65-
before do
66-
I18n.stub(:translate) { 'Categorie' }
55+
56+
describe "I18n integration" do
57+
describe "singular name" do
58+
it "should return the titleized model_name.human" do
59+
Category.model_name.should_receive(:human).and_return "Da category"
60+
61+
config.resource_name.should == "Da Category"
62+
end
6763
end
68-
it "should return the plural version defined in the i18n if available" do
69-
config.plural_resource_name.should == "Categorie"
64+
65+
describe "plural name" do
66+
it "should return the titleized plural version defined by i18n if available" do
67+
Category.model_name.should_receive(:underscore).and_return "category"
68+
Category.model_name.should_not_receive(:i18n_key) # Not implemented in Rails 3.0.0
69+
I18n.should_receive(:translate!).
70+
with("activerecord.models.category.other").
71+
and_return("Da categories")
72+
config.plural_resource_name.should == "Da Categories"
73+
end
7074
end
75+
7176
end
7277
end
7378

0 commit comments

Comments
 (0)