Skip to content

Commit a10222a

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

File tree

9 files changed

+85
-21
lines changed

9 files changed

+85
-21
lines changed

.github/workflows/cronjob.yml

+3-3
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

+1-1
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

+4-4
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

+16-10
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
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
11-
def extract(context, key, values)
11+
def extract(context, name, values)
1212
attributes, predicate, combinator =
13-
extract_values_for_condition(key, context)
13+
extract_values_for_condition(name, context)
1414

1515
if attributes.size > 0 && predicate
1616
condition = self.new(context)
1717
condition.build(
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: name
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
@@ -193,12 +197,13 @@ def eql?(other)
193197
self.attributes == other.attributes &&
194198
self.predicate == other.predicate &&
195199
self.values == other.values &&
196-
self.combinator == other.combinator
200+
self.combinator == other.combinator &&
201+
self.name == other.name
197202
end
198203
alias :== :eql?
199204

200205
def hash
201-
[attributes, predicate, values, combinator].hash
206+
[attributes, predicate, values, combinator, name].hash
202207
end
203208

204209
def predicate_name=(name)
@@ -271,7 +276,8 @@ def inspect
271276
['attributes'.freeze, a.try(:map, &:name)],
272277
['predicate'.freeze, p],
273278
[Constants::COMBINATOR, m],
274-
['values'.freeze, v.try(:map, &:value)]
279+
['values'.freeze, v.try(:map, &:value)],
280+
['name'.freeze, n]
275281
]
276282
.reject { |e| e[1].blank? }
277283
.map { |v| "#{v[0]}: #{v[1]}" }

lib/ransack/nodes/grouping.rb

+19-2
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,23 @@ def conditions=(conditions)
4949
alias :c= :conditions=
5050

5151
def [](key)
52-
conditions.detect { |c| c.key == key.to_s }
52+
conditions.detect do |c|
53+
matched_condition(c, key)
54+
end
5355
end
5456

5557
def []=(key, value)
56-
conditions.reject! { |c| c.key == key.to_s }
58+
conditions.reject! do |c|
59+
matched_condition(c, key)
60+
end
61+
5762
self.conditions << value
5863
end
5964

65+
def matched_condition(c, key)
66+
c.name == key.to_s
67+
end
68+
6069
def values
6170
conditions + groupings
6271
end
@@ -107,6 +116,14 @@ def groupings=(groupings)
107116
end
108117
alias :g= :groupings=
109118

119+
def respond_to_missing?(method_id, include_private = false)
120+
method_name = method_id.to_s.dup
121+
writer = method_name.sub!(/\=$/, ''.freeze)
122+
return true if attribute_method?(method_name)
123+
124+
super
125+
end
126+
110127
def method_missing(method_id, *args)
111128
method_name = method_id.to_s.dup
112129
writer = method_name.sub!(/\=$/, ''.freeze)

lib/ransack/search.rb

+9
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,15 @@ def new_sort(opts = {})
9999
Nodes::Sort.new(@context).build(opts)
100100
end
101101

102+
def respond_to_missing?(method_id, include_private = false)
103+
method_name = method_id.to_s
104+
getter_name = method_name.sub(/=$/, ''.freeze)
105+
return true if base.attribute_method?(getter_name)
106+
return true if @context.ransackable_scope?(getter_name, @context.object)
107+
108+
super
109+
end
110+
102111
def method_missing(method_id, *args)
103112
method_name = method_id.to_s
104113
getter_name = method_name.sub(/=$/, ''.freeze)

spec/ransack/adapters/active_record/base_spec.rb

+21
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,27 @@ 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 nil
246+
247+
s = Person.ransack(daddy_cont: 'babi')
248+
expect(s.daddy_cont).to eq 'babi'
249+
expect(s.parent_name_cont).to eq nil
250+
251+
s = Person.ransack(
252+
term_cont: 'atlo',
253+
name_or_email_cont: 'different'
254+
)
255+
expect(s.term_cont).to eq 'atlo'
256+
expect(s.name_or_email_cont).to eq 'different'
257+
expect(s.result.to_sql).to include(/("|`)people("|`)\.("|`)name("|`) I?LIKE '%different%'/i)
258+
expect(s.result.to_sql).to include(/("|`)people("|`)\.("|`)email("|`) I?LIKE '%different%'/i)
259+
expect(s.result.to_sql).to include(/("|`)people("|`)\.("|`)name("|`) I?LIKE '%atlo%'/i)
260+
expect(s.result.to_sql).to include(/("|`)people("|`)\.("|`)email("|`) I?LIKE '%atlo%'/i)
261+
end
262+
242263
it 'also works with associations' do
243264
dad = Person.create!(name: 'Birdman')
244265
son = Person.create!(name: 'Weezy', parent: dad)

spec/ransack/nodes/condition_spec.rb

+4-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,10 @@ module Nodes
9595
Ransack.configure { |c| c.default_predicate = 'eq' }
9696
end
9797

98-
specify { expect(subject).to eq Condition.extract(Context.for(Person), 'full_name_eq', Person.first.name) }
98+
specify do
99+
expect(subject).not_to be_nil
100+
expect(subject.predicate.name).to eq 'eq'
101+
end
99102
end
100103
end
101104
end

spec/ransack/search_spec.rb

+8
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,14 @@ def remove_quotes_and_backticks(str)
689689
]
690690
expect(@s.groupings.first.children_name_eq).to eq 'Ernie'
691691
end
692+
693+
it 'respond_to? method_missing' do
694+
@s.groupings = [
695+
{ m: 'or', name_eq: 'Ernie', children_name_eq: 'Ernie' }
696+
]
697+
expect(@s.groupings.first.respond_to?(:children_name_eq)).to eq true
698+
expect(@s.groupings.first.method(:children_name_eq)).to be_truthy
699+
end
692700
end
693701
end
694702
end

0 commit comments

Comments
 (0)