module ActiveAdmin::ResourceController::DataAccess

def apply_authorization_scope(collection)

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_decorator(chain)

def apply_decorator(chain)
  if decorator?
    decorator_class.decorate_collection(chain)
  else
    chain
  end
end

def apply_filtering(chain)

def apply_filtering(chain)
  @search = chain.metasearch(clean_search_params(params[:q]))
  @search.relation
end

def apply_pagination(chain)

def apply_pagination(chain)
  page_method = Kaminari.config.page_method_name
  page_param  = params[Kaminari.config.param_name]
  chain.send(page_method, page_param).per(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
  if params[:order] && params[:order] =~ /^([\w\_\.]+)_(desc|asc)$/
    column = $1
    order  = $2
    table  = active_admin_config.resource_column_names.include?(column) ? active_admin_config.resource_table_name : nil
    table_column = (column =~ /\./) ? column :
      [table, active_admin_config.resource_quoted_column_name(column)].compact.join(".")
    chain.reorder("#{table_column} #{order}")
  else
    chain # just return the chain
  end
end

def build_new_resource

@returns [ActiveRecord::Base] An un-saved active record base object

by Inherited Resources.
Builds a new resource. This method uses the method_for_build provided
def build_new_resource
  scoped_collection.send(method_for_build, *resource_params)
end

def build_resource

@returns [ActiveRecord::Base] An un-saved active record base object

new and create controller actions.
This method is used to instantiate and authorize new resources in the

#build_new_resource method.
actual work of building the new instance is delegated to the
Builds, memoize and authorize a new instance of the resource. The
def build_resource
  return resource if resource = get_resource_ivar
  resource = build_new_resource
  run_build_callbacks resource
  authorize_resource! resource
  set_resource_ivar(resource)
end

def clean_search_params(search_params)

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

def collection

@returns [ActiveRecord::Relation] The collection for the index

after the resource that the collection is for. eg: Post => @post.
either the @collection instance variable or an instance variable named
Once #collection has been called, the collection is available using

method delegates the finding of the collection to #find_collection.
Retrieve, memoize and authorize the current collection from the db. This
def collection
  _collection = get_collection_ivar
  return _collection if _collection
  _collection = find_collection
  authorize! ActiveAdmin::Authorization::READ, active_admin_config.resource_class
  set_collection_ivar _collection
end

def collection_before_scope

def collection_before_scope
  @collection_before_scope
end

def create_resource(object)

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]) if params[:scope]
  else
    active_admin_config.default_scope(self)
  end
end

def decorator?

def decorator?
  !!active_admin_config.decorator_class
end

def decorator_class

def decorator_class
  active_admin_config.decorator_class
end

def destroy_resource(object)

@returns [void]

Destroys an object from the database and calls appropriate callbacks.
def destroy_resource(object)
  run_destroy_callbacks object do
    object.destroy
  end
end

def find_collection

@returns [ActiveRecord::Relation] The collectin for the index

authorizes the collection.
some additional db # work before your controller returns and
This is a great method to override if you would like to perform
Does the actual work of retrieving the current collection from the db.
def find_collection
  collection = scoped_collection
  collection = apply_authorization_scope(collection)
  collection = apply_sorting(collection)
  collection = apply_filtering(collection)
  collection = apply_scoping(collection)
  collection = apply_pagination(collection)
  collection = apply_decorator(collection)
  collection
end

def find_resource

@returns [ActiveRecord::Base] An active record object.

method uses the finder method as defined in InheritedResources.
Does the actual work of finding a resource in the database. This
def find_resource
  scoped_collection.send(method_for_find, params[:id])
end

def max_per_page

def max_per_page
  10_000
end

def per_page

def per_page
  return max_per_page if active_admin_config.paginate == false
  @per_page || active_admin_config.per_page
end

def resource

@returns [ActiveRecord::Base] An active record object

* destroy
* update
* edit
* show

This method is used on all the member actions:

actual work of finding the resource is done in #find_resource.
Retrieve, memoize and authorize a resource based on params[:id]. The
def resource
  _resource = get_resource_ivar
  return _resource if _resource
  _resource = find_resource
  authorize_resource! _resource
  if decorator?
    _resource = decorator_class.new(_resource)
  end
  set_resource_ivar(_resource)
end

def save_resource(object)

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

method.
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 update_resource(object, attributes)

Parameters:
  • attributes (Array) -- An array with the attributes in the first position
  • object (ActiveRecord::Base) -- The instance to update
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