Skip to content

Commit 09edb7d

Browse files
pcreuxgregbell
authored andcommitted
Move shared behavior to Config.
1 parent 35cc040 commit 09edb7d

File tree

9 files changed

+93
-60
lines changed

9 files changed

+93
-60
lines changed

lib/active_admin/config.rb

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
1+
module ActiveAdmin
2+
class Config; end
3+
end
4+
5+
require 'active_admin/config/naming'
6+
require 'active_admin/config/menu'
7+
18
module ActiveAdmin
29
class Config
310
# Returns a properly formatted controller name for this
411
# config within its namespace
512
def controller_name
6-
[namespace.module_name, camelized_resource_name + "Controller"].compact.join('::')
13+
[namespace.module_name, plural_underscored_resource_name.camelize + "Controller"].compact.join('::')
714
end
815

916
# Returns the controller for this config
@@ -30,5 +37,23 @@ def route_collection_path
3037
route.compact.join('_').to_sym
3138
end
3239

40+
include Menu
41+
include Naming
42+
43+
def belongs_to?
44+
false
45+
end
46+
47+
def comments?
48+
false
49+
end
50+
51+
def action_items?
52+
!!@action_items && @action_items.any?
53+
end
54+
55+
def sidebar_sections?
56+
!!@sidebar_sections && @sidebar_sections.any?
57+
end
3358
end
3459
end

lib/active_admin/resource/menu.rb renamed to lib/active_admin/config/menu.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module ActiveAdmin
2-
class Resource < Config
2+
class Config
33
module Menu
44

55
# Set the menu options. To not add this resource to the menu, just

lib/active_admin/config/naming.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module ActiveAdmin
2+
class Config
3+
module Naming
4+
# A camelized safe representation for this resource
5+
def camelized_resource_name
6+
underscored_resource_name.camelize
7+
end
8+
9+
# Returns the plural and underscored version of this resource. Useful for element id's.
10+
def plural_underscored_resource_name
11+
plural_resource_name.underscore.gsub(/\s/, '_')
12+
end
13+
end
14+
end
15+
end

lib/active_admin/page.rb

Lines changed: 9 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,48 +9,21 @@ def initialize(namespace, name, options)
99
@page_configs = {}
1010
end
1111

12-
# Returns a properly formatted controller name for this
13-
# resource within its namespace
14-
def controller_name
15-
[namespace.module_name, camelized_resource_name + "Controller"].compact.join('::')
16-
end
17-
18-
# For Menu#include_in_menu?
19-
def belongs_to?
20-
false
21-
end
22-
23-
# For Menu#menu_item_name
12+
# plural_resource_name is singular
2413
def plural_resource_name
2514
name
2615
end
2716

28-
include ActiveAdmin::Resource::Menu
29-
30-
# ############## Naming ###################
31-
def underscored_resource_name
32-
name.gsub(' ', '').underscore
33-
end
34-
35-
# From Naming.
36-
def camelized_resource_name
37-
underscored_resource_name.camelize
38-
end
39-
# ############## Naming ###################
40-
41-
# ############# From Resource.
42-
def comments?
43-
false
44-
end
45-
46-
# From Resource::ActionItems
47-
def action_items_for(action)
48-
[]
17+
# Overwrite Naming defaults as they require a resource object.
18+
# @todo Refactor Naming to remove the dependency with Resources
19+
def resource_name
20+
name
4921
end
5022

51-
# From Resource::Sidebars
52-
def sidebar_sections_for(action)
53-
[]
23+
# Overwrite Naming defaults as they require a resource object.
24+
# @todo Refactor Naming to remove the dependency with Resources
25+
def underscored_resource_name
26+
resource_name.underscore
5427
end
5528
end
5629
end

lib/active_admin/resource.rb

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
require 'active_admin/resource/action_items'
2-
require 'active_admin/resource/menu'
32
require 'active_admin/resource/naming'
43
require 'active_admin/resource/scopes'
54
require 'active_admin/resource/sidebars'
@@ -65,7 +64,6 @@ def initialize(namespace, resource, options = {})
6564

6665
include Base
6766
include ActionItems
68-
include Menu
6967
include Naming
7068
include Scopes
7169
include Sidebars
@@ -75,11 +73,6 @@ def resource_table_name
7573
resource.quoted_table_name
7674
end
7775

78-
# Returns a properly formatted controller name for this
79-
# resource within its namespace
80-
def controller_name
81-
[namespace.module_name, camelized_resource_name.pluralize + "Controller"].compact.join('::')
82-
end
8376

8477
# Returns the named route for an instance of this resource
8578
def route_instance_path

lib/active_admin/resource/naming.rb

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
module ActiveAdmin
22
class Resource < Config
33
module Naming
4-
54
# An underscored safe representation internally for this resource
65
def underscored_resource_name
76
@underscored_resource_name ||= if @options[:as]
@@ -11,11 +10,6 @@ def underscored_resource_name
1110
end
1211
end
1312

14-
# A camelized safe representation for this resource
15-
def camelized_resource_name
16-
underscored_resource_name.camelize
17-
end
18-
1913
# Returns the name to call this resource.
2014
# By default will use resource.model_name.human
2115
def resource_name
@@ -40,11 +34,6 @@ def plural_resource_name
4034
end
4135
end
4236
end
43-
44-
# Returns the plural and underscored version of this resource. Useful for element id's.
45-
def plural_underscored_resource_name
46-
plural_resource_name.underscore.gsub(/\s/, '_')
47-
end
4837
end
4938
end
5039
end

lib/active_admin/views/pages/base.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def build_title_tag
8686
end
8787

8888
def build_action_items
89-
if active_admin_config
89+
if active_admin_config && active_admin_config.action_items?
9090
items = active_admin_config.action_items_for(params[:action])
9191
insert_tag view_factory.action_items, items
9292
end
@@ -134,7 +134,7 @@ def set_page_title
134134

135135
# Returns the sidebar sections to render for the current action
136136
def sidebar_sections_for_action
137-
if active_admin_config
137+
if active_admin_config && active_admin_config.sidebar_sections?
138138
active_admin_config.sidebar_sections_for(params[:action])
139139
else
140140
[]

spec/unit/config_shared_examples.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@
55
end
66
end
77

8-
it { respond_to :belongs_to? }
9-
108
it { respond_to :controller_name }
119
it { respond_to :controller }
1210
it { respond_to :route_prefix }
1311
it { respond_to :route_collection_path }
12+
it { respond_to :comments? }
13+
it { respond_to :belongs_to? }
14+
it { respond_to :comments? }
15+
it { respond_to :action_items? }
16+
it { respond_to :sidebar_sections? }
1417

1518
end

spec/unit/page_spec.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,40 @@ def config(options = {})
2525
end
2626
end
2727
end
28+
29+
describe "#resource_name" do
30+
it "returns the name" do
31+
config.resource_name.should == "Status"
32+
end
33+
end
34+
35+
describe "#plural_resource_name" do
36+
it "returns the singular name" do
37+
config.plural_resource_name.should == "Status"
38+
end
39+
end
40+
41+
describe "#underscored_resource_name" do
42+
it "returns the underscored name" do
43+
config.underscored_resource_name.should == "status"
44+
end
45+
end
46+
47+
it "should not belong_to anything" do
48+
config.belongs_to?.should == false
49+
end
50+
51+
it "should not have comments" do
52+
config.comments?.should == false
53+
end
54+
55+
it "should not have any action_items" do
56+
config.action_items?.should == false
57+
end
58+
59+
it "should not have any sidebar_sections" do
60+
config.sidebar_sections?.should == false
61+
end
62+
2863
end
2964
end

0 commit comments

Comments
 (0)