Class: ActiveAdmin::Resource
- Defined in:
- lib/active_admin/resource.rb,
lib/active_admin/resource/belongs_to.rb
Overview
Resource is the primary data storage for resource configuration in Active Admin
When you register a resource (ActiveAdmin.register Post) you are actually creating a new Resource instance within the given Namespace.
The instance of the current resource is available in ResourceController and views by calling the #active_admin_config method.
Defined Under Namespace
Classes: BelongsTo
Constant Summary collapse
- RegisterEvent =
Event dispatched when a new resource is registered
'active_admin.resource.register'.freeze
Instance Attribute Summary collapse
-
#admin_notes ⇒ Object
Set to false to turn off admin notes.
-
#collection_actions ⇒ Object
readonly
An array of collection actions defined for this resource.
-
#csv_builder ⇒ Object
The csv builder for this resource.
-
#member_actions ⇒ Object
readonly
An array of member actions defined for this resource.
-
#namespace ⇒ Object
readonly
The namespace this resource belongs to.
-
#page_configs ⇒ Object
readonly
A hash of page configurations for the controller indexed by action name.
-
#resource ⇒ Object
readonly
The class this resource wraps.
-
#resource_name ⇒ Object
Returns the name to call this resource.
-
#scope_to ⇒ Object
Scope this resource to an association in the controller.
-
#scope_to_association_method ⇒ Object
If we’re scoping resources, use this method on the parent to return the collection.
-
#sort_order ⇒ Object
The default sort order to use in the controller.
Instance Method Summary collapse
-
#admin_notes? ⇒ Boolean
Are admin notes turned on for this resource.
- #belongs_to(target, options = {}) ⇒ Object
-
#belongs_to? ⇒ Boolean
Do we belong to another resource.
- #belongs_to_config ⇒ Object
-
#camelized_resource_name ⇒ Object
A camelized safe representation for this resource.
- #clear_collection_actions! ⇒ Object
-
#clear_member_actions! ⇒ Object
Clears all the member actions this resource knows about.
-
#controller ⇒ Object
Returns the controller for this resource.
-
#controller_name ⇒ Object
Returns a properly formatted controller name for this resource within its namespace.
- #default_scope ⇒ Object
-
#get_scope_by_id(id) ⇒ Object
Returns a scope for this object by its identifier.
-
#include_in_menu? ⇒ Boolean
Should this resource be added to the menu system?.
-
#initialize(namespace, resource, options = {}) ⇒ Resource
constructor
A new instance of Resource.
-
#menu(options = {}) ⇒ Object
Set the menu options.
-
#menu_item_display_if ⇒ Object
Returns a proc for deciding whether to display the menu item or not in the view.
-
#menu_item_name ⇒ Object
Returns the name to be displayed in the menu for this resource.
-
#menu_item_priority ⇒ Object
Returns the items priority for altering the default sort order.
-
#parent_menu_item_name ⇒ Object
Returns the name to put this resource under in the menu.
-
#plural_resource_name ⇒ Object
Returns the plural version of this resource.
- #resource_table_name ⇒ Object
-
#route_collection_path ⇒ Object
Returns a symbol for the route to use to get to the collection of this resource.
-
#route_instance_path ⇒ Object
Returns the named route for an instance of this resource.
-
#route_prefix ⇒ Object
Returns the routes prefix for this resource.
-
#scope(*args, &block) ⇒ Object
Create a new scope object for this resource.
-
#scopes ⇒ Object
Return an array of scopes for this resource.
-
#underscored_resource_name ⇒ Object
An underscored safe representation internally for this resource.
Constructor Details
#initialize(namespace, resource, options = {}) ⇒ Resource
Returns a new instance of Resource.
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/active_admin/resource.rb', line 52 def initialize(namespace, resource, = {}) @namespace = namespace @resource = resource @options = .merge() @sort_order = @options[:sort_order] @page_configs = {} @menu_options = {} @member_actions, @collection_actions = [], [] @scopes = [] end |
Instance Attribute Details
#admin_notes ⇒ Object
Set to false to turn off admin notes
47 48 49 |
# File 'lib/active_admin/resource.rb', line 47 def admin_notes @admin_notes end |
#collection_actions ⇒ Object (readonly)
An array of collection actions defined for this resource
32 33 34 |
# File 'lib/active_admin/resource.rb', line 32 def collection_actions @collection_actions end |
#csv_builder ⇒ Object
The csv builder for this resource
212 213 214 |
# File 'lib/active_admin/resource.rb', line 212 def csv_builder @csv_builder || default_csv_builder end |
#member_actions ⇒ Object (readonly)
An array of member actions defined for this resource
29 30 31 |
# File 'lib/active_admin/resource.rb', line 29 def member_actions @member_actions end |
#namespace ⇒ Object (readonly)
The namespace this resource belongs to
19 20 21 |
# File 'lib/active_admin/resource.rb', line 19 def namespace @namespace end |
#page_configs ⇒ Object (readonly)
A hash of page configurations for the controller indexed by action name
26 27 28 |
# File 'lib/active_admin/resource.rb', line 26 def page_configs @page_configs end |
#resource ⇒ Object (readonly)
The class this resource wraps. If you register the Post model, Resource#resource will point to the Post class
23 24 25 |
# File 'lib/active_admin/resource.rb', line 23 def resource @resource end |
#resource_name ⇒ Object
Returns the name to call this resource. By default will use resource.model_name.human
35 36 37 |
# File 'lib/active_admin/resource.rb', line 35 def resource_name @resource_name end |
#scope_to ⇒ Object
Scope this resource to an association in the controller
41 42 43 |
# File 'lib/active_admin/resource.rb', line 41 def scope_to @scope_to end |
#scope_to_association_method ⇒ Object
If we’re scoping resources, use this method on the parent to return the collection
44 45 46 |
# File 'lib/active_admin/resource.rb', line 44 def scope_to_association_method @scope_to_association_method end |
#sort_order ⇒ Object
The default sort order to use in the controller
38 39 40 |
# File 'lib/active_admin/resource.rb', line 38 def sort_order @sort_order end |
Instance Method Details
#admin_notes? ⇒ Boolean
Are admin notes turned on for this resource
193 194 195 |
# File 'lib/active_admin/resource.rb', line 193 def admin_notes? admin_notes.nil? ? ActiveAdmin.admin_notes : admin_notes end |
#belongs_to(target, options = {}) ⇒ Object
197 198 199 200 |
# File 'lib/active_admin/resource.rb', line 197 def belongs_to(target, = {}) @belongs_to = Resource::BelongsTo.new(self, target, ) controller.belongs_to(target, .dup) end |
#belongs_to? ⇒ Boolean
Do we belong to another resource
207 208 209 |
# File 'lib/active_admin/resource.rb', line 207 def belongs_to? !belongs_to_config.nil? end |
#belongs_to_config ⇒ Object
202 203 204 |
# File 'lib/active_admin/resource.rb', line 202 def belongs_to_config @belongs_to end |
#camelized_resource_name ⇒ Object
A camelized safe representation for this resource
73 74 75 |
# File 'lib/active_admin/resource.rb', line 73 def camelized_resource_name underscored_resource_name.camelize end |
#clear_collection_actions! ⇒ Object
162 163 164 |
# File 'lib/active_admin/resource.rb', line 162 def clear_collection_actions! @collection_actions = [] end |
#clear_member_actions! ⇒ Object
Clears all the member actions this resource knows about
158 159 160 |
# File 'lib/active_admin/resource.rb', line 158 def clear_member_actions! @member_actions = [] end |
#controller ⇒ Object
Returns the controller for this resource
103 104 105 |
# File 'lib/active_admin/resource.rb', line 103 def controller @controller ||= controller_name.constantize end |
#controller_name ⇒ Object
Returns a properly formatted controller name for this resource within its namespace
98 99 100 |
# File 'lib/active_admin/resource.rb', line 98 def controller_name [namespace.module_name, camelized_resource_name.pluralize + "Controller"].compact.join('::') end |
#default_scope ⇒ Object
177 178 179 |
# File 'lib/active_admin/resource.rb', line 177 def default_scope @default_scope end |
#get_scope_by_id(id) ⇒ Object
Returns a scope for this object by its identifier
172 173 174 175 |
# File 'lib/active_admin/resource.rb', line 172 def get_scope_by_id(id) id = id.to_s @scopes.find{|s| s.id == id } end |
#include_in_menu? ⇒ Boolean
Should this resource be added to the menu system?
151 152 153 154 |
# File 'lib/active_admin/resource.rb', line 151 def return false if @menu_options[:display] == false !(belongs_to? && !belongs_to_config.optional?) end |
#menu(options = {}) ⇒ Object
Set the menu options. To not add this resource to the menu, just call #menu(false)
125 126 127 128 |
# File 'lib/active_admin/resource.rb', line 125 def ( = {}) = == false ? { :display => false } : @menu_options = end |
#menu_item_display_if ⇒ Object
Returns a proc for deciding whether to display the menu item or not in the view
146 147 148 |
# File 'lib/active_admin/resource.rb', line 146 def @menu_options[:if] || proc { true } end |
#menu_item_name ⇒ Object
Returns the name to be displayed in the menu for this resource
136 137 138 |
# File 'lib/active_admin/resource.rb', line 136 def @menu_options[:label] || plural_resource_name end |
#menu_item_priority ⇒ Object
Returns the items priority for altering the default sort order
141 142 143 |
# File 'lib/active_admin/resource.rb', line 141 def @menu_options[:priority] || 10 end |
#parent_menu_item_name ⇒ Object
Returns the name to put this resource under in the menu
131 132 133 |
# File 'lib/active_admin/resource.rb', line 131 def @menu_options[:parent] end |
#plural_resource_name ⇒ Object
Returns the plural version of this resource
88 89 90 |
# File 'lib/active_admin/resource.rb', line 88 def plural_resource_name @plural_resource_name ||= resource_name.pluralize end |
#resource_table_name ⇒ Object
92 93 94 |
# File 'lib/active_admin/resource.rb', line 92 def resource_table_name resource.table_name end |
#route_collection_path ⇒ Object
Returns a symbol for the route to use to get to the collection of this resource
114 115 116 |
# File 'lib/active_admin/resource.rb', line 114 def route_collection_path [route_prefix, controller.resources_configuration[:self][:route_collection_name], 'path'].compact.join('_').to_sym end |
#route_instance_path ⇒ Object
Returns the named route for an instance of this resource
119 120 121 |
# File 'lib/active_admin/resource.rb', line 119 def route_instance_path [route_prefix, controller.resources_configuration[:self][:route_instance_name], 'path'].compact.join('_').to_sym end |
#route_prefix ⇒ Object
Returns the routes prefix for this resource
108 109 110 |
# File 'lib/active_admin/resource.rb', line 108 def route_prefix namespace.module_name.try(:underscore) end |
#scope(*args, &block) ⇒ Object
Create a new scope object for this resource. If you want to internationalize the scope name, you can add to your i18n files a key like “active_admin.scopes.scope_method”.
184 185 186 187 188 189 190 |
# File 'lib/active_admin/resource.rb', line 184 def scope(*args, &block) = args. @scopes << ActiveAdmin::Scope.new(*args, &block) if [:default] @default_scope = @scopes.last end end |
#scopes ⇒ Object
Return an array of scopes for this resource
167 168 169 |
# File 'lib/active_admin/resource.rb', line 167 def scopes @scopes end |
#underscored_resource_name ⇒ Object
An underscored safe representation internally for this resource
64 65 66 67 68 69 70 |
# File 'lib/active_admin/resource.rb', line 64 def underscored_resource_name @underscored_resource_name ||= if @options[:as] @options[:as].gsub(' ', '').underscore.singularize else resource.name.gsub('::','').underscore end end |