Class: Administrate::Field::HasMany
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, association_primary_key_for, #display_associated_resource, foreign_key_for, #html_controller, reflection
Methods inherited from Base
associative?, eager_load?, field_type, html_class, #html_class, #html_controller, #initialize, #name, #read_value, #required?, searchable?, #to_partial_path, with_options
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 = {})
{"#{attr.to_s.singularize}_ids": []}
end
|
Instance Method Details
#associated_collection(order = self.order) ⇒ Object
24
25
26
27
28
29
30
|
# File 'lib/administrate/field/has_many.rb', line 24
def associated_collection(order = self.order)
Administrate::Page::Collection.new(
associated_dashboard,
order: order,
collection_attributes: options[:collection_attributes]
)
end
|
#associated_resource_options ⇒ Object
36
37
38
39
40
41
42
43
|
# File 'lib/administrate/field/has_many.rb', line 36
def associated_resource_options
candidate_resources.map do |associated_resource|
[
display_candidate_resource(associated_resource),
associated_resource.send(association_primary_key)
]
end
end
|
#attribute_key ⇒ Object
32
33
34
|
# File 'lib/administrate/field/has_many.rb', line 32
def attribute_key
permitted_attribute.keys.first
end
|
#data ⇒ Object
78
79
80
|
# File 'lib/administrate/field/has_many.rb', line 78
def data
@data ||= associated_class.none
end
|
#limit ⇒ Object
51
52
53
|
# File 'lib/administrate/field/has_many.rb', line 51
def limit
options.fetch(:limit, DEFAULT_LIMIT)
end
|
#more_than_limit? ⇒ Boolean
74
75
76
|
# File 'lib/administrate/field/has_many.rb', line 74
def more_than_limit?
paginate? && data.count(:all) > limit
end
|
#order ⇒ Object
89
90
91
|
# File 'lib/administrate/field/has_many.rb', line 89
def order
@order ||= Administrate::Order.new(sort_by, direction)
end
|
#order_from_params(params) ⇒ Object
82
83
84
85
86
87
|
# File 'lib/administrate/field/has_many.rb', line 82
def order_from_params(params)
Administrate::Order.new(
params.fetch(:order, sort_by),
params.fetch(:direction, direction)
)
end
|
55
56
57
|
# File 'lib/administrate/field/has_many.rb', line 55
def paginate?
limit.respond_to?(:positive?) ? limit.positive? : limit.present?
end
|
#permitted_attribute ⇒ Object
59
60
61
62
63
64
|
# File 'lib/administrate/field/has_many.rb', line 59
def permitted_attribute
self.class.permitted_attribute(
attribute,
resource_class: resource.class
)
end
|
#resources(page = 1, order = self.order) ⇒ Object
66
67
68
69
70
71
72
|
# File 'lib/administrate/field/has_many.rb', line 66
def resources(page = 1, order = self.order)
resources = order.apply(data)
if paginate?
resources = resources.page(page).per(limit)
end
includes.any? ? resources.includes(*includes) : resources
end
|
#selected_options ⇒ Object
45
46
47
48
49
|
# File 'lib/administrate/field/has_many.rb', line 45
def selected_options
return if data.empty?
data.map { |object| object.send(association_primary_key) }
end
|