Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

handle nils for equa;s

  • Loading branch information...
commit 17668703d5040e4896d9fa1a01c35c031d7a3a8d 1 parent 98e3389
@binarylogic authored
View
10 lib/searchlogic/named_scopes/column_conditions.rb
@@ -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
@@ -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
View
2  spec/searchlogic/named_scopes/column_conditions_spec.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.