@@ -6,7 +6,7 @@ class Mapper
6
6
# Internal helper to take extra_actions hash and convert to a consistent format.
7
7
protected def _parse_extra_actions ( extra_actions )
8
8
return ( extra_actions || { } ) . map do |k , v |
9
- kwargs = { }
9
+ kwargs = { action : k }
10
10
path = k
11
11
12
12
# Convert structure to path/methods/kwargs.
@@ -25,11 +25,6 @@ class Mapper
25
25
path = v . delete ( :path )
26
26
end
27
27
28
- # Set the action to be the action key unless it's already defined.
29
- if !kwargs [ :action ]
30
- kwargs [ :action ] = k
31
- end
32
-
33
28
# Pass any further kwargs to the underlying Rails interface.
34
29
kwargs = kwargs . merge ( v )
35
30
elsif v . is_a? ( Symbol ) || v . is_a? ( String )
@@ -77,6 +72,16 @@ class Mapper
77
72
return controller
78
73
end
79
74
75
+ # Interal interface for routing extra actions.
76
+ protected def _route_extra_actions ( actions , &block )
77
+ actions . each do |action_config |
78
+ action_config [ :methods ] . each do |m |
79
+ public_send ( m , action_config [ :path ] , **action_config [ :kwargs ] )
80
+ end
81
+ yield if block_given?
82
+ end
83
+ end
84
+
80
85
# Internal core implementation of the `rest_resource(s)` router, both singular and plural.
81
86
# @param default_singular [Boolean] the default plurality of the resource if the plurality is
82
87
# not otherwise defined by the controller
@@ -105,28 +110,20 @@ class Mapper
105
110
end
106
111
resource_method = singular ? :resource : :resources
107
112
108
- # call either `resource` or `resources`, passing appropriate modifiers
113
+ # Call either `resource` or `resources`, passing appropriate modifiers.
109
114
skip_undefined = kwargs . delete ( :skip_undefined ) || true
110
115
skip = controller_class . get_skip_actions ( skip_undefined : skip_undefined )
111
116
public_send ( resource_method , name , except : skip , **kwargs ) do
112
117
if controller_class . respond_to? ( :extra_member_actions )
113
118
member do
114
119
actions = self . _parse_extra_actions ( controller_class . extra_member_actions )
115
- actions . each do |action_config |
116
- action_config [ :methods ] . each do |m |
117
- public_send ( m , action_config [ :path ] , **action_config [ :kwargs ] )
118
- end
119
- end
120
+ _route_extra_actions ( actions )
120
121
end
121
122
end
122
123
123
124
collection do
124
125
actions = self . _parse_extra_actions ( controller_class . extra_actions )
125
- actions . each do |action_config |
126
- action_config [ :methods ] . each do |m |
127
- public_send ( m , action_config [ :path ] , **action_config [ :kwargs ] )
128
- end
129
- end
126
+ _route_extra_actions ( actions )
130
127
end
131
128
132
129
yield if block_given?
@@ -150,6 +147,7 @@ def rest_resources(*names, **kwargs, &block)
150
147
# Route a controller without the default resourceful paths.
151
148
def rest_route ( name = nil , **kwargs , &block )
152
149
controller = kwargs . delete ( :controller ) || name
150
+ route_root_to = kwargs . delete ( :route_root_to )
153
151
if controller . is_a? ( Class )
154
152
controller_class = controller
155
153
else
@@ -162,42 +160,27 @@ def rest_route(name=nil, **kwargs, &block)
162
160
# Route actions using the resourceful router, but skip all builtin actions.
163
161
actions = self . _parse_extra_actions ( controller_class . extra_actions )
164
162
public_send ( :resource , name , only : [ ] , **kwargs ) do
165
- actions . each do |action_config |
166
- action_config [ :methods ] . each do |m |
167
- public_send ( m , action_config [ :path ] , **action_config [ :kwargs ] )
168
- end
169
- yield if block_given?
163
+ # Route a root for this resource.
164
+ if route_root_to
165
+ get '' , action : route_root_to
170
166
end
167
+
168
+ _route_extra_actions ( actions , &block )
171
169
end
172
170
end
173
171
174
172
# Route a controller's `#root` to '/' in the current scope/namespace, along with other actions.
175
- # @param name [Symbol] the snake_case name of the controller
176
173
def rest_root ( name = nil , **kwargs , &block )
177
174
# By default, use RootController#root.
178
175
root_action = kwargs . delete ( :action ) || :root
179
176
controller = kwargs . delete ( :controller ) || name || :root
180
177
181
- # Route the root.
182
- get name . to_s , controller : controller , action : root_action
183
-
184
- # Route any additional actions.
185
- controller_class = self . _get_controller_class ( controller , pluralize : false )
186
- actions = self . _parse_extra_actions ( controller_class . extra_actions )
187
- actions . each do |action_config |
188
- # Add :action unless kwargs defines it.
189
- unless action_config [ :kwargs ] . key? ( :action )
190
- action_config [ :kwargs ] [ :action ] = action_config [ :path ]
191
- end
178
+ # Remove path if name is nil (routing to the root of current namespace).
179
+ unless name
180
+ kwargs [ :path ] = ''
181
+ end
192
182
193
- action_config [ :methods ] . each do |m |
194
- public_send (
195
- m ,
196
- File . join ( name . to_s , action_config [ :path ] . to_s ) ,
197
- controller : controller ,
198
- **action_config [ :kwargs ] ,
199
- )
200
- end
183
+ return rest_route ( controller , route_root_to : root_action , **kwargs ) do
201
184
yield if block_given?
202
185
end
203
186
end
0 commit comments