diff --git a/lib/ransack/constants.rb b/lib/ransack/constants.rb index b8a9838..863222b 100644 --- a/lib/ransack/constants.rb +++ b/lib/ransack/constants.rb @@ -55,6 +55,14 @@ module Ransack :formatter => proc { |v| true } } ], + ['not_true', { + :arel_predicate => proc { |v| v ? 'not_eq' : 'eq' }, + :compounds => false, + :type => :boolean, + :validator => proc { |v| BOOLEAN_VALUES.include?(v) }, + :formatter => proc { |v| true } + } + ], ['false', { :arel_predicate => proc { |v| v ? 'eq' : 'not_eq' }, :compounds => false, @@ -63,6 +71,14 @@ module Ransack :formatter => proc { |v| false } } ], + ['not_false', { + :arel_predicate => proc { |v| v ? 'not_eq' : 'eq' }, + :compounds => false, + :type => :boolean, + :validator => proc { |v| BOOLEAN_VALUES.include?(v) }, + :formatter => proc { |v| false } + } + ], ['present', { :arel_predicate => proc { |v| v ? 'not_eq_all' : 'eq_any' }, :compounds => false, diff --git a/spec/ransack/predicate_spec.rb b/spec/ransack/predicate_spec.rb index 49436d7..6c64a20 100644 --- a/spec/ransack/predicate_spec.rb +++ b/spec/ransack/predicate_spec.rb @@ -96,6 +96,22 @@ module Ransack end end + describe 'not_true' do + it 'generates an inequality condition for boolean true' do + @s.awesome_not_true = true + field = "#{quote_table_name("people")}.#{quote_column_name("awesome")}" + expect(@s.result.to_sql).to match /#{field} != #{ + ActiveRecord::Base.connection.quoted_true}/ + end + + it 'generates an equality condition for boolean true' do + @s.awesome_not_true = false + field = "#{quote_table_name("people")}.#{quote_column_name("awesome")}" + expect(@s.result.to_sql).to match /#{field} = #{ + ActiveRecord::Base.connection.quoted_true}/ + end + end + describe 'false' do it 'generates an equality condition for boolean false' do @s.awesome_false = true @@ -112,6 +128,22 @@ module Ransack end end + describe 'not_false' do + it 'generates an inequality condition for boolean false' do + @s.awesome_not_false = true + field = "#{quote_table_name("people")}.#{quote_column_name("awesome")}" + expect(@s.result.to_sql).to match /#{field} != #{ + ActiveRecord::Base.connection.quoted_false}/ + end + + it 'generates an equality condition for boolean false' do + @s.awesome_not_false = false + field = "#{quote_table_name("people")}.#{quote_column_name("awesome")}" + expect(@s.result.to_sql).to match /#{field} = #{ + ActiveRecord::Base.connection.quoted_false}/ + end + end + describe 'null' do it 'generates a value IS NULL query' do @s.name_null = true