Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing non-casted array predicates. Resolves issue #1245 #1246

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

* Drop support for rubies under 2.5. PR #1189

* Have casted array predicates type checked to Arel::Nodes::Casted fixing non-casted array predicates.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a great idea: adding to the Changelog as part of the PR.

PR [1246](https://github.com/activerecord-hackery/ransack/pull/1246)

## 2.4.1 - 2020-12-21

* Add `ActiveRecord::Base.ransack!` which raises error if passed unknown condition
Expand Down
19 changes: 10 additions & 9 deletions lib/ransack/adapters/active_record/ransack/nodes/condition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,19 @@ def in_predicate?(predicate)
end

def casted_array?(predicate)
(predicate.respond_to?(:value) && predicate.value.is_a?(Array)) || # Rails 6.1
(predicate.respond_to?(:val) && predicate.val.is_a?(Array)) # Rails 5.2, 6.0
value_from(predicate).is_a?(Array) && predicate.is_a?(Arel::Nodes::Casted)
end

def format_values_for(predicate)
value = if predicate.respond_to?(:value)
predicate.value # Rails 6.1
else
predicate.val # Rails 5.2, 6.0
end
def value_from(predicate)
if predicate.respond_to?(:value)
predicate.value # Rails 6.1
elsif predicate.respond_to?(:val)
predicate.val # Rails 5.2, 6.0
end
end

value.map do |val|
def format_values_for(predicate)
value_from(predicate).map do |val|
val.is_a?(String) ? Arel::Nodes.build_quoted(val) : val
end
end
Expand Down
13 changes: 13 additions & 0 deletions spec/ransack/nodes/condition_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,19 @@
module Ransack
module Nodes
describe Condition do
context 'bug report #1245' do
it 'preserves tuple behavior' do
ransack_hash = {
m: 'and',
g: [
{ title_type_in: ['["title 1", ""]'] }
]
}

sql = Article.ransack(ransack_hash).result.to_sql
expect(sql).to include("IN (('title 1', ''))")
end
end

context 'with an alias' do
subject {
Expand Down
23 changes: 23 additions & 0 deletions spec/support/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,29 @@ class Article < ActiveRecord::Base
alias_attribute :content, :body

default_scope { where("'default_scope' = 'default_scope'") }

ransacker :title_type, formatter: lambda { |tuples|
title, type = JSON.parse(tuples)
Arel::Nodes::Grouping.new(
[
Arel::Nodes.build_quoted(title),
Arel::Nodes.build_quoted(type)
]
)
} do |_parent|
articles = Article.arel_table
Arel::Nodes::Grouping.new(
%i[title type].map do |field|
Arel::Nodes::NamedFunction.new(
'COALESCE',
[
Arel::Nodes::NamedFunction.new('TRIM', [articles[field]]),
Arel::Nodes.build_quoted('')
]
)
end
)
end
end

class StoryArticle < Article
Expand Down