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