Module: ActiveAdmin::ResourceController::DataAccess
- Included in:
- ActiveAdmin::ResourceController
- Defined in:
- lib/active_admin/resource_controller/data_access.rb
Overview
This module overrides most of the data access methods in Inherited Resources to provide Active Admin with it’s data.
The module also deals with authorization and resource callbacks.
Constant Summary collapse
- COLLECTION_APPLIES =
[ :authorization_scope, :sorting, :filtering, :scoping, :includes, :pagination, :collection_decorator ].freeze
Instance Method Summary collapse
-
#apply_authorization_scope(collection) ⇒ ActiveRecord::Relation
protected
Gives the authorization library a change to pre-scope the collection.
-
#apply_decorations(resource) ⇒ ActiveRecord::Base
protected
Resource.
-
#apply_filtering(chain) ⇒ Object
protected
Applies any Ransack search methods to the currently scoped collection.
- #apply_includes(chain) ⇒ Object protected
- #apply_pagination(chain) ⇒ Object protected
- #apply_scoping(chain) ⇒ Object protected
- #apply_sorting(chain) ⇒ Object protected
-
#assign_attributes(resource, attributes) ⇒ ActiveRecord::Base
protected
Resource.
-
#build_new_resource ⇒ ActiveRecord::Base
protected
Builds a new resource.
-
#build_resource ⇒ ActiveRecord::Base
protected
Builds, memoize and authorize a new instance of the resource.
-
#collection ⇒ ActiveRecord::Relation
protected
Retrieve, memoize and authorize the current collection from the db.
- #collection_applies(options = {}) ⇒ Object protected
- #collection_before_scope ⇒ Object protected
- #configured_per_page ⇒ Object protected
-
#create_resource(object) ⇒ void
protected
Calls all the appropriate callbacks and then creates the new resource.
- #current_scope ⇒ Object protected
-
#destroy_resource(object) ⇒ void
protected
Destroys an object from the database and calls appropriate callbacks.
- #dynamic_per_page ⇒ Object protected
-
#find_collection(options = {}) ⇒ ActiveRecord::Relation
protected
Does the actual work of retrieving the current collection from the db.
-
#find_resource ⇒ ActiveRecord::Base
protected
Does the actual work of finding a resource in the database.
- #per_page ⇒ Object protected
-
#permitted_attr_names ⇒ Array
protected
Names.
- #permitted_params ⇒ Object protected
-
#resource ⇒ ActiveRecord::Base
protected
Retrieve, memoize and authorize a resource based on params.
-
#save_resource(object) ⇒ void
protected
Calls all the appropriate callbacks and then saves the new resource.
-
#scoped_collection ⇒ Object
protected
Override this method in your controllers to modify the start point of our searches and index.
- #smart_resource_url ⇒ String protected
-
#update_resource(object, attributes) ⇒ void
protected
Update an object with the given attributes.
Instance Method Details
#apply_authorization_scope(collection) ⇒ ActiveRecord::Relation (protected)
Gives the authorization library a change to pre-scope the collection.
In the case of the CanCan adapter, it calls ‘#accessible_by` on the collection.
198 199 200 201 |
# File 'lib/active_admin/resource_controller/data_access.rb', line 198 def (collection) action_name = (params[:action]) .scope_collection(collection, action_name) end |
#apply_decorations(resource) ⇒ ActiveRecord::Base (protected)
Returns resource.
300 301 302 |
# File 'lib/active_admin/resource_controller/data_access.rb', line 300 def apply_decorations(resource) apply_decorator(resource) end |
#apply_filtering(chain) ⇒ Object (protected)
Applies any Ransack search methods to the currently scoped collection. Both ‘search` and `ransack` are provided, but we use `ransack` to prevent conflicts.
216 217 218 219 |
# File 'lib/active_admin/resource_controller/data_access.rb', line 216 def apply_filtering(chain) @search = chain.ransack(params[:q] || {}) @search.result end |
#apply_includes(chain) ⇒ Object (protected)
231 232 233 234 235 236 237 |
# File 'lib/active_admin/resource_controller/data_access.rb', line 231 def apply_includes(chain) if active_admin_config.includes.any? chain.includes *active_admin_config.includes else chain end end |
#apply_pagination(chain) ⇒ Object (protected)
251 252 253 254 255 256 257 258 |
# File 'lib/active_admin/resource_controller/data_access.rb', line 251 def apply_pagination(chain) # skip pagination if already was paginated by scope return chain if chain.respond_to?(:total_pages) page_method_name = Kaminari.config.page_method_name page = params[Kaminari.config.param_name] chain.public_send(page_method_name, page).per(per_page) end |
#apply_scoping(chain) ⇒ Object (protected)
221 222 223 224 225 226 227 228 229 |
# File 'lib/active_admin/resource_controller/data_access.rb', line 221 def apply_scoping(chain) @collection_before_scope = chain if current_scope scope_chain(current_scope, chain) else chain end end |
#apply_sorting(chain) ⇒ Object (protected)
203 204 205 206 207 208 209 210 211 212 |
# File 'lib/active_admin/resource_controller/data_access.rb', line 203 def apply_sorting(chain) params[:order] ||= active_admin_config.sort_order order_clause = active_admin_config.order_clause.new(active_admin_config, params[:order]) if order_clause.valid? order_clause.apply(chain) else chain # just return the chain end end |
#assign_attributes(resource, attributes) ⇒ ActiveRecord::Base (protected)
Returns resource.
287 288 289 290 291 292 293 294 295 |
# File 'lib/active_admin/resource_controller/data_access.rb', line 287 def assign_attributes(resource, attributes) if resource.respond_to?(:assign_attributes) resource.assign_attributes(*attributes) else resource.attributes = attributes[0] end resource end |
#build_new_resource ⇒ ActiveRecord::Base (protected)
Builds a new resource. This method uses the method_for_build provided by Inherited Resources.
130 131 132 133 134 135 |
# File 'lib/active_admin/resource_controller/data_access.rb', line 130 def build_new_resource scoped_collection.send( method_for_build, *resource_params.map { |params| params.slice(active_admin_config.resource_class.inheritance_column) } ) end |
#build_resource ⇒ ActiveRecord::Base (protected)
Builds, memoize and authorize a new instance of the resource. The actual work of building the new instance is delegated to the #build_new_resource method.
This method is used to instantiate and authorize new resources in the new and create controller actions.
114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/active_admin/resource_controller/data_access.rb', line 114 def build_resource get_resource_ivar || begin resource = build_new_resource resource = apply_decorations(resource) resource = assign_attributes(resource, resource_params) run_build_callbacks resource resource set_resource_ivar resource end end |
#collection ⇒ ActiveRecord::Relation (protected)
Retrieve, memoize and authorize the current collection from the db. This method delegates the finding of the collection to #find_collection.
Once #collection has been called, the collection is available using either the @collection instance variable or an instance variable named after the resource that the collection is for. eg: Post => @post.
42 43 44 45 46 47 48 |
# File 'lib/active_admin/resource_controller/data_access.rb', line 42 def collection get_collection_ivar || begin collection = find_collection Authorization::READ, active_admin_config.resource_class set_collection_ivar collection end end |
#collection_applies(options = {}) ⇒ Object (protected)
260 261 262 263 264 265 |
# File 'lib/active_admin/resource_controller/data_access.rb', line 260 def collection_applies( = {}) only = Array(.fetch(:only, COLLECTION_APPLIES)) except = Array(.fetch(:except, [])) COLLECTION_APPLIES & only - except end |
#collection_before_scope ⇒ Object (protected)
239 240 241 |
# File 'lib/active_admin/resource_controller/data_access.rb', line 239 def collection_before_scope @collection_before_scope end |
#configured_per_page ⇒ Object (protected)
279 280 281 |
# File 'lib/active_admin/resource_controller/data_access.rb', line 279 def configured_per_page Array(active_admin_config.per_page).first end |
#create_resource(object) ⇒ void (protected)
This method returns an undefined value.
Calls all the appropriate callbacks and then creates the new resource.
142 143 144 145 146 |
# File 'lib/active_admin/resource_controller/data_access.rb', line 142 def create_resource(object) run_create_callbacks object do save_resource(object) end end |
#current_scope ⇒ Object (protected)
243 244 245 246 247 248 249 |
# File 'lib/active_admin/resource_controller/data_access.rb', line 243 def current_scope @current_scope ||= if params[:scope] active_admin_config.get_scope_by_id(params[:scope]) else active_admin_config.default_scope(self) end end |
#destroy_resource(object) ⇒ void (protected)
This method returns an undefined value.
Destroys an object from the database and calls appropriate callbacks.
180 181 182 183 184 |
# File 'lib/active_admin/resource_controller/data_access.rb', line 180 def destroy_resource(object) run_destroy_callbacks object do object.destroy end end |
#dynamic_per_page ⇒ Object (protected)
275 276 277 |
# File 'lib/active_admin/resource_controller/data_access.rb', line 275 def dynamic_per_page params[:per_page] || @per_page end |
#find_collection(options = {}) ⇒ ActiveRecord::Relation (protected)
Does the actual work of retrieving the current collection from the db. This is a great method to override if you would like to perform some additional db # work before your controller returns and authorizes the collection.
56 57 58 59 60 61 62 |
# File 'lib/active_admin/resource_controller/data_access.rb', line 56 def find_collection( = {}) collection = scoped_collection collection_applies().each do |applyer| collection = send("apply_#{applyer}", collection) end collection end |
#find_resource ⇒ ActiveRecord::Base (protected)
Does the actual work of finding a resource in the database. This method uses the finder method as defined in InheritedResources.
102 103 104 |
# File 'lib/active_admin/resource_controller/data_access.rb', line 102 def find_resource scoped_collection.send method_for_find, params[:id] end |
#per_page ⇒ Object (protected)
267 268 269 270 271 272 273 |
# File 'lib/active_admin/resource_controller/data_access.rb', line 267 def per_page if active_admin_config.paginate dynamic_per_page || configured_per_page else active_admin_config.max_per_page end end |
#permitted_attr_names ⇒ Array (protected)
Returns names.
318 319 320 321 |
# File 'lib/active_admin/resource_controller/data_access.rb', line 318 def permitted_attr_names attr_names = active_admin_config.permitted_attr_names attr_names.is_a?(Proc) ? instance_exec(&attr_names) : attr_names end |
#permitted_params ⇒ Object (protected)
304 305 306 307 308 309 310 311 312 313 314 |
# File 'lib/active_admin/resource_controller/data_access.rb', line 304 def permitted_params permitted_params = active_admin_namespace.permitted_params + Array.wrap(active_admin_config.belongs_to_param) permitted_params << :create_another if active_admin_config.create_another param_key = active_admin_config.param_key params.permit(*permitted_params, param_key.to_sym => permitted_attr_names) end |
#resource ⇒ ActiveRecord::Base (protected)
Retrieve, memoize and authorize a resource based on params. The actual work of finding the resource is done in #find_resource.
This method is used on all the member actions:
* show
* edit
* update
* destroy
88 89 90 91 92 93 94 95 96 |
# File 'lib/active_admin/resource_controller/data_access.rb', line 88 def resource get_resource_ivar || begin resource = find_resource resource = apply_decorations(resource) resource set_resource_ivar resource end end |
#save_resource(object) ⇒ void (protected)
This method returns an undefined value.
Calls all the appropriate callbacks and then saves the new resource.
153 154 155 156 157 |
# File 'lib/active_admin/resource_controller/data_access.rb', line 153 def save_resource(object) run_save_callbacks object do object.save end end |
#scoped_collection ⇒ Object (protected)
Override this method in your controllers to modify the start point of our searches and index.
This method should return an ActiveRecord::Relation object so that the searching and filtering can be applied on top
Note, unless you are doing something special, you should use the scope_to method from the Scoping module instead of overriding this method.
73 74 75 |
# File 'lib/active_admin/resource_controller/data_access.rb', line 73 def scoped_collection end_of_association_chain end |
#smart_resource_url ⇒ String (protected)
324 325 326 327 328 329 330 |
# File 'lib/active_admin/resource_controller/data_access.rb', line 324 def smart_resource_url if create_another? new_resource_url(create_another: params[:create_another]) else super end end |
#update_resource(object, attributes) ⇒ void (protected)
This method returns an undefined value.
Update an object with the given attributes. Also calls the appropriate callbacks for update action.
169 170 171 172 173 174 175 |
# File 'lib/active_admin/resource_controller/data_access.rb', line 169 def update_resource(object, attributes) object = assign_attributes(object, attributes) run_update_callbacks object do save_resource(object) end end |