Class: ActiveAdmin::DSL

Inherits:
Object
  • Object
show all
Defined in:
lib/active_admin/dsl.rb

Overview

The Active Admin DSL. This class is where all the registration blocks are instance eval’d. This is the central place for the API given to users of Active Admin

Direct Known Subclasses

PageDSL, ResourceDSL

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ DSL

Returns a new instance of DSL.



10
11
12
# File 'lib/active_admin/dsl.rb', line 10

def initialize(config)
  @config = config
end

Instance Method Details

#action_item(options = {}, &block) ⇒ Object

Add a new action item to the resource

Parameters:

  • options (Hash) (defaults to: {})

    valid keys include: :only: A single or array of controller actions to display

    this action item on.
    

    :except: A single or array of controller actions not to

    display this action item on.
    


86
87
88
# File 'lib/active_admin/dsl.rb', line 86

def action_item(options = {}, &block)
  config.add_action_item(options, &block)
end

#batch_action(title, options = {}, &block) ⇒ Object

Add a new batch action item to the resource Provide a symbol/string to register the action, options, & block to execute on request

To unregister an existing action, just provide the symbol & pass false as the second param

> :if is a proc that will be called to determine if the BatchAction should be displayed

> :sort_order is used to sort the batch actions ascending

> :confirm is a string which the user will have to accept in order to process the action

Parameters:

  • title (Symbol or String)
  • options (Hash) (defaults to: {})

    valid keys include:



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/active_admin/dsl.rb', line 101

def batch_action(title, options = {}, &block)
  # Create symbol & title information
  if title.is_a? String
    sym = title.titleize.gsub(' ', '').underscore.to_sym
  else
    sym = title
    title = sym.to_s.titleize
  end

  # Either add/remove the batch action
  unless options == false
    config.add_batch_action( sym, title, options, &block )
  else
    config.remove_batch_action sym
  end
end

Rewrite breadcrumb links. Block will be executed inside controller. Block must return an array if you want to rewrite breadcrumb links.

Example:

ActiveAdmin.register Post do

  breadcrumb do
    [
      link_to('my piece', '/my/link/to/piece')
    ]
  end
end


151
152
153
# File 'lib/active_admin/dsl.rb', line 151

def breadcrumb(&block)
  config.breadcrumb = block
end

#configObject

The instance of ActiveAdmin::Config that’s being registered currently. You can use this within your registration blocks to modify options:

eg:

ActiveAdmin.register Post do
  config.sort_order = "id_desc"
end


29
30
31
# File 'lib/active_admin/dsl.rb', line 29

def config
  @config
end

#controller(&block) ⇒ Object

Returns the controller for this resource. If you pass a block, it will be eval’d in the controller

Example:

ActiveAdmin.register Post do

  controller do
    def some_method_on_controller
      # Method gets added to Admin::PostsController
    end
  end

end


74
75
76
77
# File 'lib/active_admin/dsl.rb', line 74

def controller(&block)
  @config.controller.class_eval(&block) if block_given?
  @config.controller
end

#decorate_with(decorator_class) ⇒ Object



159
160
161
162
163
164
# File 'lib/active_admin/dsl.rb', line 159

def decorate_with(decorator_class)
  # Force storage as a string. This will help us with reloading issues.
  # Assuming decorator_class.to_s will return the name of the class allows
  # us to handle a string or a class.
  config.decorator_class_name = "::#{ decorator_class }"
end

#include(mod) ⇒ Object

Include a module with this resource. The modules’s ‘included` method is called with the instance of the `ActiveAdmin::DSL` passed into it.

eg:

module HelpSidebar

  def self.included(dsl)
    dsl.sidebar "Help" do
      "Call us for Help"
    end
  end

end

ActiveAdmin.register Post do
  include HelpSidebar
end

Parameters:

  • mod (Module)

    A module to include



55
56
57
# File 'lib/active_admin/dsl.rb', line 55

def include(mod)
  mod.included(self)
end

Set the options that are available for the item that will be placed in the global navigation of the menu.



120
121
122
# File 'lib/active_admin/dsl.rb', line 120

def menu(options = {})
  config.menu_item_options = options
end

Set the name of the navigation menu to display. This is mainly used in conjuction with the ‘#belongs_to` functionality.

Pass a block returning the name of a menu you want rendered for the request, being executed in the context of the controller

Parameters:

  • menu_name (Symbol) (defaults to: nil)

    The name of the menu to display as the global navigation when viewing this resource. Defaults to a menu named ‘:default`.



133
134
135
# File 'lib/active_admin/dsl.rb', line 133

def navigation_menu(menu_name=nil, &block)
  config.navigation_menu_name = menu_name || block
end

#run_registration_block(&block) ⇒ Object

Runs the registration block inside this object



15
16
17
# File 'lib/active_admin/dsl.rb', line 15

def run_registration_block(&block)
  instance_eval &block if block_given?
end


155
156
157
# File 'lib/active_admin/dsl.rb', line 155

def sidebar(name, options = {}, &block)
  config.sidebar_sections << ActiveAdmin::SidebarSection.new(name, options, &block)
end