From e383f0cea799cb2510d7da6ea0493b9d7c545375 Mon Sep 17 00:00:00 2001 From: Artem Baguinski Date: Mon, 12 May 2014 16:59:37 +0200 Subject: [PATCH] null / not_null predicates accept false --- lib/ransack/constants.rb | 9 +++++---- spec/ransack/predicate_spec.rb | 14 +++++++++++++- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/lib/ransack/constants.rb b/lib/ransack/constants.rb index 0dd73e4..37a41fe 100644 --- a/lib/ransack/constants.rb +++ b/lib/ransack/constants.rb @@ -2,6 +2,7 @@ module Ransack module Constants TRUE_VALUES = [true, 1, '1', 't', 'T', 'true', 'TRUE'].to_set FALSE_VALUES = [false, 0, '0', 'f', 'F', 'false', 'FALSE'].to_set + BOOLEAN_VALUES = TRUE_VALUES + FALSE_VALUES AREL_PREDICATES = %w(eq not_eq matches does_not_match lt lteq gt gteq in not_in) @@ -68,18 +69,18 @@ module Ransack } ], ['null', { - :arel_predicate => 'eq', + :arel_predicate => proc { |v| v ? 'eq' : 'not_eq' }, :compounds => false, :type => :boolean, - :validator => proc { |v| TRUE_VALUES.include?(v)}, + :validator => proc { |v| BOOLEAN_VALUES.include?(v)}, :formatter => proc { |v| nil } } ], ['not_null', { - :arel_predicate => 'not_eq', + :arel_predicate => proc { |v| v ? 'not_eq' : 'eq' }, :compounds => false, :type => :boolean, - :validator => proc { |v| TRUE_VALUES.include?(v) }, + :validator => proc { |v| BOOLEAN_VALUES.include?(v) }, :formatter => proc { |v| nil } } ] ] diff --git a/spec/ransack/predicate_spec.rb b/spec/ransack/predicate_spec.rb index 42d6b9f..4018aa4 100644 --- a/spec/ransack/predicate_spec.rb +++ b/spec/ransack/predicate_spec.rb @@ -42,7 +42,7 @@ module Ransack end describe 'cont' do - + it_has_behavior 'wildcard escaping', :name_cont, (if ActiveRecord::Base.connection.adapter_name == "PostgreSQL" /"people"."name" ILIKE '%\\%\\._\\\\%'/ @@ -86,6 +86,12 @@ module Ransack field = "#{quote_table_name("people")}.#{quote_column_name("name")}" @s.result.to_sql.should match /#{field} IS NULL/ end + + it 'generates a value IS NOT NULL query when assigned false' do + @s.name_null = false + field = "#{quote_table_name("people")}.#{quote_column_name("name")}" + @s.result.to_sql.should match /#{field} IS NOT NULL/ + end end describe 'not_null' do @@ -94,6 +100,12 @@ module Ransack field = "#{quote_table_name("people")}.#{quote_column_name("name")}" @s.result.to_sql.should match /#{field} IS NOT NULL/ end + + it 'generates a value IS NULL query when assigned false' do + @s.name_not_null = false + field = "#{quote_table_name("people")}.#{quote_column_name("name")}" + @s.result.to_sql.should match /#{field} IS NULL/ + end end end end