Class: Headmin::Filter::Association
- Inherits:
-
Base
- Object
- Base
- Headmin::Filter::Association
show all
- Defined in:
- app/models/headmin/filter/association.rb
Constant Summary
collapse
- OPERATORS =
%w[in not_in]
Constants inherited
from Base
Base::OPERATORS_CONVERT_TO, Base::QUERY_OPERATOR_CONVERT_TO, Base::QUERY_VALUE_CONVERT_TO
Instance Attribute Summary
Attributes inherited from Base
#attribute, #instructions, #raw_value
Instance Method Summary
collapse
Methods inherited from Base
#conditionals, #initialize, #is_i?, #operators, #string, #values
Instance Method Details
#association_class ⇒ Object
53
54
55
|
# File 'app/models/headmin/filter/association.rb', line 53
def association_class
reflection.klass
end
|
#association_column ⇒ Object
61
62
63
64
65
66
67
|
# File 'app/models/headmin/filter/association.rb', line 61
def association_column
if has_many?
association_class.arel_table[:id]
else
@parent_class.arel_table[foreign_key]
end
end
|
#build_query(query, collection, instruction) ⇒ Object
27
28
29
30
31
32
33
34
35
|
# File 'app/models/headmin/filter/association.rb', line 27
def build_query(query, collection, instruction)
query_operator = convert_to_query_operator(instruction[:operator])
query_value = convert_to_query_value(instruction[:value], instruction[:operator])
query_operator, query_value = process_null_operators(query_operator, query_value)
new_query = association_column.send(query_operator, query_value)
query ? query.send(instruction[:conditional], new_query) : new_query
end
|
#cast_value(value) ⇒ Object
6
7
8
|
# File 'app/models/headmin/filter/association.rb', line 6
def cast_value(value)
is_i?(value) ? value.to_i : 0
end
|
#display_value(value) ⇒ Object
37
38
39
40
41
42
43
|
# File 'app/models/headmin/filter/association.rb', line 37
def display_value(value)
if value.is_a? Array
value.first.to_s
else
value
end
end
|
#foreign_key ⇒ Object
57
58
59
|
# File 'app/models/headmin/filter/association.rb', line 57
def foreign_key
reflection.foreign_key
end
|
69
70
71
|
# File 'app/models/headmin/filter/association.rb', line 69
def has_many?
macro == :has_many
end
|
#macro ⇒ Object
49
50
51
|
# File 'app/models/headmin/filter/association.rb', line 49
def macro
reflection.macro
end
|
#query(collection) ⇒ Object
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
# File 'app/models/headmin/filter/association.rb', line 10
def query(collection)
return collection unless @instructions.any?
@parent_class = collection.is_a?(Class) ? collection : collection.klass
collection = collection.joins(attribute) if has_many?
query = nil
@instructions.each do |instruction|
query = build_query(query, collection, instruction)
end
collection.where(query)
end
|
#reflection ⇒ Object
45
46
47
|
# File 'app/models/headmin/filter/association.rb', line 45
def reflection
@parent_class.reflect_on_association(attribute)
end
|