File tree Expand file tree Collapse file tree 4 files changed +49
-9
lines changed
lib/ransack/adapters/active_record/ransack/nodes Expand file tree Collapse file tree 4 files changed +49
-9
lines changed Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 33module 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 {
Original file line number Diff line number Diff 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
141164end
142165
143166class StoryArticle < Article
You can’t perform that action at this time.
0 commit comments