module ActiveAdmin::ResourceController::DataAccess
def self.included(base)
def self.included(base) base.class_exec do include Callbacks include ScopeChain define_active_admin_callbacks :build, :create, :update, :save, :destroy helper_method :current_scope end end
def apply_authorization_scope(collection)
-
(ActiveRecord::Relation)
- a scoped collection of query
Parameters:
-
collection
(ActiveRecord::Relation
) -- The collection to scope
def apply_authorization_scope(collection) action_name = action_to_permission(params[:action]) active_admin_authorization.scope_collection(collection, action_name) end
def apply_decorations(resource)
-
(ActiveRecord::Base)
- resource
Parameters:
-
resource
(ActiveRecord::Base
) --
def apply_decorations(resource) apply_decorator(resource) end
def apply_filtering(chain)
Applies any Ransack search methods to the currently scoped collection.
def apply_filtering(chain) @search = chain.ransack(params[:q] || {}) @search.result end
def apply_includes(chain)
def apply_includes(chain) if active_admin_config.includes.any? chain.includes *active_admin_config.includes else chain end end
def apply_pagination(chain)
def apply_pagination(chain) # skip pagination if CSV format was requested return chain if params["format"] == "csv" # skip pagination if already was paginated by scope return chain if chain.respond_to?(:total_pages) page = params[Kaminari.config.param_name] paginate(chain, page, per_page) end
def apply_scoping(chain)
def apply_scoping(chain) @collection_before_scope = chain if current_scope scope_chain(current_scope, chain) else chain end end
def apply_sorting(chain)
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
def assign_attributes(resource, attributes)
-
(ActiveRecord::Base)
- resource
Parameters:
-
attributes
(Array
) -- ttributes [Array resource
(ActiveRecord::Base
) --
def assign_attributes(resource, attributes) if resource.respond_to?(:assign_attributes) resource.assign_attributes(*attributes) else resource.attributes = attributes[0] end resource end
def batch_size
def batch_size 1000 end
def build_new_resource
-
(ActiveRecord::Base)
- An un-saved active record base object
def build_new_resource apply_authorization_scope(scoped_collection).send( method_for_build, *resource_params.map { |params| params.slice(active_admin_config.resource_class.inheritance_column) } ) end
def build_resource
-
(ActiveRecord::Base)
- An un-saved active record base object
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 authorize_resource! resource set_resource_ivar resource end end
def collection
-
(ActiveRecord::Relation)
- The collection for the index
def collection get_collection_ivar || begin collection = find_collection authorize! Authorization::READ, active_admin_config.resource_class set_collection_ivar collection end end
def collection_applies(options = {})
def collection_applies(options = {}) only = Array(options.fetch(:only, COLLECTION_APPLIES)) except = Array(options.fetch(:except, [])) COLLECTION_APPLIES & only - except end
def collection_before_scope
def collection_before_scope @collection_before_scope end
def configured_per_page
def configured_per_page Array(active_admin_config.per_page).first end
def create_another?
-
(Boolean)
- true if user requested to create one more
def create_another? params[:create_another].present? end
def create_resource(object)
-
(void)
-
Parameters:
-
object
(ActiveRecord::Base
) -- The new resource to create
def create_resource(object) run_create_callbacks object do save_resource(object) end end
def current_scope
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
def destroy_resource(object)
-
(void)
-
def destroy_resource(object) run_destroy_callbacks object do object.destroy end end
def dynamic_per_page
def dynamic_per_page params[:per_page] || @per_page end
def find_collection(options = {})
-
(ActiveRecord::Relation)
- The collection for the index
def find_collection(options = {}) collection = scoped_collection collection_applies(options).each do |applyer| collection = send("apply_#{applyer}", collection) end collection end
def find_resource
-
(ActiveRecord::Base)
- An active record object.
def find_resource scoped_collection.send method_for_find, params[:id] end
def in_paginated_batches(&block)
def in_paginated_batches(&block) ActiveRecord::Base.uncached do (1..paginated_collection.total_pages).each do |page| paginated_collection(page).each do |resource| yield apply_decorator(resource) end end end end
def paginate(chain, page, per_page)
def paginate(chain, page, per_page) page_method_name = Kaminari.config.page_method_name chain.public_send(page_method_name, page).per(per_page) end
def paginated_collection(page_no = 1)
def paginated_collection(page_no = 1) paginate(collection, page_no, batch_size) end
def per_page
def per_page if active_admin_config.paginate dynamic_per_page || configured_per_page else active_admin_config.max_per_page end end
def resource
-
(ActiveRecord::Base)
- An active record object
def resource get_resource_ivar || begin resource = find_resource resource = apply_decorations(resource) authorize_resource! resource set_resource_ivar resource end end
def save_resource(object)
-
(void)
-
Parameters:
-
object
(ActiveRecord::Base
) -- The new resource to save
def save_resource(object) run_save_callbacks object do object.save end end
def scoped_collection
scope_to method from the Scoping module instead of overriding this
Note, unless you are doing something special, you should use the
the searching and filtering can be applied on top
This method should return an ActiveRecord::Relation object so that
of our searches and index.
Override this method in your controllers to modify the start point
def scoped_collection end_of_association_chain end
def smart_resource_url
-
(String)
-
def smart_resource_url if create_another? new_resource_url(create_another: params[:create_another]) else super end end
def update_resource(object, attributes)
-
(void)
-
Parameters:
-
attributes
(Array
) -- An array with the attributes in the first position -
object
(ActiveRecord::Base
) -- The instance to update
def update_resource(object, attributes) object = assign_attributes(object, attributes) run_update_callbacks object do save_resource(object) end end