Skip to content

Commit dbebac9

Browse files
pcreuxgregbell
authored andcommitted
Refactor ActiveAdmin Controller hierarchy.
1 parent cf9d6a7 commit dbebac9

File tree

18 files changed

+93
-149
lines changed

18 files changed

+93
-149
lines changed

features/registering_pages.feature

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ Feature: Registering Pages
1010
"""
1111
ActiveAdmin.page "Status" do
1212
content do
13-
h1 "Current Status"
13+
para "I love chocolate."
1414
end
1515
end
1616
"""
1717
When I go to the dashboard
18-
Then I should see "Status"
19-
When I follow "Status"
20-
Then I should see "Current Status"
18+
And I follow "Status"
19+
Then I should see the page title "Status"
20+
Then I should see "I love chocolate."
2121

lib/active_admin.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ module ActiveAdmin
1616
autoload :Breadcrumbs, 'active_admin/breadcrumbs'
1717
autoload :Callbacks, 'active_admin/callbacks'
1818
autoload :Component, 'active_admin/component'
19+
autoload :BaseController, 'active_admin/base_controller'
1920
autoload :ControllerAction, 'active_admin/controller_action'
2021
autoload :CSVBuilder, 'active_admin/csv_builder'
2122
autoload :Dashboards, 'active_admin/dashboards'
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
require 'inherited_resources'
2+
3+
module ActiveAdmin
4+
class BaseController < ::InheritedResources::Base
5+
helper ::ActiveAdmin::ViewHelpers
6+
7+
layout 'active_admin'
8+
9+
before_filter :only_render_implemented_actions
10+
before_filter :authenticate_active_admin_user
11+
12+
class << self
13+
# Ensure that this method is available for the DSL
14+
public :actions
15+
16+
# Reference to the Resource object which initialized
17+
# this controller
18+
attr_accessor :active_admin_config
19+
end
20+
21+
# By default Rails will render un-implemented actions when the view exists. Becuase Active
22+
# Admin allows you to not render any of the actions by using the #actions method, we need
23+
# to check if they are implemented.
24+
def only_render_implemented_actions
25+
raise AbstractController::ActionNotFound unless action_methods.include?(params[:action])
26+
end
27+
28+
29+
private
30+
31+
# Calls the authentication method as defined in ActiveAdmin.authentication_method
32+
def authenticate_active_admin_user
33+
send(active_admin_namespace.authentication_method) if active_admin_namespace.authentication_method
34+
end
35+
36+
def current_active_admin_user
37+
send(active_admin_namespace.current_user_method) if active_admin_namespace.current_user_method
38+
end
39+
helper_method :current_active_admin_user
40+
41+
def current_active_admin_user?
42+
!current_active_admin_user.nil?
43+
end
44+
helper_method :current_active_admin_user?
45+
46+
def active_admin_config
47+
self.class.active_admin_config
48+
end
49+
helper_method :active_admin_config
50+
51+
def active_admin_namespace
52+
active_admin_config.namespace
53+
end
54+
helper_method :active_admin_namespace
55+
56+
end
57+
end

lib/active_admin/dashboards/dashboard_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def index
99
render 'active_admin/dashboard/index.html.arb'
1010
end
1111

12-
protected
12+
private
1313

1414
def set_current_tab
1515
@current_tab = I18n.t("active_admin.dashboard")

lib/active_admin/page.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,18 @@ def route_collection_path
6161
def comments?
6262
false
6363
end
64+
65+
# Overwrite Resource::ActionItems
66+
def action_items_for(action)
67+
[]
68+
end
69+
70+
def skip_sidebar?
71+
true
72+
end
73+
74+
def sidebar_sections_for(action)
75+
[]
76+
end
6477
end
6578
end
Lines changed: 3 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,17 @@
11
module ActiveAdmin
22
# Note: I could try to just inherit from ResourceController
3-
class PageController < ::InheritedResources::Base
3+
class PageController < BaseController
44

5-
helper ::ActiveAdmin::ViewHelpers
5+
layout false # Page page is a subclass of Base page.
66

7-
layout :determine_active_admin_layout
7+
actions :index
88

9-
respond_to :html
10-
11-
before_filter :only_render_implemented_actions
12-
before_filter :authenticate_active_admin_user
13-
14-
ACTIVE_ADMIN_ACTIONS = [:index]
15-
16-
include ActiveAdmin::Resource::ActionItems
179
include ActiveAdmin::ResourceController::PageConfigurations
1810
include ActiveAdmin::ResourceController::Menu
1911

2012
def index(options={}, &block)
2113
arbre_block = index_config.block
2214
render "active_admin/page/index"
2315
end
24-
25-
class << self
26-
# Ensure that this method is available for the DSL
27-
public :actions
28-
29-
# Reference to the Resource object which initialized
30-
# this controller
31-
attr_accessor :active_admin_config
32-
33-
def active_admin_config=(config)
34-
@active_admin_config = config
35-
#defaults :resource_class => config.resource,
36-
# :route_prefix => config.route_prefix,
37-
# :instance_name => config.underscored_resource_name
38-
end
39-
40-
end
41-
42-
protected
43-
44-
# By default Rails will render un-implemented actions when the view exists. Becuase Active
45-
# Admin allows you to not render any of the actions by using the #actions method, we need
46-
# to check if they are implemented.
47-
def only_render_implemented_actions
48-
raise AbstractController::ActionNotFound unless action_methods.include?(params[:action])
49-
end
50-
51-
# Determine which layout to use.
52-
#
53-
# 1. If we're rendering a standard Active Admin action, we want layout(false)
54-
# because these actions are subclasses of the Base page (which implementes
55-
# all the required layout code)
56-
# 2. If we're rendering a custom action, we'll use the active_admin layout so
57-
# that users can render any template inside Active Admin.
58-
def determine_active_admin_layout
59-
ACTIVE_ADMIN_ACTIONS.include?(params[:action].to_sym) ? false : 'active_admin'
60-
end
61-
62-
# Calls the authentication method as defined in ActiveAdmin.authentication_method
63-
def authenticate_active_admin_user
64-
send(active_admin_application.authentication_method) if active_admin_application.authentication_method
65-
end
66-
67-
def current_active_admin_user
68-
send(active_admin_application.current_user_method) if active_admin_application.current_user_method
69-
end
70-
helper_method :current_active_admin_user
71-
72-
def current_active_admin_user?
73-
!current_active_admin_user.nil?
74-
end
75-
helper_method :current_active_admin_user?
76-
77-
def active_admin_config
78-
self.class.active_admin_config
79-
end
80-
helper_method :active_admin_config
81-
82-
def active_admin_application
83-
ActiveAdmin.application
84-
end
8516
end
8617
end

lib/active_admin/resource_controller.rb

Lines changed: 3 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,12 @@
1010
require 'active_admin/resource_controller/scoping'
1111

1212
module ActiveAdmin
13-
class ResourceController < ::InheritedResources::Base
14-
15-
helper ::ActiveAdmin::ViewHelpers
16-
13+
class ResourceController < BaseController
1714
layout :determine_active_admin_layout
1815

1916
respond_to :html, :xml, :json
2017
respond_to :csv, :only => :index
2118

22-
before_filter :only_render_implemented_actions
23-
before_filter :authenticate_active_admin_user
24-
2519
ACTIVE_ADMIN_ACTIONS = [:index, :show, :new, :create, :edit, :update, :destroy]
2620

2721
include Actions
@@ -35,13 +29,6 @@ class ResourceController < ::InheritedResources::Base
3529
include Scoping
3630

3731
class << self
38-
# Ensure that this method is available for the DSL
39-
public :actions
40-
41-
# Reference to the Resource object which initialized
42-
# this controller
43-
attr_accessor :active_admin_config
44-
4532
def active_admin_config=(config)
4633
@active_admin_config = config
4734
defaults :resource_class => config.resource,
@@ -52,14 +39,7 @@ def active_admin_config=(config)
5239
public :belongs_to
5340
end
5441

55-
protected
56-
57-
# By default Rails will render un-implemented actions when the view exists. Becuase Active
58-
# Admin allows you to not render any of the actions by using the #actions method, we need
59-
# to check if they are implemented.
60-
def only_render_implemented_actions
61-
raise AbstractController::ActionNotFound unless action_methods.include?(params[:action])
62-
end
42+
private
6343

6444
# Determine which layout to use.
6545
#
@@ -72,39 +52,11 @@ def determine_active_admin_layout
7252
ACTIVE_ADMIN_ACTIONS.include?(params[:action].to_sym) ? false : 'active_admin'
7353
end
7454

75-
# Calls the authentication method as defined in ActiveAdmin.authentication_method
76-
def authenticate_active_admin_user
77-
send(active_admin_namespace.authentication_method) if active_admin_namespace.authentication_method
78-
end
79-
80-
def current_active_admin_user
81-
send(active_admin_namespace.current_user_method) if active_admin_namespace.current_user_method
82-
end
83-
helper_method :current_active_admin_user
84-
85-
def current_active_admin_user?
86-
!current_active_admin_user.nil?
87-
end
88-
helper_method :current_active_admin_user?
89-
90-
def active_admin_config
91-
self.class.active_admin_config
92-
end
93-
helper_method :active_admin_config
94-
95-
def active_admin_namespace
96-
active_admin_config.namespace
97-
end
98-
helper_method :active_admin_namespace
99-
100-
def active_admin_namespace
101-
active_admin_config.namespace
102-
end
103-
10455
# Returns the renderer class to use for the given action.
10556
def renderer_for(action)
10657
active_admin_namespace.view_factory["#{action}_page"]
10758
end
10859
helper_method :renderer_for
60+
10961
end
11062
end

lib/active_admin/resource_controller/action_builder.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module ActiveAdmin
2-
class ResourceController < ::InheritedResources::Base
2+
class ResourceController < BaseController
33

44
module ActionBuilder
55
extend ActiveSupport::Concern

lib/active_admin/resource_controller/actions.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module ActiveAdmin
2-
class ResourceController < ::InheritedResources::Base
2+
class ResourceController < BaseController
33

44
# Override the InheritedResources actions to use the
55
# Active Admin templates.

lib/active_admin/resource_controller/callbacks.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module ActiveAdmin
2-
class ResourceController < ::InheritedResources::Base
2+
class ResourceController < BaseController
33

44
module Callbacks
55
extend ActiveSupport::Concern

0 commit comments

Comments
 (0)