Skip to content

Commit e1da6c0

Browse files
committed
Merge pull request activeadmin#1831 from goshakkk/refactor
Some refactoring
2 parents 06ee4fe + f8b067e commit e1da6c0

File tree

3 files changed

+74
-68
lines changed

3 files changed

+74
-68
lines changed

lib/active_admin/helpers/optional_display.rb

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module ActiveAdmin
2-
2+
33
# Shareable module to give a #display_on?(action) method
44
# which returns true or false depending on an options hash.
55
#
@@ -23,7 +23,7 @@ def display_on?(action, render_context = nil)
2323
when Symbol, String
2424
render_context ? render_context.send(symbol_or_proc) : self.send(symbol_or_proc)
2525
when Proc
26-
render_context ? render_context.instance_exec(&symbol_or_proc) : instance_exec(&symbol_or_proc)
26+
render_context ? render_context.instance_exec(&symbol_or_proc) : instance_exec(&symbol_or_proc)
2727
else symbol_or_proc
2828
end
2929
end
@@ -33,12 +33,8 @@ def display_on?(action, render_context = nil)
3333
private
3434

3535
def normalize_display_options!
36-
if @options[:only]
37-
@options[:only] = @options[:only].is_a?(Array) ? @options[:only] : [@options[:only]]
38-
end
39-
if @options[:except]
40-
@options[:except] = @options[:except].is_a?(Array) ? @options[:except] : [@options[:except]]
41-
end
36+
@options[:only] = Array(@options[:only]) if @options[:only]
37+
@options[:except] = Array(@options[:except]) if @options[:except]
4238
end
4339
end
4440
end

lib/active_admin/resource_dsl.rb

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def belongs_to(target, options = {})
1818
# end
1919
#
2020
# Then every time we instantiate and object, it would call
21-
#
21+
#
2222
# current_user.posts.build
2323
#
2424
# By default Active Admin will use the resource name to build a
@@ -28,7 +28,7 @@ def belongs_to(target, options = {})
2828
# scope_to :current_user, :association_method => :blog_posts
2929
#
3030
# will result in the following
31-
#
31+
#
3232
# current_user.blog_posts.build
3333
#
3434
def scope_to(*args, &block)
@@ -94,9 +94,9 @@ def csv(options={}, &block)
9494
#
9595
# You can treat everything within the block as a standard Rails controller
9696
# action.
97-
#
98-
def member_action(name, options = {}, &block)
99-
config.member_actions << ControllerAction.new(name, options)
97+
#
98+
def action(set, name, options = {}, &block)
99+
set << ControllerAction.new(name, options)
100100
title = options.delete(:title)
101101

102102
controller do
@@ -105,14 +105,12 @@ def member_action(name, options = {}, &block)
105105
end
106106
end
107107

108-
def collection_action(name, options = {}, &block)
109-
config.collection_actions << ControllerAction.new(name, options)
110-
title = options.delete(:title)
108+
def member_action(name, options = {}, &block)
109+
action config.member_actions, name, options, &block
110+
end
111111

112-
controller do
113-
before_filter(:only => [name]){ @page_title = title } if title
114-
define_method(name, &block || Proc.new{})
115-
end
112+
def collection_action(name, options = {}, &block)
113+
action config.collection_actions, name, options, &block
116114
end
117115

118116
# Defined Callbacks
@@ -151,7 +149,7 @@ def collection_action(name, options = {}, &block)
151149
# Specify which actions to create in the controller
152150
#
153151
# Eg:
154-
#
152+
#
155153
# ActiveAdmin.register Post do
156154
# actions :index, :show
157155
# end

lib/active_admin/router.rb

Lines changed: 59 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
module ActiveAdmin
22
class Router
3-
43
def initialize(application)
54
@application = application
65
end
@@ -14,63 +13,42 @@ def initialize(application)
1413
# end
1514
#
1615
def apply(router)
17-
# Define any necessary dashboard routes and root
18-
router.instance_exec(@application.namespaces.values) do |namespaces|
19-
namespaces.each do |namespace|
20-
root_and_dashboard_routes = Proc.new do
21-
root :to => (namespace.root_to || "dashboard#index")
22-
if ActiveAdmin::Dashboards.built?
23-
match '/dashboard' => 'dashboard#index', :as => 'dashboard'
24-
end
25-
end
16+
define_basic_routes router
17+
define_resource_routes router
18+
end
2619

20+
# Define any necessary dashboard routes and root
21+
def define_basic_routes(router)
22+
router.instance_exec(@application.namespaces.values, self) do |namespaces, aa_router|
23+
namespaces.each do |namespace|
2724
if namespace.root?
28-
instance_eval &root_and_dashboard_routes
25+
instance_eval &aa_router.root_and_dashboard_routes(namespace)
2926
else
3027
namespace(namespace.name) do
31-
instance_eval &root_and_dashboard_routes
28+
instance_eval &aa_router.root_and_dashboard_routes(namespace)
3229
end
3330
end
3431
end
3532
end
33+
end
3634

37-
# Now define the routes for each resource
38-
router.instance_exec(@application.namespaces) do |namespaces|
39-
resources = namespaces.values.collect{|n| n.resources.resources }.flatten
40-
resources.each do |config|
41-
42-
# Define the block the will get eval'd within the namespace
43-
route_definition_block = Proc.new do
44-
case config
45-
when Resource
46-
resources config.resource_name.route_key, :only => config.defined_actions do
47-
# Define any member actions
48-
member do
49-
config.member_actions.each do |action|
50-
# eg: get :comment
51-
send(action.http_verb, action.name)
52-
end
53-
end
54-
55-
# Define any collection actions
56-
collection do
57-
config.collection_actions.each do |action|
58-
send(action.http_verb, action.name)
59-
end
35+
def root_and_dashboard_routes(namespace)
36+
Proc.new do
37+
root :to => (namespace.root_to || "dashboard#index")
38+
if ActiveAdmin::Dashboards.built?
39+
match '/dashboard' => 'dashboard#index', :as => 'dashboard'
40+
end
41+
end
42+
end
6043

61-
post :batch_action
62-
end
63-
end
64-
when Page
44+
# Define the routes for each resource
45+
def define_resource_routes(router)
46+
resource_routes = method(:resource_routes)
6547

66-
match "/#{config.underscored_resource_name}" => "#{config.underscored_resource_name}#index"
67-
config.page_actions.each do |action|
68-
match "/#{config.underscored_resource_name}/#{action.name}" => "#{config.underscored_resource_name}##{action.name}", :via => action.http_verb
69-
end
70-
else
71-
raise "Unsupported config class: #{config.class}"
72-
end
73-
end
48+
router.instance_exec(@application.namespaces, self) do |namespaces, aa_router|
49+
resources = namespaces.values.collect{|n| n.resources.resources }.flatten
50+
resources.each do |config|
51+
route_definition_block = aa_router.resource_routes(config)
7452

7553
# Add in the parent if it exists
7654
if config.belongs_to?
@@ -110,5 +88,39 @@ def apply(router)
11088
end
11189
end
11290
end
91+
92+
def resource_routes(config)
93+
Proc.new do
94+
case config
95+
when Resource
96+
resources config.resource_name.route_key, :only => config.defined_actions do
97+
# Define any member actions
98+
member do
99+
config.member_actions.each do |action|
100+
# eg: get :comment
101+
send(action.http_verb, action.name)
102+
end
103+
end
104+
105+
# Define any collection actions
106+
collection do
107+
config.collection_actions.each do |action|
108+
send(action.http_verb, action.name)
109+
end
110+
111+
post :batch_action
112+
end
113+
end
114+
when Page
115+
match "/#{config.underscored_resource_name}" => "#{config.underscored_resource_name}#index"
116+
config.page_actions.each do |action|
117+
match "/#{config.underscored_resource_name}/#{action.name}" => "#{config.underscored_resource_name}##{action.name}", :via => action.http_verb
118+
end
119+
else
120+
raise "Unsupported config class: #{config.class}"
121+
end
122+
end
123+
124+
end
113125
end
114126
end

0 commit comments

Comments
 (0)