activeadmin 0.5.1 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of activeadmin might be problematic. Click here for more details.

Files changed (196) hide show
  1. data/.travis.yml +8 -5
  2. data/CHANGELOG.md +151 -0
  3. data/Gemfile +16 -16
  4. data/LICENSE +1 -1
  5. data/activeadmin.gemspec +1 -0
  6. data/app/assets/images/active_admin/index_list_icons/block_icon.svg +10 -0
  7. data/app/assets/images/active_admin/index_list_icons/blog_icon.svg +4 -0
  8. data/app/assets/images/active_admin/index_list_icons/grid_icon.svg +13 -0
  9. data/app/assets/images/active_admin/index_list_icons/table_icon.svg +3 -0
  10. data/app/assets/javascripts/active_admin/components/jquery.aa.checkbox-toggler.js.coffee +17 -44
  11. data/app/assets/javascripts/active_admin/components/jquery.aa.dropdown-menu.js.coffee +2 -4
  12. data/app/assets/javascripts/active_admin/components/jquery.aa.popover.js.coffee +0 -3
  13. data/app/assets/javascripts/active_admin/components/jquery.aa.table-checkbox-toggler.js.coffee +10 -12
  14. data/app/assets/javascripts/active_admin/pages/application.js.coffee +13 -10
  15. data/app/assets/javascripts/active_admin/pages/batch_actions.js.coffee +3 -3
  16. data/app/assets/stylesheets/active_admin/_base.css.scss +1 -0
  17. data/app/assets/stylesheets/active_admin/_header.css.scss +5 -2
  18. data/app/assets/stylesheets/active_admin/components/_dropdown_menu.scss +2 -2
  19. data/app/assets/stylesheets/active_admin/components/_index_list.scss +12 -0
  20. data/app/assets/stylesheets/active_admin/components/_popovers.css.scss +2 -2
  21. data/app/assets/stylesheets/active_admin/components/_table_tools.css.scss +2 -2
  22. data/app/assets/stylesheets/active_admin/mixins/_buttons.css.scss +4 -4
  23. data/app/assets/stylesheets/active_admin/mixins/_gradients.css.scss +5 -4
  24. data/app/assets/stylesheets/active_admin/mixins/_sections.css.scss +1 -1
  25. data/app/assets/stylesheets/active_admin/pages/_logged_out.scss +1 -1
  26. data/app/assets/stylesheets/active_admin/print.css.scss +2 -2
  27. data/app/assets/stylesheets/active_admin/structure/_title_bar.scss +1 -1
  28. data/app/views/active_admin/devise/unlocks/new.html.erb +14 -12
  29. data/docs/1-general-configuration.md +13 -0
  30. data/docs/10-custom-pages.md +40 -28
  31. data/docs/11-decorators.md +6 -4
  32. data/docs/12-arbre-components.md +1 -1
  33. data/docs/13-authorization-adapter.md +206 -0
  34. data/docs/2-resource-customization.md +129 -14
  35. data/docs/3-index-pages.md +65 -0
  36. data/docs/3-index-pages/create-an-index.md +29 -0
  37. data/docs/3-index-pages/index-as-table.md +35 -11
  38. data/docs/5-forms.md +2 -2
  39. data/features/authorization.feature +64 -0
  40. data/features/authorization_cancan.feature +52 -0
  41. data/features/belongs_to.feature +41 -5
  42. data/features/index/batch_actions.feature +27 -3
  43. data/features/index/formats.feature +27 -0
  44. data/features/index/index_as_table.feature +74 -2
  45. data/features/index/index_parameters.feature +69 -0
  46. data/features/index/index_scopes.feature +7 -11
  47. data/features/index/page_title.feature +18 -0
  48. data/features/index/switch_index_view.feature +73 -0
  49. data/features/menu.feature +27 -0
  50. data/features/registering_pages.feature +21 -0
  51. data/features/step_definitions/additional_web_steps.rb +4 -0
  52. data/features/step_definitions/batch_action_steps.rb +16 -6
  53. data/features/step_definitions/index_scope_steps.rb +3 -3
  54. data/features/step_definitions/index_views_steps.rb +3 -0
  55. data/features/step_definitions/menu_steps.rb +4 -0
  56. data/features/support/env.rb +4 -0
  57. data/features/support/paths.rb +3 -0
  58. data/lib/active_admin.rb +4 -0
  59. data/lib/active_admin/application.rb +43 -76
  60. data/lib/active_admin/authorization_adapter.rb +128 -0
  61. data/lib/active_admin/base_controller.rb +18 -1
  62. data/lib/active_admin/base_controller/authorization.rb +153 -0
  63. data/lib/active_admin/base_controller/menu.rb +8 -3
  64. data/lib/active_admin/batch_actions/resource_extension.rb +8 -7
  65. data/lib/active_admin/batch_actions/views/batch_action_form.rb +1 -1
  66. data/lib/active_admin/cancan_adapter.rb +40 -0
  67. data/lib/active_admin/dashboards.rb +4 -5
  68. data/lib/active_admin/dashboards/dashboard_controller.rb +1 -1
  69. data/lib/active_admin/devise.rb +22 -7
  70. data/lib/active_admin/dsl.rb +18 -5
  71. data/lib/active_admin/engine.rb +1 -1
  72. data/lib/active_admin/filters/dsl.rb +1 -1
  73. data/lib/active_admin/filters/forms.rb +9 -7
  74. data/lib/active_admin/filters/resource_extension.rb +20 -15
  75. data/lib/active_admin/form_builder.rb +44 -49
  76. data/lib/active_admin/helpers/optional_display.rb +4 -8
  77. data/lib/active_admin/inputs/filter_boolean_input.rb +1 -1
  78. data/lib/active_admin/inputs/filter_string_input.rb +5 -1
  79. data/lib/active_admin/locales/bg.yml +1 -1
  80. data/lib/active_admin/locales/ca.yml +1 -1
  81. data/lib/active_admin/locales/cs.yml +26 -25
  82. data/lib/active_admin/locales/da.yml +1 -1
  83. data/lib/active_admin/locales/de.yml +1 -1
  84. data/lib/active_admin/locales/en.yml +11 -1
  85. data/lib/active_admin/locales/es.yml +16 -16
  86. data/lib/active_admin/locales/fr.yml +1 -1
  87. data/lib/active_admin/locales/he.yml +1 -1
  88. data/lib/active_admin/locales/hr.yml +1 -1
  89. data/lib/active_admin/locales/hu.yml +1 -1
  90. data/lib/active_admin/locales/it.yml +1 -1
  91. data/lib/active_admin/locales/ja.yml +4 -1
  92. data/lib/active_admin/locales/ko.yml +1 -1
  93. data/lib/active_admin/locales/lt.yml +1 -1
  94. data/lib/active_admin/locales/lv.yml +12 -1
  95. data/lib/active_admin/locales/nl.yml +2 -2
  96. data/lib/active_admin/locales/no-NB.yml +22 -1
  97. data/lib/active_admin/locales/pl.yml +1 -1
  98. data/lib/active_admin/locales/pt-BR.yml +1 -1
  99. data/lib/active_admin/locales/pt-PT.yml +81 -0
  100. data/lib/active_admin/locales/ro.yml +1 -1
  101. data/lib/active_admin/locales/ru.yml +1 -1
  102. data/lib/active_admin/locales/sv-SE.yml +1 -1
  103. data/lib/active_admin/locales/tr.yml +1 -1
  104. data/lib/active_admin/locales/vi.yml +1 -1
  105. data/lib/active_admin/locales/zh-CN.yml +1 -1
  106. data/lib/active_admin/locales/zh-TW.yml +1 -1
  107. data/lib/active_admin/menu.rb +76 -38
  108. data/lib/active_admin/menu_collection.rb +96 -0
  109. data/lib/active_admin/menu_item.rb +61 -71
  110. data/lib/active_admin/namespace.rb +67 -5
  111. data/lib/active_admin/page.rb +6 -0
  112. data/lib/active_admin/page_controller.rb +13 -3
  113. data/lib/active_admin/resource.rb +11 -20
  114. data/lib/active_admin/resource/action_items.rb +3 -3
  115. data/lib/active_admin/resource/controllers.rb +0 -17
  116. data/lib/active_admin/resource/menu.rb +52 -24
  117. data/lib/active_admin/resource/naming.rb +6 -10
  118. data/lib/active_admin/resource/page_presenters.rb +57 -3
  119. data/lib/active_admin/resource/routes.rb +46 -0
  120. data/lib/active_admin/resource_controller.rb +2 -17
  121. data/lib/active_admin/resource_controller/data_access.rb +299 -0
  122. data/lib/active_admin/resource_controller/decorators.rb +2 -1
  123. data/lib/active_admin/resource_dsl.rb +12 -14
  124. data/lib/active_admin/router.rb +61 -56
  125. data/lib/active_admin/scope.rb +28 -18
  126. data/lib/active_admin/version.rb +1 -1
  127. data/lib/active_admin/view_factory.rb +0 -1
  128. data/lib/active_admin/view_helpers/breadcrumb_helper.rb +13 -19
  129. data/lib/active_admin/view_helpers/fields_for.rb +2 -0
  130. data/lib/active_admin/view_helpers/form_helper.rb +1 -1
  131. data/lib/active_admin/view_helpers/method_or_proc_helper.rb +66 -11
  132. data/lib/active_admin/views/components/attributes_table.rb +7 -4
  133. data/lib/active_admin/views/components/index_list.rb +68 -0
  134. data/lib/active_admin/views/components/paginated_collection.rb +13 -9
  135. data/lib/active_admin/views/components/scopes.rb +2 -8
  136. data/lib/active_admin/views/components/table_for.rb +1 -1
  137. data/lib/active_admin/views/footer.rb +1 -1
  138. data/lib/active_admin/views/header.rb +3 -2
  139. data/lib/active_admin/views/index_as_block.rb +4 -0
  140. data/lib/active_admin/views/index_as_blog.rb +4 -0
  141. data/lib/active_admin/views/index_as_grid.rb +4 -0
  142. data/lib/active_admin/views/index_as_table.rb +49 -7
  143. data/lib/active_admin/views/pages/index.rb +19 -4
  144. data/lib/active_admin/views/tabbed_navigation.rb +10 -54
  145. data/lib/generators/active_admin/install/install_generator.rb +5 -1
  146. data/lib/generators/active_admin/install/templates/active_admin.rb.erb +58 -0
  147. data/spec/integration/{default_namespace.rb → default_namespace_spec.rb} +0 -16
  148. data/spec/spec_helper.rb +3 -0
  149. data/spec/support/detect_rails_version.rb +10 -3
  150. data/spec/support/rails_template.rb +1 -1
  151. data/spec/support/templates/post_decorator.rb +6 -6
  152. data/spec/unit/application_spec.rb +5 -5
  153. data/spec/unit/authorization/authorization_adapter_spec.rb +61 -0
  154. data/spec/unit/authorization/controller_authorization_spec.rb +47 -0
  155. data/spec/unit/base_controller_spec.rb +0 -2
  156. data/spec/unit/batch_actions/resource_spec.rb +1 -1
  157. data/spec/unit/breadcrumbs_spec.rb +3 -3
  158. data/spec/unit/cancan_adapter_spec.rb +35 -0
  159. data/spec/unit/config_shared_examples.rb +9 -30
  160. data/spec/unit/devise_spec.rb +29 -0
  161. data/spec/unit/dsl_spec.rb +61 -0
  162. data/spec/unit/filters/filter_form_builder_spec.rb +58 -4
  163. data/spec/unit/filters/resource_spec.rb +17 -5
  164. data/spec/unit/form_builder_spec.rb +15 -0
  165. data/spec/unit/menu_collection_spec.rb +62 -0
  166. data/spec/unit/menu_item_spec.rb +51 -37
  167. data/spec/unit/menu_spec.rb +33 -29
  168. data/spec/unit/namespace/authorization_spec.rb +31 -0
  169. data/spec/unit/namespace/register_page_spec.rb +6 -28
  170. data/spec/unit/namespace/register_resource_spec.rb +11 -34
  171. data/spec/unit/namespace_spec.rb +63 -2
  172. data/spec/unit/page_controller_spec.rb +0 -3
  173. data/spec/unit/reloader_spec.rb +5 -5
  174. data/spec/unit/resource/menu_spec.rb +1 -13
  175. data/spec/unit/resource/naming_spec.rb +20 -2
  176. data/spec/unit/resource/page_presenters_spec.rb +14 -2
  177. data/spec/unit/resource_controller/{collection_spec.rb → data_access_spec.rb} +31 -13
  178. data/spec/unit/resource_controller_spec.rb +4 -5
  179. data/spec/unit/resource_spec.rb +33 -11
  180. data/spec/unit/routing_spec.rb +43 -0
  181. data/spec/unit/scope_spec.rb +13 -0
  182. data/spec/unit/view_factory_spec.rb +0 -1
  183. data/spec/unit/view_helpers/fields_for_spec.rb +4 -0
  184. data/spec/unit/view_helpers/form_helper_spec.rb +6 -2
  185. data/spec/unit/view_helpers/method_or_proc_helper_spec.rb +97 -0
  186. data/spec/unit/views/components/attributes_table_spec.rb +11 -0
  187. data/spec/unit/views/components/index_list_spec.rb +32 -0
  188. data/spec/unit/views/tabbed_navigation_spec.rb +42 -40
  189. data/tasks/test.rake +1 -1
  190. metadata +127 -40
  191. data/lib/active_admin/menu_builder.rb +0 -65
  192. data/lib/active_admin/resource_controller/callbacks.rb +0 -59
  193. data/lib/active_admin/resource_controller/collection.rb +0 -158
  194. data/lib/active_admin/views/utility_nav.rb +0 -41
  195. data/spec/unit/base_controller_shared_examples.rb +0 -25
  196. data/spec/unit/dsl_include_spec.rb +0 -20
@@ -0,0 +1,299 @@
1
+ module ActiveAdmin
2
+ class ResourceController < BaseController
3
+
4
+ # This module overrides most of the data access methods in Inherited
5
+ # Resources to provide Active Admin with it's data.
6
+ #
7
+ # The module also deals with authorization and resource callbacks.
8
+ #
9
+ module DataAccess
10
+ extend ActiveSupport::Concern
11
+
12
+ include ActiveAdmin::Callbacks
13
+ include ActiveAdmin::ScopeChain
14
+
15
+ included do
16
+ define_active_admin_callbacks :build, :create, :update, :save, :destroy
17
+ end
18
+
19
+ protected
20
+
21
+ # Retrieve, memoize and authorize the current collection from the db. This
22
+ # method delegates the finding of the collection to #find_collection.
23
+ #
24
+ # Once #collection has been called, the collection is available using
25
+ # either the @collection instance variable or an instance variable named
26
+ # after the resource that the collection is for. eg: Post => @post.
27
+ #
28
+ # @returns [ActiveRecord::Relation] The collection for the index
29
+ def collection
30
+ _collection = get_collection_ivar
31
+
32
+ return _collection if _collection
33
+
34
+ _collection = find_collection
35
+ authorize! ActiveAdmin::Authorization::READ, active_admin_config.resource_class
36
+
37
+ set_collection_ivar _collection
38
+ end
39
+
40
+
41
+ # Does the actual work of retrieving the current collection from the db.
42
+ # This is a great method to override if you would like to perform
43
+ # some additional db # work before your controller returns and
44
+ # authorizes the collection.
45
+ #
46
+ # @returns [ActiveRecord::Relation] The collectin for the index
47
+ def find_collection
48
+ collection = scoped_collection
49
+
50
+ collection = apply_sorting(collection)
51
+ collection = apply_filtering(collection)
52
+ collection = apply_scoping(collection)
53
+ collection = apply_pagination(collection)
54
+ collection = apply_decorator(collection)
55
+
56
+ collection
57
+ end
58
+
59
+
60
+ # Override this method in your controllers to modify the start point
61
+ # of our searches and index.
62
+ #
63
+ # This method should return an ActiveRecord::Relation object so that
64
+ # the searching and filtering can be applied on top
65
+ #
66
+ # Note, unless you are doing something special, you should use the
67
+ # scope_to method from the Scoping module instead of overriding this
68
+ # method.
69
+ def scoped_collection
70
+ scope_for_authorization end_of_association_chain
71
+ end
72
+
73
+ # Retrieve, memoize and authorize a resource based on params[:id]. The
74
+ # actual work of finding the resource is done in #find_resource.
75
+ #
76
+ # This method is used on all the member actions:
77
+ #
78
+ # * show
79
+ # * edit
80
+ # * update
81
+ # * destroy
82
+ #
83
+ # @returns [ActiveRecord::Base] An active record object
84
+ def resource
85
+ _resource = get_resource_ivar
86
+
87
+ return _resource if _resource
88
+
89
+ _resource = find_resource
90
+ authorize_resource! _resource
91
+
92
+ if decorator?
93
+ _resource = decorator_class.new(_resource)
94
+ end
95
+
96
+ set_resource_ivar(_resource)
97
+ end
98
+
99
+ def decorator?
100
+ !!active_admin_config.decorator_class
101
+ end
102
+
103
+ def decorator_class
104
+ active_admin_config.decorator_class
105
+ end
106
+
107
+
108
+ # Does the actual work of finding a resource in the database. This
109
+ # method uses the finder method as defined in InheritedResources.
110
+ #
111
+ # @returns [ActiveRecord::Base] An active record object.
112
+ def find_resource
113
+ scoped_collection.send(method_for_find, params[:id])
114
+ end
115
+
116
+
117
+ # Builds, memoize and authorize a new instance of the resource. The
118
+ # actual work of building the new instance is delegated to the
119
+ # #build_new_resource method.
120
+ #
121
+ # This method is used to instantiate and authorize new resources in the
122
+ # new and create controller actions.
123
+ #
124
+ # @returns [ActiveRecord::Base] An un-saved active record base object
125
+ def build_resource
126
+ return resource if resource = get_resource_ivar
127
+
128
+ resource = build_new_resource
129
+
130
+ run_build_callbacks resource
131
+ authorize_resource! resource
132
+
133
+ set_resource_ivar(resource)
134
+ end
135
+
136
+ # Builds a new resource. This method uses the method_for_build provided
137
+ # by Inherited Resources.
138
+ #
139
+ # @returns [ActiveRecord::Base] An un-saved active record base object
140
+ def build_new_resource
141
+ scoped_collection.send(method_for_build, *resource_params)
142
+ end
143
+
144
+ # Calls all the appropriate callbacks and then creates the new resource.
145
+ #
146
+ # @param [ActiveRecord::Base] object The new resource to create
147
+ #
148
+ # @returns [void]
149
+ def create_resource(object)
150
+ run_create_callbacks object do
151
+ save_resource(object)
152
+ end
153
+ end
154
+
155
+ # Calls all the appropriate callbacks and then saves the new resource.
156
+ #
157
+ # @param [ActiveRecord::Base] object The new resource to save
158
+ #
159
+ # @returns [void]
160
+ def save_resource(object)
161
+ run_save_callbacks object do
162
+ object.save
163
+ end
164
+ end
165
+
166
+ # Update an object with the given attributes. Also calls the appropriate
167
+ # callbacks for update action.
168
+ #
169
+ # @param [ActiveRecord::Base] object The instance to update
170
+ #
171
+ # @param [Array] attributes An array with the attributes in the first position
172
+ # and the Active Record "role" in the second. The role
173
+ # may be set to nil.
174
+ #
175
+ # @returns [void]
176
+ def update_resource(object, attributes)
177
+ if object.respond_to?(:assign_attributes)
178
+ object.assign_attributes(*attributes)
179
+ else
180
+ object.attributes = attributes[0]
181
+ end
182
+
183
+ run_update_callbacks object do
184
+ save_resource(object)
185
+ end
186
+ end
187
+
188
+ # Destroys an object from the database and calls appropriate callbacks.
189
+ #
190
+ # @returns [void]
191
+ def destroy_resource(object)
192
+ run_destroy_callbacks object do
193
+ object.destroy
194
+ end
195
+ end
196
+
197
+
198
+ #
199
+ # Collection Helper Methods
200
+ #
201
+
202
+
203
+ # Gives the authorization library a change to pre-scope the collection.
204
+ #
205
+ # In the case of the CanCan adapter, it calls `#accessible_by` on
206
+ # the collection.
207
+ #
208
+ # @param [ActiveRecord::Relation] collection The collection to scope
209
+ #
210
+ # @retruns [ActiveRecord::Relation] a scoped collection of query
211
+ def scope_for_authorization(collection)
212
+ active_admin_authorization.scope_collection(collection)
213
+ end
214
+
215
+
216
+ def apply_sorting(chain)
217
+ params[:order] ||= active_admin_config.sort_order
218
+ if params[:order] && params[:order] =~ /^([\w\_\.]+)_(desc|asc)$/
219
+ column = $1
220
+ order = $2
221
+ table = active_admin_config.resource_column_names.include?(column) ? active_admin_config.resource_table_name : nil
222
+ table_column = (column =~ /\./) ? column :
223
+ [table, active_admin_config.resource_quoted_column_name(column)].compact.join(".")
224
+
225
+ chain.reorder("#{table_column} #{order}")
226
+ else
227
+ chain # just return the chain
228
+ end
229
+ end
230
+
231
+ def apply_filtering(chain)
232
+ @search = chain.metasearch(clean_search_params(params[:q]))
233
+ @search.relation
234
+ end
235
+
236
+ def clean_search_params(search_params)
237
+ return {} unless search_params.is_a?(Hash)
238
+ search_params = search_params.dup
239
+ search_params.delete_if do |key, value|
240
+ value == ""
241
+ end
242
+ search_params
243
+ end
244
+
245
+ def apply_scoping(chain)
246
+ @collection_before_scope = chain
247
+
248
+ if current_scope
249
+ scope_chain(current_scope, chain)
250
+ else
251
+ chain
252
+ end
253
+ end
254
+
255
+ def collection_before_scope
256
+ @collection_before_scope
257
+ end
258
+
259
+ def current_scope
260
+ @current_scope ||= if params[:scope]
261
+ active_admin_config.get_scope_by_id(params[:scope]) if params[:scope]
262
+ else
263
+ active_admin_config.default_scope
264
+ end
265
+ end
266
+
267
+ def apply_pagination(chain)
268
+ page_method_name = Kaminari.config.page_method_name
269
+ page = params[Kaminari.config.param_name]
270
+
271
+ chain.send(page_method_name, page).per(per_page)
272
+ end
273
+
274
+ def per_page
275
+ return max_csv_records if request.format == 'text/csv'
276
+ return max_per_page if active_admin_config.paginate == false
277
+
278
+ @per_page || active_admin_config.per_page
279
+ end
280
+
281
+ def max_csv_records
282
+ 10_000
283
+ end
284
+
285
+ def max_per_page
286
+ 10_000
287
+ end
288
+
289
+ def apply_decorator(chain)
290
+ if decorator?
291
+ decorator_class.decorate_collection(chain)
292
+ else
293
+ chain
294
+ end
295
+ end
296
+
297
+ end
298
+ end
299
+ end
@@ -12,8 +12,9 @@ module ActiveAdmin
12
12
  def active_admin_collection
13
13
  decorator = active_admin_config.decorator_class
14
14
  collection = super
15
- decorator ? decorator.decorate(collection) : collection
15
+ decorator ? decorator.decorate_collection(collection) : collection
16
16
  end
17
+
17
18
  end
18
19
  end
19
20
  end
@@ -18,7 +18,7 @@ module ActiveAdmin
18
18
  # end
19
19
  #
20
20
  # Then every time we instantiate and object, it would call
21
- #
21
+ #
22
22
  # current_user.posts.build
23
23
  #
24
24
  # By default Active Admin will use the resource name to build a
@@ -28,7 +28,7 @@ module ActiveAdmin
28
28
  # scope_to :current_user, :association_method => :blog_posts
29
29
  #
30
30
  # will result in the following
31
- #
31
+ #
32
32
  # current_user.blog_posts.build
33
33
  #
34
34
  def scope_to(*args, &block)
@@ -49,7 +49,7 @@ module ActiveAdmin
49
49
  options[:as] ||= :table
50
50
  config.set_page_presenter :index, ActiveAdmin::PagePresenter.new(options, &block)
51
51
  end
52
-
52
+
53
53
  # Configure the show page for the resource
54
54
  def show(options = {}, &block)
55
55
  config.set_page_presenter :show, ActiveAdmin::PagePresenter.new(options, &block)
@@ -94,9 +94,9 @@ module ActiveAdmin
94
94
  #
95
95
  # You can treat everything within the block as a standard Rails controller
96
96
  # 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)
100
100
  title = options.delete(:title)
101
101
 
102
102
  controller do
@@ -105,14 +105,12 @@ module ActiveAdmin
105
105
  end
106
106
  end
107
107
 
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
111
111
 
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
116
114
  end
117
115
 
118
116
  # Defined Callbacks
@@ -151,7 +149,7 @@ module ActiveAdmin
151
149
  # Specify which actions to create in the controller
152
150
  #
153
151
  # Eg:
154
- #
152
+ #
155
153
  # ActiveAdmin.register Post do
156
154
  # actions :index, :show
157
155
  # end
@@ -1,6 +1,5 @@
1
1
  module ActiveAdmin
2
2
  class Router
3
-
4
3
  def initialize(application)
5
4
  @application = application
6
5
  end
@@ -14,63 +13,42 @@ module ActiveAdmin
14
13
  # end
15
14
  #
16
15
  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
26
19
 
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|
27
24
  if namespace.root?
28
- instance_eval &root_and_dashboard_routes
25
+ instance_eval &aa_router.root_and_dashboard_routes(namespace)
29
26
  else
30
27
  namespace(namespace.name) do
31
- instance_eval &root_and_dashboard_routes
28
+ instance_eval &aa_router.root_and_dashboard_routes(namespace)
32
29
  end
33
30
  end
34
31
  end
35
32
  end
33
+ end
36
34
 
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
60
43
 
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)
65
47
 
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)
74
52
 
75
53
  # Add in the parent if it exists
76
54
  if config.belongs_to?
@@ -84,15 +62,6 @@ module ActiveAdmin
84
62
  resources config.belongs_to_config.target.resource_name.plural, :only => [] do
85
63
  instance_eval &routes_for_belongs_to
86
64
  end
87
-
88
- # Batch action path is not nested.
89
- if config.is_a?(Resource)
90
- resources config.resource_name.route_key, :only => config.defined_actions do
91
- collection do
92
- post :batch_action
93
- end
94
- end
95
- end
96
65
  end
97
66
  end
98
67
 
@@ -110,5 +79,41 @@ module ActiveAdmin
110
79
  end
111
80
  end
112
81
  end
82
+
83
+ def resource_routes(config)
84
+ Proc.new do
85
+ case config
86
+ when Resource
87
+ resources config.resource_name.route_key, :only => config.defined_actions do
88
+ # Define any member actions
89
+ member do
90
+ config.member_actions.each do |action|
91
+ [*action.http_verb].each do |http_verb|
92
+ # eg: get :comment
93
+ send(http_verb, action.name)
94
+ end
95
+ end
96
+ end
97
+
98
+ # Define any collection actions
99
+ collection do
100
+ config.collection_actions.each do |action|
101
+ send(action.http_verb, action.name)
102
+ end
103
+
104
+ post :batch_action
105
+ end
106
+ end
107
+ when Page
108
+ match "/#{config.underscored_resource_name}" => "#{config.underscored_resource_name}#index"
109
+ config.page_actions.each do |action|
110
+ match "/#{config.underscored_resource_name}/#{action.name}" => "#{config.underscored_resource_name}##{action.name}", :via => action.http_verb
111
+ end
112
+ else
113
+ raise "Unsupported config class: #{config.class}"
114
+ end
115
+ end
116
+
117
+ end
113
118
  end
114
119
  end