Skip to content

Commit 54379ae

Browse files
pcreuxgregbell
authored andcommitted
Refactor Naming for Config and Resource!
Note: The documentation says that when registering a resource you can set its name doing: ``` ActiveAdmin.register Post, :as => "Article" ``` The code however was made so that the :as resource would get a plural. For instance: ``` ActiveAdmin.register Post, :as => "Articles" ``` I fixed this. It might be worth mentioning in the release notes.
1 parent 4eb4b4a commit 54379ae

File tree

5 files changed

+52
-50
lines changed

5 files changed

+52
-50
lines changed

lib/active_admin/config/naming.rb

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,35 @@
11
module ActiveAdmin
22
class Config
33
module Naming
4+
# @return [String] Example: "My Post"
45
def resource_name
56
raise "Subclasses of Config should implement resource_name"
67
end
78

9+
# @return [String] Example: "My Posts"
810
def plural_resource_name
911
raise "Subclasses of Config should implement plural_resource_name"
1012
end
1113

12-
def underscored_resource_name
13-
raise "Subclasses of Config should implement underscored_resource_name"
14-
end
15-
1614
# A camelized safe representation for this resource
1715
def camelized_resource_name
18-
underscored_resource_name.camelize
16+
resource_name.gsub(' ', '')
17+
end
18+
19+
def plural_camelized_resource_name
20+
plural_resource_name.gsub(' ', '')
21+
end
22+
23+
# An underscored safe representation internally for this resource
24+
def underscored_resource_name
25+
camelized_resource_name.underscore
1926
end
2027

2128
# Returns the plural and underscored version of this resource. Useful for element id's.
2229
def plural_underscored_resource_name
23-
plural_resource_name.underscore.gsub(/\s/, '_')
30+
plural_camelized_resource_name.underscore
2431
end
32+
2533
end
2634
end
2735
end

lib/active_admin/page.rb

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,8 @@ def plural_resource_name
1313
name
1414
end
1515

16-
# Overwrite Naming defaults as they require a resource object.
17-
# @todo Refactor Naming to remove the dependency with Resources
1816
def resource_name
1917
name
2018
end
21-
22-
# Overwrite Naming defaults as they require a resource object.
23-
# @todo Refactor Naming to remove the dependency with Resources
24-
def underscored_resource_name
25-
resource_name.underscore
26-
end
2719
end
2820
end

lib/active_admin/resource/naming.rb

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,36 @@
11
module ActiveAdmin
22
class Resource < Config
33
module Naming
4-
# An underscored safe representation internally for this resource
5-
def underscored_resource_name
6-
@underscored_resource_name ||= if @options[:as]
7-
@options[:as].gsub(' ', '').underscore.singularize
8-
else
9-
resource.name.gsub('::','').underscore
10-
end
11-
end
12-
134
# Returns the name to call this resource.
14-
# By default will use resource.model_name.human
155
def resource_name
16-
@resource_name ||= if @options[:as] || !resource.respond_to?(:model_name)
17-
underscored_resource_name.titleize
18-
else
19-
resource.model_name.human.titleize
20-
end
6+
@resource_name ||= @options[:as]
7+
@resource_name ||= singular_human_name
8+
@resource_name ||= resource.name.gsub('::',' ')
219
end
2210

2311
# Returns the plural version of this resource
2412
def plural_resource_name
25-
@plural_resource_name ||= if @options[:as] || !resource.respond_to?(:model_name)
26-
resource_name.pluralize
27-
else
28-
# Check if we have a translation available otherwise pluralize
29-
begin
30-
I18n.translate!("activerecord.models.#{resource.model_name.underscore}")
31-
resource.model_name.human(:count => 3)
32-
rescue I18n::MissingTranslationData
33-
resource_name.pluralize
34-
end
13+
@plural_resource_name ||= @options[:as].pluralize if @options[:as]
14+
@plural_resource_name ||= plural_human_name
15+
@plural_resource_name ||= resource_name.pluralize
16+
end
17+
18+
private
19+
20+
# @return [String] Human name via ActiveRecord I18n or nil
21+
def singular_human_name
22+
return nil unless resource.respond_to?(:model_name)
23+
resource.model_name.human
24+
end
25+
26+
# @return [String] Plural human name via ActiveRecord I18n or nil
27+
def plural_human_name
28+
return nil unless resource.respond_to?(:model_name)
29+
begin
30+
I18n.translate!("activerecord.models.#{resource.model_name.i18n_key}")
31+
resource.model_name.human(:count => 3)
32+
rescue I18n::MissingTranslationData
33+
nil
3534
end
3635
end
3736
end

spec/unit/config_shared_examples.rb

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,23 @@
2929
expect { config.plural_resource_name }.should_not raise_error
3030
end
3131

32-
it "implements #underscored_resource_name" do
33-
expect { config.underscored_resource_name }.should_not raise_error
34-
end
35-
3632
describe "#camelized_resource_name" do
37-
it "returns the underscored_resource_name camelized" do
38-
config.should_receive(:underscored_resource_name).and_return "my_resource"
33+
it "returns the resource_name without spaces" do
34+
config.should_receive(:resource_name).and_return "My Resource"
3935
config.camelized_resource_name.should == "MyResource"
4036
end
4137
end
4238

39+
describe "#underscored_resource_name" do
40+
it "returns the camelized_resource_name underscored" do
41+
config.should_receive(:camelized_resource_name).and_return "MyResource"
42+
config.underscored_resource_name.should == "my_resource"
43+
end
44+
end
45+
4346
describe "#plural_underscored_resource_name" do
44-
it "returns the plural_resource_name underscored" do
45-
config.should_receive(:plural_resource_name).and_return "My Resources"
47+
it "returns the plural_camelized_resource_name underscored" do
48+
config.should_receive(:plural_camelized_resource_name).and_return "MyResources"
4649
config.plural_underscored_resource_name.should == "my_resources"
4750
end
4851
end

spec/unit/resource/naming_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@ module ::Mock; class Resource; end; end
2626
end
2727
end
2828
context "when you pass the 'as' option" do
29-
it "should underscore the passed through string and singulralize" do
30-
config(:as => "Blog Categories").underscored_resource_name.should == "blog_category"
29+
it "should underscore the passed through string" do
30+
config(:as => "Blog Category").underscored_resource_name.should == "blog_category"
3131
end
3232
end
3333
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 Categories").camelized_resource_name.should == "BlogCategory"
37+
config(:as => "Blog Category").camelized_resource_name.should == "BlogCategory"
3838
end
3939
end
4040

0 commit comments

Comments
 (0)