Permalink
Browse files

added support for IN and NOT IN

  • Loading branch information...
1 parent 6e9ef37 commit 769ddc25a37a4ccf3acb58d4f59534a586c209e6 Carlos Paramio committed with Sep 23, 2009
Showing with 23 additions and 0 deletions.
  1. +11 −0 lib/searchlogic/named_scopes/conditions.rb
  2. +12 −0 spec/named_scopes/conditions_spec.rb
View
11 lib/searchlogic/named_scopes/conditions.rb
@@ -35,6 +35,11 @@ module Conditions
:not_blank => [:present]
}
+ GROUP_CONDITIONS = {
+ :in => [],
+ :not_in => []
+ }
+
CONDITIONS = {}
# Add any / all variations to every comparison and wildcard condition
@@ -46,6 +51,8 @@ module Conditions
BOOLEAN_CONDITIONS.each { |condition, aliases| CONDITIONS[condition] = aliases }
+ GROUP_CONDITIONS.each { |condition, aliases| CONDITIONS[condition] = aliases }
+
PRIMARY_CONDITIONS = CONDITIONS.keys
ALIAS_CONDITIONS = CONDITIONS.values.flatten
@@ -123,6 +130,10 @@ def create_primary_condition(column, condition)
{:conditions => "#{table_name}.#{column} = '' OR #{table_name}.#{column} IS NULL"}
when "not_blank"
{:conditions => "#{table_name}.#{column} != '' AND #{table_name}.#{column} IS NOT NULL"}
+ when "in"
+ scope_options(condition, column_type, "#{table_name}.#{column} IN (?)")
+ when "not_in"
+ scope_options(condition, column_type, "#{table_name}.#{column} NOT IN (?)")
end
named_scope("#{column}_#{condition}".to_sym, scope_options)
View
12 spec/named_scopes/conditions_spec.rb
@@ -269,6 +269,18 @@
end
end
+ context "group conditions" do
+ it "should have in" do
+ (5..7).each { |age| User.create(:age => age) }
+ User.age_in([5,6]).all == User.find(:all, :conditions => ["users.age IN (?)", [5, 6]])
+ end
+
+ it "should have not_in" do
+ (5..7).each { |age| User.create(:age => age) }
+ User.age_not_in([5,6]).all == User.find(:all, :conditions => ["users.age NOT IN (?)", [5, 6]])
+ end
+ end
+
context "searchlogic lambda" do
it "should be a string" do
User.username_like("test")

0 comments on commit 769ddc2

Please sign in to comment.