Class: ActiveAdmin::Resource

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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, options = {})
  @namespace = namespace
  @resource = resource
  @options = default_options.merge(options)
  @sort_order = @options[:sort_order]
  @page_configs = {}
  @menu_options = {}
  @member_actions, @collection_actions = [], []
  @scopes = []
end

Instance Attribute Details

#admin_notesObject

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_actionsObject (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_builderObject

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_actionsObject (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

#namespaceObject (readonly)

The namespace this resource belongs to



19
20
21
# File 'lib/active_admin/resource.rb', line 19

def namespace
  @namespace
end

#page_configsObject (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

#resourceObject (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_nameObject

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_toObject

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_methodObject

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_orderObject

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

Returns:

  • (Boolean)


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, options = {})
  @belongs_to = Resource::BelongsTo.new(self, target, options)
  controller.belongs_to(target, options.dup)
end

#belongs_to?Boolean

Do we belong to another resource

Returns:

  • (Boolean)


207
208
209
# File 'lib/active_admin/resource.rb', line 207

def belongs_to?
  !belongs_to_config.nil?
end

#belongs_to_configObject



202
203
204
# File 'lib/active_admin/resource.rb', line 202

def belongs_to_config
  @belongs_to
end

#camelized_resource_nameObject

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

#controllerObject

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_nameObject

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_scopeObject



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?

Returns:

  • (Boolean)


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

def include_in_menu?
  return false if @menu_options[:display] == false
  !(belongs_to? && !belongs_to_config.optional?)
end

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 menu(options = {})
  options = options == false ? { :display => false } : options
  @menu_options = options
end

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_item_display_if
  @menu_options[:if] || proc { true }
end

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_item_name
  @menu_options[:label] || plural_resource_name
end

Returns the items priority for altering the default sort order



141
142
143
# File 'lib/active_admin/resource.rb', line 141

def menu_item_priority
  @menu_options[:priority] || 10
end

#parent_menu_item_nameObject

Returns the name to put this resource under in the menu



131
132
133
# File 'lib/active_admin/resource.rb', line 131

def parent_menu_item_name
  @menu_options[:parent]
end

#plural_resource_nameObject

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_nameObject



92
93
94
# File 'lib/active_admin/resource.rb', line 92

def resource_table_name
  resource.table_name
end

#route_collection_pathObject

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_pathObject

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_prefixObject

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)
  options = args.extract_options!
  @scopes << ActiveAdmin::Scope.new(*args, &block)
  if options[:default]
    @default_scope = @scopes.last
  end
end

#scopesObject

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_nameObject

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