Permalink
Browse files

clean up any or all conditions

  • Loading branch information...
1 parent 6d404a5 commit 6ab444e838a1bc067542cb56679ecfc106572f92 @binarylogic committed Apr 16, 2010
@@ -35,11 +35,6 @@ module Conditions
:not_blank => [:present]
}
- GROUP_CONDITIONS = {
- :in => [],
- :not_in => []
- }
-
CONDITIONS = {}
# Add any / all variations to every comparison and wildcard condition
@@ -50,12 +45,10 @@ module Conditions
end
CONDITIONS[:equals_any] = CONDITIONS[:equals_any] + [:in]
- CONDITIONS[:does_not_equal_any] = CONDITIONS[:does_not_equal_any] + [:not_in]
+ CONDITIONS[:does_not_equal_all] = CONDITIONS[:does_not_equal_all] + [:not_in]
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
@@ -158,35 +151,28 @@ def create_primary_condition(column, condition)
def scope_options(condition, column_type, sql, options = {})
case condition.to_s
when /_(any|all)$/
- any_or_all_scope_options(column_type, sql, options)
+ searchlogic_lambda(column_type, :skip_conversion => options[:skip_conversion]) { |*values|
+ return {} if values.empty?
+ values.flatten!
+ values.collect! { |value| value_with_modifier(value, options[:value_modifier]) }
+
+ join = $1 == "any" ? " OR " : " AND "
+
+ scope_sql = values.collect { |value| sql.is_a?(Proc) ? sql.call(value) : sql }.join(join)
+
+ {:conditions => [scope_sql, *expand_range_bind_variables(values)]}
+ }
else
- general_scope_options(column_type, sql, options)
+ searchlogic_lambda(column_type, :skip_conversion => options[:skip_conversion]) { |*values|
+ values.collect! { |value| value_with_modifier(value, options[:value_modifier]) }
+
+ scope_sql = sql.is_a?(Proc) ? sql.call(*values) : sql
+
+ {:conditions => [scope_sql, *expand_range_bind_variables(values)]}
+ }
end
end
- def any_or_all_scope_options(column_type, sql, options)
- searchlogic_lambda(column_type, :skip_conversion => options[:skip_conversion]) { |*values|
- return {} if values.empty?
- values.flatten!
- values.collect! { |value| value_with_modifier(value, options[:value_modifier]) }
-
- join = $1 == "any" ? " OR " : " AND "
- scope_sql = values.collect { |value| sql.is_a?(Proc) ? sql.call(value) : sql }.join(join)
-
- {:conditions => [scope_sql, *expand_range_bind_variables(values)]}
- }
- end
-
- def general_scope_options(column_type, sql, options)
- searchlogic_lambda(column_type, :skip_conversion => options[:skip_conversion]) { |*values|
- values.collect! { |value| value_with_modifier(value, options[:value_modifier]) }
-
- scope_sql = sql.is_a?(Proc) ? sql.call(*values) : sql
-
- {:conditions => [scope_sql, *expand_range_bind_variables(values)]}
- }
- end
-
def value_with_modifier(value, modifier)
case modifier
when :like
@@ -185,4 +185,14 @@
:joins => ["INNER JOIN \"users\" ON \"users\".id = \"audits\".auditable_id AND \"audits\".auditable_type = 'User'", " INNER JOIN \"companies\" ON \"companies\".id = \"users\".company_id "]
}
end
+
+ it "should allow any on a has_many relationship" do
+ company1 = Company.create
+ user1 = company1.users.create
+ company2 = Company.create
+ user2 = company2.users.create
+ user3 = company2.users.create
+
+ Company.users_id_equals_any([user2.id, user3.id]).all(:select => "DISTINCT companies.*").should == [company2]
+ end
end
@@ -123,93 +123,93 @@
it "should have equals any" do
%w(bjohnson thunt dgainor).each { |username| User.create(:username => username) }
- User.username_equals_any("bjohnson", "thunt").all == User.find_all_by_username(["bjohnson", "thunt"])
+ User.username_equals_any("bjohnson", "thunt").all.should == User.find_all_by_username(["bjohnson", "thunt"])
end
it "should have equals all" do
%w(bjohnson thunt dainor).each { |username| User.create(:username => username) }
- User.username_equals_all("bjohnson", "thunt").all == []
+ User.username_equals_all("bjohnson", "thunt").all.should == []
end
it "should have does not equal any" do
%w(bjohnson thunt dgainor).each { |username| User.create(:username => username) }
- User.username_does_not_equal_any("bjohnson", "thunt").all == User.find_all_by_username("dgainor")
+ User.username_does_not_equal_any("bjohnson", "thunt").all.should == User.find_all_by_username(["bjohnson", "thunt", "dgainor"])
end
it "should have does not equal all" do
%w(bjohnson thunt dgainor).each { |username| User.create(:username => username) }
- User.username_does_not_equal_all("bjohnson", "thunt").all == User.find_all_by_username("dgainor")
+ User.username_does_not_equal_all("bjohnson", "thunt").all.should == User.find_all_by_username("dgainor")
end
it "should have less than any" do
(5..7).each { |age| User.create(:age => age) }
- User.age_less_than_any(7,6).all == User.find_all_by_age([5, 6])
+ User.age_less_than_any(7,6).all.should == User.find_all_by_age([5, 6])
end
it "should have less than all" do
(5..7).each { |age| User.create(:age => age) }
- User.age_less_than_all(7,6).all == User.find_all_by_age(5)
+ User.age_less_than_all(7,6).all.should == User.find_all_by_age(5)
end
it "should have less than or equal to any" do
(5..7).each { |age| User.create(:age => age) }
- User.age_less_than_or_equal_to_any(7,6).all == User.find_all_by_age([5, 6, 7])
+ User.age_less_than_or_equal_to_any(7,6).all.should == User.find_all_by_age([5, 6, 7])
end
it "should have less than or equal to all" do
(5..7).each { |age| User.create(:age => age) }
- User.age_less_than_or_equal_to_all(7,6).all == User.find_all_by_age([5, 6])
+ User.age_less_than_or_equal_to_all(7,6).all.should == User.find_all_by_age([5, 6])
end
it "should have less than any" do
(5..7).each { |age| User.create(:age => age) }
- User.age_greater_than_any(5,6).all == User.find_all_by_age([6, 7])
+ User.age_greater_than_any(5,6).all.should == User.find_all_by_age([6, 7])
end
it "should have greater than all" do
(5..7).each { |age| User.create(:age => age) }
- User.age_greater_than_all(5,6).all == User.find_all_by_age(7)
+ User.age_greater_than_all(5,6).all.should == User.find_all_by_age(7)
end
it "should have greater than or equal to any" do
(5..7).each { |age| User.create(:age => age) }
- User.age_greater_than_or_equal_to_any(5,6).all == User.find_all_by_age([5, 6, 7])
+ User.age_greater_than_or_equal_to_any(5,6).all.should == User.find_all_by_age([5, 6, 7])
end
it "should have greater than or equal to all" do
(5..7).each { |age| User.create(:age => age) }
- User.age_greater_than_or_equal_to_all(5,6).all == User.find_all_by_age([6, 7])
+ User.age_greater_than_or_equal_to_all(5,6).all.should == User.find_all_by_age([6, 7])
end
it "should have like all" do
%w(bjohnson thunt dgainor).each { |username| User.create(:username => username) }
- User.username_like_all("bjohnson", "thunt").all == []
- User.username_like_all("n", "o").all == User.find_all_by_username(["bjohnson", "thunt"])
+ User.username_like_all("bjohnson", "thunt").all.should == []
+ User.username_like_all("n", "o").all.should == User.find_all_by_username(["bjohnson", "dgainor"])
end
it "should have like any" do
%w(bjohnson thunt dgainor).each { |username| User.create(:username => username) }
- User.username_like_all("bjohnson", "thunt").all == User.find_all_by_username(["bjohnson", "thunt"])
+ User.username_like_any("bjohnson", "thunt").all.should == User.find_all_by_username(["bjohnson", "thunt"])
end
it "should have begins with all" do
%w(bjohnson thunt dgainor).each { |username| User.create(:username => username) }
- User.username_begins_with_all("bjohnson", "thunt").all == []
+ User.username_begins_with_all("bjohnson", "thunt").all.should == []
end
it "should have begins with any" do
%w(bjohnson thunt dgainor).each { |username| User.create(:username => username) }
- User.username_begins_with_any("bj", "th").all == User.find_all_by_username(["bjohnson", "thunt"])
+ User.username_begins_with_any("bj", "th").all.should == User.find_all_by_username(["bjohnson", "thunt"])
end
it "should have ends with all" do
%w(bjohnson thunt dgainor).each { |username| User.create(:username => username) }
- User.username_ends_with_all("n", "r").all == []
+ User.username_ends_with_all("n", "r").all.should == []
end
it "should have ends with any" do
%w(bjohnson thunt dgainor).each { |username| User.create(:username => username) }
- User.username_ends_with_any("n", "r").all == User.find_all_by_username(["bjohnson", "dgainor"])
+ User.username_ends_with_any("n", "r").all.should == User.find_all_by_username(["bjohnson", "dgainor"])
end
end
@@ -278,12 +278,12 @@
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]])
+ User.age_in([5,6]).all.should == 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]])
+ User.age_not_in([5,6]).all.should == User.find(:all, :conditions => ["users.age NOT IN (?)", [5, 6]])
end
end

0 comments on commit 6ab444e

Please sign in to comment.