Class: ActiveAdmin::Resource

Inherits:
Object
  • Object
show all
Includes:
Authorization, ActionItems, Attributes, Base, Controllers, Includes, Menu, Naming, Ordering, PagePresenters, Pagination, Routes, ScopeTo, Scopes, Sidebars
Defined in:
lib/active_admin/resource.rb,
lib/active_admin/resource/menu.rb,
lib/active_admin/resource/naming.rb,
lib/active_admin/resource/routes.rb,
lib/active_admin/resource/scopes.rb,
lib/active_admin/resource/includes.rb,
lib/active_admin/resource/ordering.rb,
lib/active_admin/resource/scope_to.rb,
lib/active_admin/resource/sidebars.rb,
lib/active_admin/resource/attributes.rb,
lib/active_admin/resource/belongs_to.rb,
lib/active_admin/resource/pagination.rb,
lib/active_admin/resource/controllers.rb,
lib/active_admin/resource/action_items.rb,
lib/active_admin/resource/page_presenters.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

Modules: ActionItems, Attributes, Base, Controllers, Includes, Menu, Naming, Ordering, PagePresenters, Pagination, Routes, ScopeTo, Scopes, Sidebars Classes: BelongsTo, Name

Constant Summary collapse

RegisterEvent =

Event dispatched when a new resource is registered

'active_admin.resource.register'.freeze

Constants included from Authorization

Authorization::CREATE, Authorization::DESTROY, Authorization::READ, Authorization::UPDATE

Instance Attribute Summary collapse

Attributes included from Pagination

#max_per_page, #paginate, #per_page

Attributes included from Menu

#menu_item

Instance Method Summary collapse

Methods included from Attributes

#counter_cache_col?, #default_attributes, #filtered_col?, #foreign_methods, #method_for_column, #primary_col?, #reject_col?, #sti_col?

Methods included from Ordering

#ordering

Methods included from Routes

#route_batch_action_path, #route_builder, #route_collection_path, #route_edit_instance_path, #route_instance_path, #route_member_action_path, #route_prefix, #route_uncountable?

Methods included from Sidebars

#clear_sidebar_sections!, #sidebar_sections, #sidebar_sections?, #sidebar_sections_for

Methods included from ScopeTo

#scope_to, #scope_to?, #scope_to_association_method, #scope_to_config, #scope_to_method

Methods included from Includes

#includes

Methods included from Scopes

#default_scope, #get_scope_by_id, #scope, #scopes

Methods included from Pagination

#initialize

Methods included from PagePresenters

#default_index_class, #find_index_class, #get_page_presenter, #page_presenters, #set_index_presenter, #set_page_presenter

Methods included from Naming

#param_key, #plural_resource_label, #resource_label, #resource_name

Methods included from Menu

#add_to_menu, #default_menu_options, #include_in_menu?, #menu_item_options, #menu_item_options=, #navigation_menu, #navigation_menu_name, #navigation_menu_name=

Methods included from Controllers

#controller, #controller_name

Methods included from ActionItems

#action_items, #action_items?, #action_items_for, #add_action_item, #clear_action_items!, #initialize, #remove_action_item

Methods included from Base

#initialize

Instance Attribute Details


165
166
167
# File 'lib/active_admin/resource.rb', line 165

def breadcrumb
  instance_variable_defined?(:@breadcrumb) ? @breadcrumb : namespace.breadcrumb
end

#collection_actionsObject (readonly)

An array of collection actions defined for this resource


42
43
44
# File 'lib/active_admin/resource.rb', line 42

def collection_actions
  @collection_actions
end

#create_anotherObject


173
174
175
# File 'lib/active_admin/resource.rb', line 173

def create_another
  instance_variable_defined?(:@create_another) ? @create_another : namespace.create_another
end

#csv_builderObject

The csv builder for this resource


161
162
163
# File 'lib/active_admin/resource.rb', line 161

def csv_builder
  @csv_builder || default_csv_builder
end

#decorator_class_nameObject

The string identifying a class to decorate our resource with for the view. nil to not decorate.


67
68
69
# File 'lib/active_admin/resource.rb', line 67

def decorator_class_name
  @decorator_class_name
end

#dslObject

Store a reference to the DSL so that we can dereference it during garbage collection.


63
64
65
# File 'lib/active_admin/resource.rb', line 63

def dsl
  @dsl
end

#member_actionsObject (readonly)

An array of member actions defined for this resource


39
40
41
# File 'lib/active_admin/resource.rb', line 39

def member_actions
  @member_actions
end

#namespaceObject (readonly)

The namespace this config belongs to


33
34
35
# File 'lib/active_admin/resource.rb', line 33

def namespace
  @namespace
end

#order_clauseObject


169
170
171
# File 'lib/active_admin/resource.rb', line 169

def order_clause
  @order_clause || namespace.order_clause
end

#resource_class_nameObject (readonly)

The name of the resource class


36
37
38
# File 'lib/active_admin/resource.rb', line 36

def resource_class_name
  @resource_class_name
end

#sort_orderObject


46
47
48
# File 'lib/active_admin/resource.rb', line 46

def sort_order
  @sort_order ||= (resource_class.respond_to?(:primary_key) ? resource_class.primary_key.to_s : 'id') + '_desc'
end

Instance Method Details

#association_columnsObject


190
191
192
# File 'lib/active_admin/resource.rb', line 190

def association_columns
  @association_columns ||= resource_attributes.select { |key, value| key != value }.values
end

#belongs_to(target, options = {}) ⇒ Object


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

def belongs_to(target, options = {})
  @belongs_to = Resource::BelongsTo.new(self, target, options)
  self.menu_item_options = false if @belongs_to.required?
  options[:class_name] ||= @belongs_to.resource.resource_class_name if @belongs_to.resource
  controller.send :belongs_to, target, options.dup
end

#belongs_to?Boolean

Do we belong to another resource?

Returns:

  • (Boolean)

156
157
158
# File 'lib/active_admin/resource.rb', line 156

def belongs_to?
  !!belongs_to_config
end

#belongs_to_configObject


145
146
147
# File 'lib/active_admin/resource.rb', line 145

def belongs_to_config
  @belongs_to
end

#belongs_to_paramObject


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

def belongs_to_param
  if belongs_to? && belongs_to_config.required?
    belongs_to_config.to_param
  end
end

#clear_collection_actions!Object


129
130
131
# File 'lib/active_admin/resource.rb', line 129

def clear_collection_actions!
  @collection_actions = []
end

#clear_member_actions!Object

Clears all the member actions this resource knows about


125
126
127
# File 'lib/active_admin/resource.rb', line 125

def clear_member_actions!
  @member_actions = []
end

#content_columnsObject


194
195
196
# File 'lib/active_admin/resource.rb', line 194

def content_columns
  @content_columns ||= resource_attributes.select { |key, value| key == value }.values
end

#decorator_classObject


104
105
106
# File 'lib/active_admin/resource.rb', line 104

def decorator_class
  ActiveSupport::Dependencies.constantize(decorator_class_name) if decorator_class_name
end

#defined_actionsObject

Return only defined resource actions


134
135
136
# File 'lib/active_admin/resource.rb', line 134

def defined_actions
  controller.instance_methods.map(&:to_sym) & ResourceController::ACTIVE_ADMIN_ACTIONS
end

#find_resource(id) ⇒ Object


177
178
179
180
# File 'lib/active_admin/resource.rb', line 177

def find_resource(id)
  resource = resource_class.public_send *method_for_find(id)
  (decorator_class && resource) ? decorator_class.new(resource) : resource
end

#resource_attributesObject


186
187
188
# File 'lib/active_admin/resource.rb', line 186

def resource_attributes
  @resource_attributes ||= default_attributes
end

#resource_classObject

The class this resource wraps. If you register the Post model, Resource#resource_class will point to the Post class


100
101
102
# File 'lib/active_admin/resource.rb', line 100

def resource_class
  ActiveSupport::Dependencies.constantize(resource_class_name)
end

#resource_column_namesObject


116
117
118
# File 'lib/active_admin/resource.rb', line 116

def resource_column_names
  resource_class.column_names
end

#resource_columnsObject


182
183
184
# File 'lib/active_admin/resource.rb', line 182

def resource_columns
  resource_attributes.values
end

#resource_name_extensionObject


108
109
110
# File 'lib/active_admin/resource.rb', line 108

def resource_name_extension
  @resource_name_extension ||= define_resource_name_extension(self)
end

#resource_quoted_column_name(column) ⇒ Object


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

def resource_quoted_column_name(column)
  resource_class.connection.quote_column_name(column)
end

#resource_table_nameObject


112
113
114
# File 'lib/active_admin/resource.rb', line 112

def resource_table_name
  resource_class.quoted_table_name
end