Class: ActiveAdmin::DSL
- Inherits:
-
Object
- Object
- ActiveAdmin::DSL
- Defined in:
- lib/active_admin/dsl.rb
Overview
The Active Admin DSL. This class is where all the registration blocks are evaluated. This is the central place for the API given to users of Active Admin.
Direct Known Subclasses
Instance Method Summary collapse
-
#action_item(name = nil, options = {}, &block) ⇒ Object
Add a new action item to the resource.
-
#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.
-
#breadcrumb(&block) ⇒ Object
Rewrite breadcrumb links.
-
#config ⇒ Object
The instance of ActiveAdmin::Resource that’s being registered currently.
-
#controller(&block) ⇒ Object
Returns the controller for this resource.
- #define_controller_method(name, &block) ⇒ Object protected
-
#include(mod) ⇒ Nil
Include a module with this resource.
-
#initialize(config) ⇒ DSL
constructor
A new instance of DSL.
-
#menu(options = {}) ⇒ Object
Set the options that are available for the item that will be placed in the global navigation of the menu.
-
#navigation_menu(menu_name = nil, &block) ⇒ Object
Set the name of the navigation menu to display.
-
#run_registration_block(&block) ⇒ Object
Runs the registration block inside this object.
- #set_title_before_action(name, title) ⇒ Object protected
- #sidebar(name, options = {}, &block) ⇒ Object
Constructor Details
#initialize(config) ⇒ DSL
Returns a new instance of DSL.
9 10 11 |
# File 'lib/active_admin/dsl.rb', line 9 def initialize(config) @config = config end |
Instance Method Details
#action_item(name = nil, options = {}, &block) ⇒ Object
Add a new action item to the resource
86 87 88 89 90 91 92 93 94 95 |
# File 'lib/active_admin/dsl.rb', line 86 def action_item(name = nil, = {}, &block) if name.is_a?(Hash) = name name = nil end Deprecation.warn "using `action_item` without a name is deprecated! Use `action_item(:edit)`." unless name config.add_action_item(name, , &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
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/active_admin/dsl.rb', line 108 def batch_action(title, = {}, &block) # Create symbol & title information if title.is_a? String sym = title.titleize.tr(' ', '').underscore.to_sym else sym = title title = sym.to_s.titleize end # Either add/remove the batch action unless == false config.add_batch_action(sym, title, , &block) else config.remove_batch_action sym end end |
#breadcrumb(&block) ⇒ Object
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
do
[
link_to('my piece', '/my/link/to/piece')
]
end
end
158 159 160 |
# File 'lib/active_admin/dsl.rb', line 158 def (&block) config. = block end |
#config ⇒ Object
The instance of ActiveAdmin::Resource 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
28 29 30 |
# File 'lib/active_admin/dsl.rb', line 28 def config @config end |
#controller(&block) ⇒ Object
Returns the controller for this resource. If you pass a block, it will be evaluated in the controller.
Example:
ActiveAdmin.register Post do
controller do
def some_method_on_controller
# Method gets added to Admin::PostsController
end
end
end
73 74 75 76 |
# File 'lib/active_admin/dsl.rb', line 73 def controller(&block) @config.controller.class_exec(&block) if block_given? @config.controller end |
#define_controller_method(name, &block) ⇒ Object (protected)
178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/active_admin/dsl.rb', line 178 def define_controller_method(name, &block) if block_given? warn "Warning: method `#{name}` already defined in #{controller.name}" if controller.method_defined?(name) controller do define_method(name, &block) end elsif !controller.method_defined?(name) controller do define_method(name, Proc.new{}) end end end |
#include(mod) ⇒ Nil
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. "Help" do
"Call us for Help"
end
end
end
ActiveAdmin.register Post do
include HelpSidebar
end
54 55 56 |
# File 'lib/active_admin/dsl.rb', line 54 def include(mod) mod.included(self) end |
#menu(options = {}) ⇒ Object
Set the options that are available for the item that will be placed in the global navigation of the menu.
127 128 129 |
# File 'lib/active_admin/dsl.rb', line 127 def ( = {}) config. = end |
#navigation_menu(menu_name = nil, &block) ⇒ Object
Set the name of the navigation menu to display. This is mainly used in conjunction 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
140 141 142 |
# File 'lib/active_admin/dsl.rb', line 140 def ( = nil, &block) config. = || block end |
#run_registration_block(&block) ⇒ Object
Runs the registration block inside this object
14 15 16 |
# File 'lib/active_admin/dsl.rb', line 14 def run_registration_block(&block) instance_exec &block if block_given? end |
#set_title_before_action(name, title) ⇒ Object (protected)
172 173 174 175 176 |
# File 'lib/active_admin/dsl.rb', line 172 def set_title_before_action(name, title) controller do before_action(only: [name]) { @page_title = title } end end |
#sidebar(name, options = {}, &block) ⇒ Object
162 163 164 165 166 167 168 |
# File 'lib/active_admin/dsl.rb', line 162 def (name, = {}, &block) if block_given? config. << ActiveAdmin::SidebarSection.new(name, , &block) else config.(name, ) end end |