Class: Ransack::Nodes::Condition
- Inherits:
-
Node
- Object
- Node
- Ransack::Nodes::Condition
show all
- Defined in:
- lib/ransack/nodes/condition.rb
Instance Attribute Summary collapse
Attributes inherited from Node
#context
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Node
i18n_alias, i18n_word, #initialize, #translate
Instance Attribute Details
#predicate ⇒ Object
Returns the value of attribute predicate.
10
11
12
|
# File 'lib/ransack/nodes/condition.rb', line 10
def predicate
@predicate
end
|
Class Method Details
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/ransack/nodes/condition.rb', line 13
def (context, key, values)
attributes, predicate, combinator =
(key, context)
if attributes.size > 0 && predicate
condition = self.new(context)
condition.build(
a: attributes,
p: predicate.name,
m: combinator,
v: predicate.wants_array ? Array(values) : [values]
)
if predicate.validate(condition.values, condition.default_type)
condition
else
nil
end
end
end
|
Instance Method Details
#arel_predicate ⇒ Object
220
221
222
|
# File 'lib/ransack/nodes/condition.rb', line 220
def arel_predicate
raise "not implemented"
end
|
#arel_predicate_for_attribute(attr) ⇒ Object
247
248
249
250
251
252
253
254
255
256
257
|
# File 'lib/ransack/nodes/condition.rb', line 247
def arel_predicate_for_attribute(attr)
if predicate.arel_predicate === Proc
values = casted_values_for_attribute(attr)
unless predicate.wants_array
values = values.first
end
predicate.arel_predicate.call(values)
else
predicate.arel_predicate
end
end
|
#attr_value_for_attribute(attr) ⇒ Object
259
260
261
262
263
264
265
|
# File 'lib/ransack/nodes/condition.rb', line 259
def attr_value_for_attribute(attr)
return attr.attr if ActiveRecord::Base.connection.adapter_name == "PostgreSQL"
predicate.case_insensitive ? attr.attr.lower : attr.attr
rescue
attr.attr
end
|
#attributes ⇒ Object
Also known as:
a
76
77
78
|
# File 'lib/ransack/nodes/condition.rb', line 76
def attributes
@attributes ||= []
end
|
#attributes=(args) ⇒ Object
Also known as:
a=
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
# File 'lib/ransack/nodes/condition.rb', line 81
def attributes=(args)
case args
when Array
args.each do |name|
build_attribute(name)
end
when Hash
args.each do |index, attrs|
build_attribute(attrs[:name], attrs[:ransacker_args])
end
else
raise ArgumentError,
"Invalid argument (#{args.class}) supplied to attributes="
end
end
|
#build(params) ⇒ Object
174
175
176
177
178
179
180
181
182
|
# File 'lib/ransack/nodes/condition.rb', line 174
def build(params)
params.with_indifferent_access.each do |key, value|
if key.match(/^(a|v|p|m)$/)
self.send("#{key}=", value)
end
end
self
end
|
#build_attribute(name = nil, ransacker_args = []) ⇒ Object
build_attribute
This method was originally called from Nodes::Grouping#new_condition
only, without arguments, without #valid? checking, to build a new
grouping condition.
After refactoring in 235eae3, it is now called from 2 places:
1. Nodes::Condition#attributes=, with +name+ argument passed or +name+
and +ransacker_args+. Attributes are included only if #valid?.
2. Nodes::Grouping#new_condition without arguments. In this case, the
#valid? conditional needs to be bypassed, otherwise nothing is
built. The `name.nil?` conditional below currently does this.
TODO: Add test coverage for this behavior and ensure that `name.nil?`
isn't fixing issue #701 by introducing untested regressions.
150
151
152
153
154
155
156
157
158
|
# File 'lib/ransack/nodes/condition.rb', line 150
def build_attribute(name = nil, ransacker_args = [])
Attribute.new(@context, name, ransacker_args).tap do |attribute|
@context.bind(attribute, attribute.name)
self.attributes << attribute if name.nil? || attribute.valid?
if predicate && !negative?
@context.lock_association(attribute.parent)
end
end
end
|
#build_value(val = nil) ⇒ Object
160
161
162
163
164
|
# File 'lib/ransack/nodes/condition.rb', line 160
def build_value(val = nil)
Value.new(@context, val).tap do |value|
self.values << value
end
end
|
#casted_values_for_attribute(attr) ⇒ Object
228
229
230
|
# File 'lib/ransack/nodes/condition.rb', line 228
def casted_values_for_attribute(attr)
validated_values.map { |v| v.cast(predicate.type || attr.type) }
end
|
#combinator ⇒ Object
Also known as:
m
122
123
124
|
# File 'lib/ransack/nodes/condition.rb', line 122
def combinator
@attributes.size > 1 ? @combinator : nil
end
|
#combinator=(val) ⇒ Object
Also known as:
m=
126
127
128
|
# File 'lib/ransack/nodes/condition.rb', line 126
def combinator=(val)
@combinator = Constants::AND_OR.detect { |v| v == val.to_s } || nil
end
|
#default_type ⇒ Object
267
268
269
|
# File 'lib/ransack/nodes/condition.rb', line 267
def default_type
predicate.type || (attributes.first && attributes.first.type)
end
|
#eql?(other) ⇒ Boolean
Also known as:
==
193
194
195
196
197
198
199
|
# File 'lib/ransack/nodes/condition.rb', line 193
def eql?(other)
self.class == other.class &&
self.attributes == other.attributes &&
self.predicate == other.predicate &&
self.values == other.values &&
self.combinator == other.combinator
end
|
232
233
234
235
236
237
238
239
240
241
242
243
244
245
|
# File 'lib/ransack/nodes/condition.rb', line 232
def formatted_values_for_attribute(attr)
formatted = casted_values_for_attribute(attr).map do |val|
if attr.ransacker && attr.ransacker.formatter
val = attr.ransacker.formatter.call(val)
end
val = predicate.format(val)
val
end
if predicate.wants_array
formatted
else
formatted.first
end
end
|
#hash ⇒ Object
202
203
204
|
# File 'lib/ransack/nodes/condition.rb', line 202
def hash
[attributes, predicate, values, combinator].hash
end
|
#inspect ⇒ Object
271
272
273
274
275
276
277
278
279
280
281
282
|
# File 'lib/ransack/nodes/condition.rb', line 271
def inspect
data = [
['attributes'.freeze, a.try(:map, &:name)],
['predicate'.freeze, p],
[Constants::COMBINATOR, m],
['values'.freeze, v.try(:map, &:value)]
]
.reject { |e| e[1].blank? }
.map { |v| "#{v[0]}: #{v[1]}" }
.join(', '.freeze)
"Condition <#{data}>"
end
|
#key ⇒ Object
188
189
190
191
|
# File 'lib/ransack/nodes/condition.rb', line 188
def key
@key ||= attributes.map(&:name).join("_#{combinator}_") +
"_#{predicate.name}"
end
|
#negative? ⇒ Boolean
284
285
286
|
# File 'lib/ransack/nodes/condition.rb', line 284
def negative?
predicate.negative?
end
|
#persisted? ⇒ Boolean
184
185
186
|
# File 'lib/ransack/nodes/condition.rb', line 184
def persisted?
false
end
|
#predicate_name ⇒ Object
Also known as:
p
215
216
217
|
# File 'lib/ransack/nodes/condition.rb', line 215
def predicate_name
predicate.name if predicate
end
|
#predicate_name=(name) ⇒ Object
Also known as:
p=
206
207
208
209
210
211
212
|
# File 'lib/ransack/nodes/condition.rb', line 206
def predicate_name=(name)
self.predicate = Predicate.named(name)
unless negative?
attributes.each { |a| context.lock_association(a.parent) }
end
@predicate
end
|
#valid? ⇒ Boolean
67
68
69
70
|
# File 'lib/ransack/nodes/condition.rb', line 67
def valid?
attributes.detect(&:valid?) && predicate && valid_arity? &&
predicate.validate(values, default_type) && valid_combinator?
end
|
#valid_arity? ⇒ Boolean
72
73
74
|
# File 'lib/ransack/nodes/condition.rb', line 72
def valid_arity?
values.size <= 1 || predicate.wants_array
end
|
#validated_values ⇒ Object
224
225
226
|
# File 'lib/ransack/nodes/condition.rb', line 224
def validated_values
values.select { |v| predicate.validator.call(v.value) }
end
|
#value ⇒ Object
166
167
168
169
170
171
172
|
# File 'lib/ransack/nodes/condition.rb', line 166
def value
if predicate.wants_array
values.map { |v| v.cast(default_type) }
else
values.first.cast(default_type)
end
end
|
#values ⇒ Object
Also known as:
v
98
99
100
|
# File 'lib/ransack/nodes/condition.rb', line 98
def values
@values ||= []
end
|
#values=(args) ⇒ Object
Also known as:
v=
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
# File 'lib/ransack/nodes/condition.rb', line 103
def values=(args)
case args
when Array
args.each do |val|
val = Value.new(@context, val)
self.values << val
end
when Hash
args.each do |index, attrs|
val = Value.new(@context, attrs[:value])
self.values << val
end
else
raise ArgumentError,
"Invalid argument (#{args.class}) supplied to values="
end
end
|