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, #page, #resource

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Associative

#associated_class, #display_associated_resource

Methods inherited from Base

#html_class, html_class, #initialize, #name, searchable?, #to_partial_path, with_options

Constructor Details

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

Class Method Details

.permitted_attribute(attr, _options = nil) ⇒ Object



10
11
12
# File 'lib/administrate/field/has_many.rb', line 10

def self.permitted_attribute(attr, _options = nil)
  { "#{attr.to_s.singularize}_ids".to_sym => [] }
end

Instance Method Details

#associated_collection(order = self.order) ⇒ Object



14
15
16
# File 'lib/administrate/field/has_many.rb', line 14

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

#associated_resource_optionsObject



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

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

#attribute_keyObject



18
19
20
# File 'lib/administrate/field/has_many.rb', line 18

def attribute_key
  permitted_attribute.keys.first
end

#dataObject



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

def data
  @data ||= associated_class.none
end

#limitObject



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

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

#more_than_limit?Boolean

Returns:



47
48
49
# File 'lib/administrate/field/has_many.rb', line 47

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

#orderObject



62
63
64
# File 'lib/administrate/field/has_many.rb', line 62

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

#order_from_params(params) ⇒ Object



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

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

#permitted_attributeObject



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

def permitted_attribute
  self.class.permitted_attribute(attribute)
end

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



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

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

#selected_optionsObject



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

def selected_options
  return if data.empty?

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