Skip to content

Commit 81910c2

Browse files
authored
Merge pull request #1246 from danielpclark/bugfix-pr-1245
Fixing non-casted array predicates. Resolves issue #1245
2 parents 1f9c40f + 0e2ed0d commit 81910c2

File tree

4 files changed

+49
-9
lines changed

4 files changed

+49
-9
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
* Drop support for rubies under 2.5. PR #1189
44

5+
* Have casted array predicates type checked to Arel::Nodes::Casted fixing non-casted array predicates.
6+
PR [1246](https://github.com/activerecord-hackery/ransack/pull/1246)
7+
58
## 2.4.1 - 2020-12-21
69

710
* Add `ActiveRecord::Base.ransack!` which raises error if passed unknown condition

lib/ransack/adapters/active_record/ransack/nodes/condition.rb

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,19 @@ def in_predicate?(predicate)
4747
end
4848

4949
def casted_array?(predicate)
50-
(predicate.respond_to?(:value) && predicate.value.is_a?(Array)) || # Rails 6.1
51-
(predicate.respond_to?(:val) && predicate.val.is_a?(Array)) # Rails 5.2, 6.0
50+
value_from(predicate).is_a?(Array) && predicate.is_a?(Arel::Nodes::Casted)
5251
end
5352

54-
def format_values_for(predicate)
55-
value = if predicate.respond_to?(:value)
56-
predicate.value # Rails 6.1
57-
else
58-
predicate.val # Rails 5.2, 6.0
59-
end
53+
def value_from(predicate)
54+
if predicate.respond_to?(:value)
55+
predicate.value # Rails 6.1
56+
elsif predicate.respond_to?(:val)
57+
predicate.val # Rails 5.2, 6.0
58+
end
59+
end
6060

61-
value.map do |val|
61+
def format_values_for(predicate)
62+
value_from(predicate).map do |val|
6263
val.is_a?(String) ? Arel::Nodes.build_quoted(val) : val
6364
end
6465
end

spec/ransack/nodes/condition_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,19 @@
33
module Ransack
44
module Nodes
55
describe Condition do
6+
context 'bug report #1245' do
7+
it 'preserves tuple behavior' do
8+
ransack_hash = {
9+
m: 'and',
10+
g: [
11+
{ title_type_in: ['["title 1", ""]'] }
12+
]
13+
}
14+
15+
sql = Article.ransack(ransack_hash).result.to_sql
16+
expect(sql).to include("IN (('title 1', ''))")
17+
end
18+
end
619

720
context 'with an alias' do
821
subject {

spec/support/schema.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,29 @@ class Article < ActiveRecord::Base
138138
alias_attribute :content, :body
139139

140140
default_scope { where("'default_scope' = 'default_scope'") }
141+
142+
ransacker :title_type, formatter: lambda { |tuples|
143+
title, type = JSON.parse(tuples)
144+
Arel::Nodes::Grouping.new(
145+
[
146+
Arel::Nodes.build_quoted(title),
147+
Arel::Nodes.build_quoted(type)
148+
]
149+
)
150+
} do |_parent|
151+
articles = Article.arel_table
152+
Arel::Nodes::Grouping.new(
153+
%i[title type].map do |field|
154+
Arel::Nodes::NamedFunction.new(
155+
'COALESCE',
156+
[
157+
Arel::Nodes::NamedFunction.new('TRIM', [articles[field]]),
158+
Arel::Nodes.build_quoted('')
159+
]
160+
)
161+
end
162+
)
163+
end
141164
end
142165

143166
class StoryArticle < Article

0 commit comments

Comments
 (0)