Module: ActiveAdmin::ResourceController::DataAccess
- Extended by:
- ActiveSupport::Concern
- Includes:
- Callbacks, ScopeChain
- 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.
Instance Method Summary collapse
-
#apply_authorization_scope(collection) ⇒ Object
protected
Gives the authorization library a change to pre-scope the collection.
-
#apply_filtering(chain) ⇒ Object
protected
Applies any Ransack search methods to the currently scoped collection.
- #apply_pagination(chain) ⇒ Object protected
- #apply_scoping(chain) ⇒ Object protected
- #apply_sorting(chain) ⇒ Object protected
-
#build_new_resource ⇒ Object
protected
Builds a new resource.
-
#build_resource ⇒ Object
protected
Builds, memoize and authorize a new instance of the resource.
- #clean_search_params(search_params) ⇒ Object protected
-
#collection ⇒ Object
protected
Retrieve, memoize and authorize the current collection from the db.
- #collection_before_scope ⇒ Object protected
-
#create_resource(object) ⇒ Object
protected
Calls all the appropriate callbacks and then creates the new resource.
- #current_scope ⇒ Object protected
-
#destroy_resource(object) ⇒ Object
protected
Destroys an object from the database and calls appropriate callbacks.
-
#find_collection ⇒ Object
protected
Does the actual work of retrieving the current collection from the db.
-
#find_resource ⇒ Object
protected
Does the actual work of finding a resource in the database.
- #max_csv_records ⇒ Object protected
- #max_per_page ⇒ Object protected
- #per_page ⇒ Object protected
-
#resource ⇒ Object
protected
Retrieve, memoize and authorize a resource based on params.
-
#save_resource(object) ⇒ Object
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.
-
#update_resource(object, attributes) ⇒ Object
protected
Update an object with the given attributes.
Methods included from ScopeChain
Methods included from Callbacks
Instance Method Details
#apply_authorization_scope(collection) ⇒ Object (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.
205 206 207 208 |
# File 'lib/active_admin/resource_controller/data_access.rb', line 205 def (collection) action_name = (params[:action]) .scope_collection(collection, action_name) 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.
224 225 226 227 |
# File 'lib/active_admin/resource_controller/data_access.rb', line 224 def apply_filtering(chain) @search = chain.ransack clean_search_params params[:q] @search.result end |
#apply_pagination(chain) ⇒ Object (protected)
260 261 262 263 264 265 |
# File 'lib/active_admin/resource_controller/data_access.rb', line 260 def apply_pagination(chain) page_method_name = Kaminari.config.page_method_name page = params[Kaminari.config.param_name] chain.send(page_method_name, page).per(per_page) end |
#apply_scoping(chain) ⇒ Object (protected)
238 239 240 241 242 243 244 245 246 |
# File 'lib/active_admin/resource_controller/data_access.rb', line 238 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)
210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/active_admin/resource_controller/data_access.rb', line 210 def apply_sorting(chain) params[:order] ||= active_admin_config.sort_order order_clause = OrderClause.new params[:order] if order_clause.valid? chain.reorder(order_clause.to_sql(active_admin_config)) else chain # just return the chain end end |
#build_new_resource ⇒ Object (protected)
Builds a new resource. This method uses the method_for_build provided by Inherited Resources.
134 135 136 |
# File 'lib/active_admin/resource_controller/data_access.rb', line 134 def build_new_resource scoped_collection.send(method_for_build, *resource_params) end |
#build_resource ⇒ Object (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.
115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/active_admin/resource_controller/data_access.rb', line 115 def build_resource _resource = get_resource_ivar return _resource if _resource _resource = build_new_resource run_build_callbacks _resource _resource _resource = apply_decorator(_resource) set_resource_ivar(_resource) end |
#clean_search_params(search_params) ⇒ Object (protected)
229 230 231 232 233 234 235 236 |
# File 'lib/active_admin/resource_controller/data_access.rb', line 229 def clean_search_params(search_params) return {} unless search_params.is_a?(Hash) search_params = search_params.dup search_params.delete_if do |key, value| value == "" end search_params end |
#collection ⇒ Object (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.
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/active_admin/resource_controller/data_access.rb', line 29 def collection _collection = get_collection_ivar return _collection if _collection _collection = find_collection ActiveAdmin::Authorization::READ, active_admin_config.resource_class set_collection_ivar _collection end |
#collection_before_scope ⇒ Object (protected)
248 249 250 |
# File 'lib/active_admin/resource_controller/data_access.rb', line 248 def collection_before_scope @collection_before_scope end |
#create_resource(object) ⇒ Object (protected)
Calls all the appropriate callbacks and then creates the new resource.
143 144 145 146 147 |
# File 'lib/active_admin/resource_controller/data_access.rb', line 143 def create_resource(object) run_create_callbacks object do save_resource(object) end end |
#current_scope ⇒ Object (protected)
252 253 254 255 256 257 258 |
# File 'lib/active_admin/resource_controller/data_access.rb', line 252 def current_scope @current_scope ||= if params[:scope] active_admin_config.get_scope_by_id(params[:scope]) if params[:scope] else active_admin_config.default_scope(self) end end |
#destroy_resource(object) ⇒ Object (protected)
Destroys an object from the database and calls appropriate callbacks.
185 186 187 188 189 |
# File 'lib/active_admin/resource_controller/data_access.rb', line 185 def destroy_resource(object) run_destroy_callbacks object do object.destroy end end |
#find_collection ⇒ Object (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.
47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/active_admin/resource_controller/data_access.rb', line 47 def find_collection collection = scoped_collection collection = (collection) collection = apply_sorting(collection) collection = apply_filtering(collection) collection = apply_scoping(collection) collection = apply_pagination(collection) collection = apply_collection_decorator(collection) collection end |
#find_resource ⇒ Object (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 |
#max_csv_records ⇒ Object (protected)
274 275 276 |
# File 'lib/active_admin/resource_controller/data_access.rb', line 274 def max_csv_records 10_000 end |
#max_per_page ⇒ Object (protected)
278 279 280 |
# File 'lib/active_admin/resource_controller/data_access.rb', line 278 def max_per_page 10_000 end |
#per_page ⇒ Object (protected)
267 268 269 270 271 272 |
# File 'lib/active_admin/resource_controller/data_access.rb', line 267 def per_page return max_csv_records if request.format == 'text/csv' return max_per_page if active_admin_config.paginate == false @per_page || active_admin_config.per_page end |
#resource ⇒ Object (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
85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/active_admin/resource_controller/data_access.rb', line 85 def resource _resource = get_resource_ivar return _resource if _resource _resource = find_resource _resource _resource = apply_decorator(_resource) set_resource_ivar(_resource) end |
#save_resource(object) ⇒ Object (protected)
Calls all the appropriate callbacks and then saves the new resource.
154 155 156 157 158 |
# File 'lib/active_admin/resource_controller/data_access.rb', line 154 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.
70 71 72 |
# File 'lib/active_admin/resource_controller/data_access.rb', line 70 def scoped_collection end_of_association_chain end |
#update_resource(object, attributes) ⇒ Object (protected)
Update an object with the given attributes. Also calls the appropriate callbacks for update action.
170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/active_admin/resource_controller/data_access.rb', line 170 def update_resource(object, attributes) if object.respond_to?(:assign_attributes) object.assign_attributes(*attributes) else object.attributes = attributes[0] end run_update_callbacks object do save_resource(object) end end |