class ActiveAdmin::ResourceDSL

This is the class where all the register blocks are evaluated.

Public Class Methods

new(config, resource_class) click to toggle source
Calls superclass method ActiveAdmin::DSL::new
# File lib/active_admin/resource_dsl.rb, line 4
def initialize(config, resource_class)
  @resource = resource_class
  super(config)
end

Private Instance Methods

action(set, name, options = {}, &block) click to toggle source

Member Actions give you the functionality of defining both the action and the route directly from your ActiveAdmin registration block.

For example:

ActiveAdmin.register Post do
  member_action :comments do
    @post = Post.find(params[:id]
    @comments = @post.comments
  end
end

Will create a new controller action comments and will hook it up to the named route (comments_admin_post_path) /admin/posts/:id/comments

You can treat everything within the block as a standard Rails controller action.

# File lib/active_admin/resource_dsl.rb, line 107
def action(set, name, options = {}, &block)
  set << ControllerAction.new(name, options)
  title = options.delete(:title)

  controller do
    before_filter(only: [name]) { @page_title = title } if title
    define_method(name, &block || Proc.new{})
  end
end
belongs_to(target, options = {}) click to toggle source
# File lib/active_admin/resource_dsl.rb, line 11
def belongs_to(target, options = {})
  config.belongs_to(target, options)
end
collection_action(name, options = {}, &block) click to toggle source
# File lib/active_admin/resource_dsl.rb, line 121
def collection_action(name, options = {}, &block)
  action config.collection_actions, name, options, &block
end
csv(options={}, &block) click to toggle source

Configure the CSV format

For example:

csv do
  column :name
  column("Author") { |post| post.author.full_name }
end

csv col_sep: ";", force_quotes: true do
  column :name
end
# File lib/active_admin/resource_dsl.rb, line 82
def csv(options={}, &block)
  options[:resource] = @resource

  config.csv_builder = CSVBuilder.new(options, &block)
end
form(options = {}, &block) click to toggle source
# File lib/active_admin/resource_dsl.rb, line 65
def form(options = {}, &block)
  config.set_page_presenter :form, ActiveAdmin::PagePresenter.new(options, &block)
end
index(options = {}, &block) click to toggle source

Configure the index page for the resource

# File lib/active_admin/resource_dsl.rb, line 55
def index(options = {}, &block)
  options[:as] ||= :table
  config.set_page_presenter :index, ActiveAdmin::PagePresenter.new(options, &block)
end
member_action(name, options = {}, &block) click to toggle source
# File lib/active_admin/resource_dsl.rb, line 117
def member_action(name, options = {}, &block)
  action config.member_actions, name, options, &block
end
permit_params(*args, &block) click to toggle source

Rails 4 Strong Parameters Support

Either

permit_params :title, :author, :body

Or

permit_params do
  defaults = [:title, :body]
  if current_user.admin?
    defaults + [:author]
  else
    defaults
  end
end
# File lib/active_admin/resource_dsl.rb, line 43
def permit_params(*args, &block)
  resource_sym = config.resource_name.singular.to_sym

  controller do
    define_method :permitted_params do
      params.permit resource_sym =>
                    block ? instance_exec(&block) : args
    end
  end
end
scope(*args, &block) click to toggle source

Create a scope

# File lib/active_admin/resource_dsl.rb, line 21
def scope(*args, &block)
  config.scope(*args, &block)
end
scope_to(*args, &block) click to toggle source

Scope collection to a relation

# File lib/active_admin/resource_dsl.rb, line 16
def scope_to(*args, &block)
  config.scope_to(*args, &block)
end
show(options = {}, &block) click to toggle source

Configure the show page for the resource

# File lib/active_admin/resource_dsl.rb, line 61
def show(options = {}, &block)
  config.set_page_presenter :show, ActiveAdmin::PagePresenter.new(options, &block)
end