11module 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
114126end
0 commit comments