@@ -456,6 +456,49 @@ module Ransack
456456 let ( :notable_type_field ) {
457457 "#{ quote_table_name ( "notes" ) } .#{ quote_column_name ( "notable_type" ) } "
458458 }
459+ let ( :people_temperament_field ) {
460+ "#{ quote_table_name ( "people" ) } .#{ quote_column_name ( "temperament" ) } "
461+ }
462+
463+ context 'when evaluating enums' do
464+ before do
465+ Person . first . update_attribute ( :temperament , 'choleric' )
466+ end
467+
468+ it 'evaluates enum key correctly' do
469+ s = Search . new ( Person , temperament_eq : 'choleric' )
470+
471+ expect ( s . result . to_sql ) . not_to match ( /#{ people_temperament_field } = 0/ )
472+ expect ( s . result . to_sql ) . to match ( /#{ people_temperament_field } = #{ Person . temperaments [ :choleric ] } / )
473+ expect ( s . result ) . not_to be_empty
474+ end
475+
476+ it 'evaluates enum value correctly' do
477+ s = Search . new ( Person , temperament_eq : Person . temperaments [ :choleric ] )
478+
479+ expect ( s . result . to_sql ) . not_to match ( /#{ people_temperament_field } = 0/ )
480+ expect ( s . result . to_sql ) . to match ( /#{ people_temperament_field } = #{ Person . temperaments [ :choleric ] } / )
481+ expect ( s . result ) . not_to be_empty
482+ end
483+ end
484+
485+ # Regression test for https://github.com/activerecord-hackery/ransack/issues/1644
486+ context 'when enum fix does not break boolean predicate casting' do
487+ it 'casts 0 to false for not_null predicate' do
488+ s = Search . new ( Person , name_not_null : 0 )
489+ expect ( s . result . to_sql ) . to match ( /#{ people_name_field } IS NULL/ )
490+ end
491+
492+ it 'casts 1 to true for not_null predicate' do
493+ s = Search . new ( Person , name_not_null : 1 )
494+ expect ( s . result . to_sql ) . to match ( /#{ people_name_field } IS NOT NULL/ )
495+ end
496+
497+ it 'casts "false" to false for not_null predicate' do
498+ s = Search . new ( Person , name_not_null : 'false' )
499+ expect ( s . result . to_sql ) . to match ( /#{ people_name_field } IS NULL/ )
500+ end
501+ end
459502
460503 it 'evaluates conditions contextually' do
461504 s = Search . new ( Person , children_name_eq : 'Ernie' )
0 commit comments