Class: Administrate::Field::HasMany

Inherits:
Associative show all
Defined in:
lib/administrate/field/has_many.rb

Constant Summary collapse

DEFAULT_LIMIT =
5

Instance Attribute Summary

Attributes inherited from Base

#attribute, #options, #page, #resource

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Associative

#associated_class, associated_class, associated_class_name, #associated_class_name, #display_associated_resource, foreign_key_for, reflection

Methods inherited from Base

associative?, eager_load?, field_type, html_class, #html_class, #initialize, #name, #required?, searchable?, #to_partial_path, with_options

Constructor Details

This class inherits a constructor from Administrate::Field::Base

Class Method Details

.permitted_attribute(attr, _options = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/administrate/field/has_many.rb', line 10

def self.permitted_attribute(attr, _options = {})
  # This may seem arbitrary, and improvable by using reflection.
  # Worry not: here we do exactly what Rails does. Regardless of the name
  # of the foreign key, has_many associations use the suffix `_ids`
  # for this.
  #
  # Eg: if the associated table and primary key are `countries.code`,
  # you may expect `country_codes` as attribute here, but it will
  # be `country_ids` instead.
  #
  # See https://github.com/rails/rails/blob/b30a23f53b52e59d31358f7b80385ee5c2ba3afe/activerecord/lib/active_record/associations/builder/collection_association.rb#L48
  { "#{attr.to_s.singularize}_ids".to_sym => [] }
end

Instance Method Details

#associated_collection(order = self.order) ⇒ Object



24
25
26
# File 'lib/administrate/field/has_many.rb', line 24

def associated_collection(order = self.order)
  Administrate::Page::Collection.new(associated_dashboard, order: order)
end

#associated_resource_optionsObject



32
33
34
35
36
# File 'lib/administrate/field/has_many.rb', line 32

def associated_resource_options
  candidate_resources.map do |resource|
    [display_candidate_resource(resource), resource.send(primary_key)]
  end
end

#attribute_keyObject



28
29
30
# File 'lib/administrate/field/has_many.rb', line 28

def attribute_key
  permitted_attribute.keys.first
end

#dataObject



64
65
66
# File 'lib/administrate/field/has_many.rb', line 64

def data
  @data ||= associated_class.none
end

#limitObject



44
45
46
# File 'lib/administrate/field/has_many.rb', line 44

def limit
  options.fetch(:limit, DEFAULT_LIMIT)
end

#more_than_limit?Boolean

Returns:



60
61
62
# File 'lib/administrate/field/has_many.rb', line 60

def more_than_limit?
  data.count(:all) > limit
end

#orderObject



75
76
77
# File 'lib/administrate/field/has_many.rb', line 75

def order
  @order ||= Administrate::Order.new(sort_by, direction)
end

#order_from_params(params) ⇒ Object



68
69
70
71
72
73
# File 'lib/administrate/field/has_many.rb', line 68

def order_from_params(params)
  Administrate::Order.new(
    params.fetch(:order, sort_by),
    params.fetch(:direction, direction),
  )
end

#permitted_attributeObject



48
49
50
51
52
53
# File 'lib/administrate/field/has_many.rb', line 48

def permitted_attribute
  self.class.permitted_attribute(
    attribute,
    resource_class: resource.class,
  )
end

#resources(page = 1, order = self.order) ⇒ Object



55
56
57
58
# File 'lib/administrate/field/has_many.rb', line 55

def resources(page = 1, order = self.order)
  resources = order.apply(data).page(page).per(limit)
  includes.any? ? resources.includes(*includes) : resources
end

#selected_optionsObject



38
39
40
41
42
# File 'lib/administrate/field/has_many.rb', line 38

def selected_options
  return if data.empty?

  data.map { |object| object.send(primary_key) }
end