Skip to content

Commit

Permalink
handle nils for equa;s
Browse files Browse the repository at this point in the history
  • Loading branch information
binarylogic committed Mar 8, 2011
1 parent 98e3389 commit 1766870
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/searchlogic/named_scopes/column_conditions.rb
Expand Up @@ -159,11 +159,13 @@ def create_primary_condition(column_name, condition)
# Kepp in mind that the lambdas get cached in a method, so you want to keep the contents of the lambdas as
# fast as possible, which is why I didn't do the case statement inside of the lambda.
def scope_options(condition, column, sql, options = {})
equals = !(condition.to_s =~ /^equals/).nil?
does_not_equal = !(condition.to_s =~ /^does_not_equal/).nil?

case condition.to_s
when /_(any|all)$/
any = $1 == "any"
join_word = any ? " OR " : " AND "
equals = condition.to_s =~ /^equals_/
searchlogic_lambda(column.type, :skip_conversion => options[:skip_conversion]) { |*values|
unless values.empty?
if equals && any
Expand Down Expand Up @@ -194,6 +196,12 @@ def scope_options(condition, column, sql, options = {})
searchlogic_lambda(column.type, :skip_conversion => options[:skip_conversion]) { |*values|
values.collect! { |value| value_with_modifier(value, options[:value_modifier]) }

if does_not_equal && values == [nil]
sql.gsub!('!=', 'IS NOT')
elsif equals && values == [nil]
sql.gsub!('=', 'IS')
end

{:conditions => [sql, *values]}
}
end
Expand Down
2 changes: 2 additions & 0 deletions spec/searchlogic/named_scopes/column_conditions_spec.rb
Expand Up @@ -18,7 +18,9 @@
context "comparison conditions" do
it "should have equals" do
(5..7).each { |age| User.create(:age => age) }
nil_user = User.create
User.age_equals(6).all.should == User.find_all_by_age(6)
User.age_equals(nil).all.should == User.find_all_by_age(nil)
end

it "should have does not equal" do
Expand Down

0 comments on commit 1766870

Please sign in to comment.