Class: Ransack::Nodes::Grouping
- Inherits:
-
Node
- Object
- Node
- Ransack::Nodes::Grouping
show all
- Defined in:
- lib/ransack/nodes/grouping.rb
Instance Attribute Summary collapse
Attributes inherited from Node
#context
Instance Method Summary
collapse
Methods inherited from Node
i18n_alias, i18n_word
Constructor Details
#initialize(context, combinator = nil) ⇒ Grouping
Returns a new instance of Grouping.
14
15
16
17
|
# File 'lib/ransack/nodes/grouping.rb', line 14
def initialize(context, combinator = nil)
super(context)
self.combinator = combinator.to_s if combinator
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_id, *args) ⇒ Object
110
111
112
113
114
115
116
117
118
119
120
121
122
|
# File 'lib/ransack/nodes/grouping.rb', line 110
def method_missing(method_id, *args)
method_name = method_id.to_s.dup
writer = method_name.sub!(/\=$/, ''.freeze)
if attribute_method?(method_name)
if writer
write_attribute(method_name, *args)
else
read_attribute(method_name)
end
else
super
end
end
|
Instance Attribute Details
#combinator ⇒ Object
Also known as:
m
Returns the value of attribute combinator.
5
6
7
|
# File 'lib/ransack/nodes/grouping.rb', line 5
def combinator
@combinator
end
|
#conditions ⇒ Object
Also known as:
c
Returns the value of attribute conditions.
4
5
6
|
# File 'lib/ransack/nodes/grouping.rb', line 4
def conditions
@conditions
end
|
Instance Method Details
#[](key) ⇒ Object
51
52
53
|
# File 'lib/ransack/nodes/grouping.rb', line 51
def [](key)
conditions.detect { |c| c.key == key.to_s }
end
|
#[]=(key, value) ⇒ Object
55
56
57
58
|
# File 'lib/ransack/nodes/grouping.rb', line 55
def []=(key, value)
conditions.reject! { |c| c.key == key.to_s }
self.conditions << value
end
|
#attribute_method?(name) ⇒ Boolean
124
125
126
127
128
129
130
131
132
133
134
135
136
|
# File 'lib/ransack/nodes/grouping.rb', line 124
def attribute_method?(name)
stripped_name = strip_predicate_and_index(name)
return true if @context.attribute_method?(stripped_name) ||
@context.attribute_method?(name)
case stripped_name
when /^(g|c|m|groupings|conditions|combinator)=?$/
true
else
stripped_name
.split(/_and_|_or_/)
.none? { |n| !@context.attribute_method?(n) }
end
end
|
#build(params) ⇒ Object
149
150
151
152
153
154
155
156
157
158
159
|
# File 'lib/ransack/nodes/grouping.rb', line 149
def build(params)
params.with_indifferent_access.each do |key, value|
case key
when /^(g|c|m)$/
self.send("#{key}=", value)
else
write_attribute(key.to_s, value)
end
end
self
end
|
#build_condition(opts = {}) ⇒ Object
71
72
73
74
75
|
# File 'lib/ransack/nodes/grouping.rb', line 71
def build_condition(opts = {})
new_condition(opts).tap do |condition|
self.conditions << condition
end
end
|
#build_grouping(params = {}) ⇒ Object
138
139
140
141
142
143
|
# File 'lib/ransack/nodes/grouping.rb', line 138
def build_grouping(params = {})
params ||= {}
new_grouping(params).tap do |new_grouping|
self.groupings << new_grouping
end
end
|
#groupings ⇒ Object
Also known as:
g
86
87
88
|
# File 'lib/ransack/nodes/grouping.rb', line 86
def groupings
@groupings ||= []
end
|
#groupings=(groupings) ⇒ Object
Also known as:
g=
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
# File 'lib/ransack/nodes/grouping.rb', line 91
def groupings=(groupings)
case groupings
when Array
groupings.each do |attrs|
grouping_object = new_grouping(attrs)
self.groupings << grouping_object if grouping_object.values.any?
end
when Hash
groupings.each do |index, attrs|
grouping_object = new_grouping(attrs)
self.groupings << grouping_object if grouping_object.values.any?
end
else
raise ArgumentError,
"Invalid argument (#{groupings.class}) supplied to groupings="
end
end
|
#inspect ⇒ Object
161
162
163
164
165
166
167
168
169
|
# File 'lib/ransack/nodes/grouping.rb', line 161
def inspect
data = [
['conditions'.freeze, conditions], [Constants::COMBINATOR, combinator]
]
.reject { |e| e[1].blank? }
.map { |v| "#{v[0]}: #{v[1]}" }
.join(', '.freeze)
"Grouping <#{data}>"
end
|
#new_condition(opts = {}) ⇒ Object
77
78
79
80
81
82
83
84
|
# File 'lib/ransack/nodes/grouping.rb', line 77
def new_condition(opts = {})
attrs = opts[:attributes] || 1
vals = opts[:values] || 1
condition = Condition.new(@context)
attrs.times { condition.build_attribute }
vals.times { condition.build_value }
condition
end
|
#new_grouping(params = {}) ⇒ Object
145
146
147
|
# File 'lib/ransack/nodes/grouping.rb', line 145
def new_grouping(params = {})
Grouping.new(@context).build(params)
end
|
#persisted? ⇒ Boolean
19
20
21
|
# File 'lib/ransack/nodes/grouping.rb', line 19
def persisted?
false
end
|
#respond_to?(method_id) ⇒ Boolean
64
65
66
67
68
69
|
# File 'lib/ransack/nodes/grouping.rb', line 64
def respond_to?(method_id)
super or begin
method_name = method_id.to_s
attribute_method?(method_name) ? true : false
end
end
|
#translate(key, options = {}) ⇒ Object
23
24
25
26
27
|
# File 'lib/ransack/nodes/grouping.rb', line 23
def translate(key, options = {})
super or Translate.attribute(
key.to_s, options.merge(context: context)
)
end
|
#values ⇒ Object
60
61
62
|
# File 'lib/ransack/nodes/grouping.rb', line 60
def values
conditions + groupings
end
|