null / not_null predicates accept false

This commit is contained in:
Artem Baguinski 2014-05-12 16:59:37 +02:00
parent 99840fcc28
commit e383f0cea7
2 changed files with 18 additions and 5 deletions

View File

@ -2,6 +2,7 @@ module Ransack
module Constants module Constants
TRUE_VALUES = [true, 1, '1', 't', 'T', 'true', 'TRUE'].to_set TRUE_VALUES = [true, 1, '1', 't', 'T', 'true', 'TRUE'].to_set
FALSE_VALUES = [false, 0, '0', 'f', 'F', 'false', 'FALSE'].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) AREL_PREDICATES = %w(eq not_eq matches does_not_match lt lteq gt gteq in not_in)
@ -68,18 +69,18 @@ module Ransack
} }
], ],
['null', { ['null', {
:arel_predicate => 'eq', :arel_predicate => proc { |v| v ? 'eq' : 'not_eq' },
:compounds => false, :compounds => false,
:type => :boolean, :type => :boolean,
:validator => proc { |v| TRUE_VALUES.include?(v)}, :validator => proc { |v| BOOLEAN_VALUES.include?(v)},
:formatter => proc { |v| nil } :formatter => proc { |v| nil }
} }
], ],
['not_null', { ['not_null', {
:arel_predicate => 'not_eq', :arel_predicate => proc { |v| v ? 'not_eq' : 'eq' },
:compounds => false, :compounds => false,
:type => :boolean, :type => :boolean,
:validator => proc { |v| TRUE_VALUES.include?(v) }, :validator => proc { |v| BOOLEAN_VALUES.include?(v) },
:formatter => proc { |v| nil } } :formatter => proc { |v| nil } }
] ]
] ]

View File

@ -42,7 +42,7 @@ module Ransack
end end
describe 'cont' do describe 'cont' do
it_has_behavior 'wildcard escaping', :name_cont, it_has_behavior 'wildcard escaping', :name_cont,
(if ActiveRecord::Base.connection.adapter_name == "PostgreSQL" (if ActiveRecord::Base.connection.adapter_name == "PostgreSQL"
/"people"."name" ILIKE '%\\%\\._\\\\%'/ /"people"."name" ILIKE '%\\%\\._\\\\%'/
@ -86,6 +86,12 @@ module Ransack
field = "#{quote_table_name("people")}.#{quote_column_name("name")}" field = "#{quote_table_name("people")}.#{quote_column_name("name")}"
@s.result.to_sql.should match /#{field} IS NULL/ @s.result.to_sql.should match /#{field} IS NULL/
end 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 end
describe 'not_null' do describe 'not_null' do
@ -94,6 +100,12 @@ module Ransack
field = "#{quote_table_name("people")}.#{quote_column_name("name")}" field = "#{quote_table_name("people")}.#{quote_column_name("name")}"
@s.result.to_sql.should match /#{field} IS NOT NULL/ @s.result.to_sql.should match /#{field} IS NOT NULL/
end 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 end
end end