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
- def extract ( context , key , values )
11
+ def extract ( context , name , values )
12
12
attributes , predicate , combinator =
13
- extract_values_for_condition ( key , context )
13
+ extract_values_for_condition ( name , context )
14
14
15
15
if attributes . size > 0 && predicate
16
16
condition = self . new ( context )
17
17
condition . build (
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 : name
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
@@ -193,12 +197,13 @@ def eql?(other)
193
197
self . attributes == other . attributes &&
194
198
self . predicate == other . predicate &&
195
199
self . values == other . values &&
196
- self . combinator == other . combinator
200
+ self . combinator == other . combinator &&
201
+ self . name == other . name
197
202
end
198
203
alias :== :eql?
199
204
200
205
def hash
201
- [ attributes , predicate , values , combinator ] . hash
206
+ [ attributes , predicate , values , combinator , name ] . hash
202
207
end
203
208
204
209
def predicate_name = ( name )
@@ -271,7 +276,8 @@ def inspect
271
276
[ 'attributes' . freeze , a . try ( :map , &:name ) ] ,
272
277
[ 'predicate' . freeze , p ] ,
273
278
[ Constants ::COMBINATOR , m ] ,
274
- [ 'values' . freeze , v . try ( :map , &:value ) ]
279
+ [ 'values' . freeze , v . try ( :map , &:value ) ] ,
280
+ [ 'name' . freeze , n ]
275
281
]
276
282
. reject { |e | e [ 1 ] . blank? }
277
283
. map { |v | "#{ v [ 0 ] } : #{ v [ 1 ] } " }
0 commit comments