1
1
module Ransack
2
2
module Nodes
3
3
class Condition < Node
4
- i18n_word :attribute , :predicate , :combinator , :value
4
+ i18n_word :attribute , :predicate , :combinator , :value , :name
5
5
i18n_alias a : :attribute , p : :predicate ,
6
- m : :combinator , v : :value
6
+ m : :combinator , v : :value , n : :name
7
7
8
- attr_accessor :predicate
8
+ attr_accessor :predicate , :name
9
9
10
10
class << self
11
11
def extract ( context , key , values )
@@ -18,7 +18,8 @@ def extract(context, key, values)
18
18
a : attributes ,
19
19
p : predicate . name ,
20
20
m : combinator ,
21
- v : predicate . wants_array ? Array ( values ) : [ values ]
21
+ v : predicate . wants_array ? Array ( values ) : [ values ] ,
22
+ n : key
22
23
)
23
24
# TODO: Figure out what to do with multiple types of attributes,
24
25
# if anything. Tempted to go with "garbage in, garbage out" here.
@@ -127,6 +128,9 @@ def combinator=(val)
127
128
alias :m= :combinator=
128
129
alias :m :combinator
129
130
131
+ alias :n= :name=
132
+ alias :n :name
133
+
130
134
# == build_attribute
131
135
#
132
136
# This method was originally called from Nodes::Grouping#new_condition
@@ -171,7 +175,7 @@ def value
171
175
172
176
def build ( params )
173
177
params . with_indifferent_access . each do |key , value |
174
- if key . match ( /^(a|v|p|m)$/ )
178
+ if key . match ( /^(a|v|p|m|n )$/ )
175
179
self . send ( "#{ key } =" , value )
176
180
end
177
181
end
@@ -188,6 +192,13 @@ def key
188
192
"_#{ predicate . name } "
189
193
end
190
194
195
+ def has_name_or_key? ( val )
196
+ return nil if val . nil?
197
+
198
+ val = val . to_s
199
+ name == val || key == val
200
+ end
201
+
191
202
def eql? ( other )
192
203
self . class == other . class &&
193
204
self . attributes == other . attributes &&
@@ -271,7 +282,8 @@ def inspect
271
282
[ 'attributes' . freeze , a . try ( :map , &:name ) ] ,
272
283
[ 'predicate' . freeze , p ] ,
273
284
[ Constants ::COMBINATOR , m ] ,
274
- [ 'values' . freeze , v . try ( :map , &:value ) ]
285
+ [ 'values' . freeze , v . try ( :map , &:value ) ] ,
286
+ [ 'name' . freeze , n ]
275
287
]
276
288
. reject { |e | e [ 1 ] . blank? }
277
289
. map { |v | "#{ v [ 0 ] } : #{ v [ 1 ] } " }
0 commit comments