Skip to content

Commit a41d4e9

Browse files
committed
Correct usage ransack_alias in forms
1 parent 2d56e78 commit a41d4e9

File tree

6 files changed

+38
-16
lines changed

6 files changed

+38
-16
lines changed

.github/workflows/cronjob.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
DB: sqlite3
1717
RAILS: main
1818
steps:
19-
- uses: actions/checkout@v2
19+
- uses: actions/checkout@v4
2020
- name: Set up Ruby
2121
uses: ruby/setup-ruby@v1
2222
with:
@@ -39,7 +39,7 @@ jobs:
3939
MYSQL_USERNAME: root
4040
MYSQL_PASSWORD: root
4141
steps:
42-
- uses: actions/checkout@v2
42+
- uses: actions/checkout@v4
4343
- name: Set up Ruby
4444
uses: ruby/setup-ruby@v1
4545
with:
@@ -85,7 +85,7 @@ jobs:
8585
--health-retries 5
8686
8787
steps:
88-
- uses: actions/checkout@v2
88+
- uses: actions/checkout@v4
8989
- name: Set up Ruby
9090
uses: ruby/setup-ruby@v1
9191
with:

.github/workflows/rubocop.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ jobs:
99
runs-on: ubuntu-22.04
1010

1111
steps:
12-
- uses: actions/checkout@v2
12+
- uses: actions/checkout@v4
1313
- name: Set up Ruby
1414
uses: ruby/setup-ruby@v1
1515
with:

.github/workflows/test.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424
DB: sqlite3
2525
RAILS: ${{ matrix.rails }}
2626
steps:
27-
- uses: actions/checkout@v2
27+
- uses: actions/checkout@v4
2828
- name: Set up Ruby
2929
uses: ruby/setup-ruby@v1
3030
with:
@@ -52,7 +52,7 @@ jobs:
5252
MYSQL_USERNAME: root
5353
MYSQL_PASSWORD: root
5454
steps:
55-
- uses: actions/checkout@v2
55+
- uses: actions/checkout@v4
5656
- name: Set up Ruby
5757
uses: ruby/setup-ruby@v1
5858
with:
@@ -103,7 +103,7 @@ jobs:
103103
--health-retries 5
104104
105105
steps:
106-
- uses: actions/checkout@v2
106+
- uses: actions/checkout@v4
107107
- name: Set up Ruby
108108
uses: ruby/setup-ruby@v1
109109
with:
@@ -118,7 +118,7 @@ jobs:
118118
bug-report-templates:
119119
runs-on: ubuntu-22.04
120120
steps:
121-
- uses: actions/checkout@v2
121+
- uses: actions/checkout@v4
122122
- name: Set up Ruby
123123
uses: ruby/setup-ruby@v1
124124
with:

lib/ransack/nodes/condition.rb

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
module Ransack
22
module Nodes
33
class Condition < Node
4-
i18n_word :attribute, :predicate, :combinator, :value
4+
i18n_word :attribute, :predicate, :combinator, :value, :name
55
i18n_alias a: :attribute, p: :predicate,
6-
m: :combinator, v: :value
6+
m: :combinator, v: :value, n: :name
77

8-
attr_accessor :predicate
8+
attr_accessor :predicate, :name
99

1010
class << self
1111
def extract(context, key, values)
@@ -18,7 +18,8 @@ def extract(context, key, values)
1818
a: attributes,
1919
p: predicate.name,
2020
m: combinator,
21-
v: predicate.wants_array ? Array(values) : [values]
21+
v: predicate.wants_array ? Array(values) : [values],
22+
n: key
2223
)
2324
# TODO: Figure out what to do with multiple types of attributes,
2425
# if anything. Tempted to go with "garbage in, garbage out" here.
@@ -127,6 +128,9 @@ def combinator=(val)
127128
alias :m= :combinator=
128129
alias :m :combinator
129130

131+
alias :n= :name=
132+
alias :n :name
133+
130134
# == build_attribute
131135
#
132136
# This method was originally called from Nodes::Grouping#new_condition
@@ -171,7 +175,7 @@ def value
171175

172176
def build(params)
173177
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)$/)
175179
self.send("#{key}=", value)
176180
end
177181
end
@@ -188,6 +192,13 @@ def key
188192
"_#{predicate.name}"
189193
end
190194

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+
191202
def eql?(other)
192203
self.class == other.class &&
193204
self.attributes == other.attributes &&
@@ -271,7 +282,8 @@ def inspect
271282
['attributes'.freeze, a.try(:map, &:name)],
272283
['predicate'.freeze, p],
273284
[Constants::COMBINATOR, m],
274-
['values'.freeze, v.try(:map, &:value)]
285+
['values'.freeze, v.try(:map, &:value)],
286+
['name'.freeze, n]
275287
]
276288
.reject { |e| e[1].blank? }
277289
.map { |v| "#{v[0]}: #{v[1]}" }

lib/ransack/nodes/grouping.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ def conditions=(conditions)
4949
alias :c= :conditions=
5050

5151
def [](key)
52-
conditions.detect { |c| c.key == key.to_s }
52+
conditions.detect { |c| c.has_name_or_key?(key) }
5353
end
5454

5555
def []=(key, value)
56-
conditions.reject! { |c| c.key == key.to_s }
56+
conditions.reject! { |c| c.has_name_or_key?(key) }
5757
self.conditions << value
5858
end
5959

spec/ransack/adapters/active_record/base_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,16 @@ module ActiveRecord
239239
expect(s.result.to_a).to eq []
240240
end
241241

242+
it 'return alias correctly from search' do
243+
s = Person.ransack(term_cont: 'atlo')
244+
expect(s.term_cont).to eq 'atlo'
245+
expect(s.name_or_email_cont).to eq 'atlo'
246+
247+
s = Person.ransack(daddy_cont: 'babi')
248+
expect(s.daddy_cont).to eq 'babi'
249+
expect(s.parent_name_cont).to eq 'babi'
250+
end
251+
242252
it 'also works with associations' do
243253
dad = Person.create!(name: 'Birdman')
244254
son = Person.create!(name: 'Weezy', parent: dad)

0 commit comments

Comments
 (0)