module ActiveAdmin::ResourceController::Decorators

Protected Class Methods

undecorate_resource(resource) click to toggle source

TODO: find a more suitable place for this

# File lib/active_admin/resource_controller/decorators.rb, line 19
def self.undecorate_resource(resource)
  if resource.respond_to?(:decorated?) && resource.decorated?
    resource.model
  else
    resource
  end
end

Protected Instance Methods

apply_collection_decorator(collection) click to toggle source
# File lib/active_admin/resource_controller/decorators.rb, line 10
def apply_collection_decorator(collection)
  if decorate?
    collection_decorator.decorate(collection, with: decorator_class)
  else
    collection
  end
end
apply_decorator(resource) click to toggle source
# File lib/active_admin/resource_controller/decorators.rb, line 6
def apply_decorator(resource)
  decorate? ? decorator_class.new(resource) : resource
end

Private Instance Methods

collection_decorator() click to toggle source
# File lib/active_admin/resource_controller/decorators.rb, line 43
def collection_decorator
  if decorator_class
    collection_decorator = collection_decorator_class_for(decorator_class)

    delegate_collection_methods_for_draper(collection_decorator, decorator_class)
  end
end
collection_decorator_class_for(decorator) click to toggle source

Draper::CollectionDecorator was introduced in 1.0.0 Draper::Decorator#collection_decorator_class was introduced in 1.3.0

# File lib/active_admin/resource_controller/decorators.rb, line 53
def collection_decorator_class_for(decorator)
  if Dependencies.draper?    :>=, '1.3.0'
    decorator.collection_decorator_class
  elsif Dependencies.draper? :>=, '1.0.0'
    draper_collection_decorator
  else
    decorator
  end
end
decorate?() click to toggle source
# File lib/active_admin/resource_controller/decorators.rb, line 29
def decorate?
  case action_name
  when 'new', 'edit'
    form = active_admin_config.get_page_presenter :form
    form && form.options[:decorate] && decorator_class.present?
  else
    decorator_class.present?
  end
end
decorator_class() click to toggle source
# File lib/active_admin/resource_controller/decorators.rb, line 39
def decorator_class
  active_admin_config.decorator_class
end
decorator_class_cache() click to toggle source
# File lib/active_admin/resource_controller/decorators.rb, line 84
def decorator_class_cache
  @@decorator_class_cache ||= {}
end
delegate_collection_methods_for_draper(collection_decorator, resource_decorator) click to toggle source
# File lib/active_admin/resource_controller/decorators.rb, line 63
def delegate_collection_methods_for_draper(collection_decorator, resource_decorator)
  return collection_decorator unless is_draper_collection_decorator?(collection_decorator)

  decorator_name = "#{collection_decorator.name} of #{resource_decorator} with ActiveAdmin extensions"
  decorator_class_cache[decorator_name] ||= generate_collection_decorator(collection_decorator, decorator_name)
end
draper_collection_decorator() click to toggle source
# File lib/active_admin/resource_controller/decorators.rb, line 94
def draper_collection_decorator
  Draper::CollectionDecorator
end
generate_collection_decorator(parent, name) click to toggle source

Create a new class that inherits from the collection decorator we are using. We use this class to delegate collection scoping methods that active_admin needs to render the table.

# File lib/active_admin/resource_controller/decorators.rb, line 73
def generate_collection_decorator(parent, name)
  klass = Class.new(parent) do
    delegate :reorder, :page, :current_page, :total_pages, :limit_value,
             :total_count, :num_pages, :to_key, :group_values
  end

  klass.define_singleton_method(:name) { name }

  klass
end
is_draper_collection_decorator?(decorator) click to toggle source
# File lib/active_admin/resource_controller/decorators.rb, line 88
def is_draper_collection_decorator?(decorator)
  decorator && decorator <= draper_collection_decorator
rescue NameError
  false
end