Class: Plutonium::Query::Filters::Association
Overview
Select filter for association records
Instance Attribute Summary
#key
Instance Method Summary
collapse
lookup
#description, #icon, #label
Constructor Details
#initialize(class_name: nil, resource_class: nil, scope: nil, multiple: true) ⇒ Association
Returns a new instance of Association.
19
20
21
22
23
24
25
|
# File 'lib/plutonium/query/filters/association.rb', line 19
def initialize(class_name: nil, resource_class: nil, scope: nil, multiple: true, **)
super(**)
@class_name = class_name
@resource_class = resource_class
@scope_proc = scope
@multiple = multiple
end
|
Instance Method Details
#apply(scope, value:) ⇒ Object
37
38
39
40
41
42
|
# File 'lib/plutonium/query/filters/association.rb', line 37
def apply(scope, value:)
return scope if value.blank?
ids = decode_ids(value)
return scope if ids.empty?
scope.where("#{key}_id": ids)
end
|
44
45
46
47
48
49
50
|
# File 'lib/plutonium/query/filters/association.rb', line 44
def customize_inputs
input :value,
as: :resource_select,
association_class: association_class,
multiple: @multiple,
include_blank: @multiple ? false : "All"
end
|
#humanize_value(value) ⇒ Object
27
28
29
30
31
32
33
34
35
|
# File 'lib/plutonium/query/filters/association.rb', line 27
def humanize_value(value)
return "" if value.blank?
ids = decode_ids(value)
return "" if ids.empty?
records = association_class.where(id: ids)
records.map { |r| r.respond_to?(:to_label) ? r.to_label : r.to_s }.join(", ")
rescue
Array(value).reject(&:blank?).join(", ")
end
|