Permalink
Browse files

Allow the use of _or_ conditions for Search class and in forms

Before interpolating the condition parts order is reversed, but never
order back again. Therefore those dynamically named scopes have a
different name then the calling method and cannot be identified after.

Signed-off-by: Ben Johnson <bjohnson@binarylogic.com>
  • Loading branch information...
1 parent 1570bb8 commit 0761c95f47be71b99779199b2c525690ca48b97e @namxam namxam committed with Sep 22, 2009
Showing with 13 additions and 8 deletions.
  1. +1 −1 lib/searchlogic/named_scopes/or_conditions.rb
  2. +12 −7 spec/named_scopes/or_conditions_spec.rb
View
2 lib/searchlogic/named_scopes/or_conditions.rb
@@ -91,7 +91,7 @@ def interpolate_or_conditions(parts)
end
end
- conditions
+ conditions.reverse
end
def create_or_condition(scopes, args)
View
19 spec/named_scopes/or_conditions_spec.rb
@@ -1,36 +1,41 @@
require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
describe "Or conditions" do
+ it "should define a scope by the exact same name as requested by the code" do
+ User.name_or_username_like('Test')
+ User.respond_to?(:name_or_username_like).should be_true
+ end
+
it "should match username or name" do
- User.username_or_name_like("ben").proxy_options.should == {:conditions => "(users.name LIKE '%ben%') OR (users.username LIKE '%ben%')"}
+ User.username_or_name_like("ben").proxy_options.should == {:conditions => "(users.username LIKE '%ben%') OR (users.name LIKE '%ben%')"}
end
it "should use the specified condition" do
- User.username_begins_with_or_name_like("ben").proxy_options.should == {:conditions => "(users.name LIKE '%ben%') OR (users.username LIKE 'ben%')"}
+ User.username_begins_with_or_name_like("ben").proxy_options.should == {:conditions => "(users.username LIKE 'ben%') OR (users.name LIKE '%ben%')"}
end
it "should use the last specified condition" do
- User.username_or_name_like_or_id_or_age_lt(10).proxy_options.should == {:conditions => "(users.age < 10) OR (users.id < 10) OR (users.name LIKE '%10%') OR (users.username LIKE '%10%')"}
+ User.username_or_name_like_or_id_or_age_lt(10).proxy_options.should == {:conditions => "(users.username LIKE '%10%') OR (users.name LIKE '%10%') OR (users.id < 10) OR (users.age < 10)"}
end
it "should raise an error on unknown conditions" do
lambda { User.usernme_begins_with_or_name_like("ben") }.should raise_error(Searchlogic::NamedScopes::OrConditions::UnknownConditionError)
end
it "should work well with _or_equal_to" do
- User.id_less_than_or_equal_to_or_age_gt(10).proxy_options.should == {:conditions => "(users.age > 10) OR (users.id <= 10)"}
+ User.id_less_than_or_equal_to_or_age_gt(10).proxy_options.should == {:conditions => "(users.id <= 10) OR (users.age > 10)"}
end
it "should work well with _or_equal_to_any" do
- User.id_less_than_or_equal_to_all_or_age_gt(10).proxy_options.should == {:conditions => "(users.age > 10) OR (users.id <= 10)"}
+ User.id_less_than_or_equal_to_all_or_age_gt(10).proxy_options.should == {:conditions => "(users.id <= 10) OR (users.age > 10)"}
end
it "should work well with _or_equal_to_all" do
- User.id_less_than_or_equal_to_any_or_age_gt(10).proxy_options.should == {:conditions => "(users.age > 10) OR (users.id <= 10)"}
+ User.id_less_than_or_equal_to_any_or_age_gt(10).proxy_options.should == {:conditions => "(users.id <= 10) OR (users.age > 10)"}
end
it "should play nice with other scopes" do
User.username_begins_with("ben").id_gt(10).age_not_nil.username_or_name_ends_with("ben").scope(:find).should ==
- {:conditions => "((users.name LIKE '%ben') OR (users.username LIKE '%ben')) AND ((users.age IS NOT NULL) AND ((users.id > 10) AND (users.username LIKE 'ben%')))"}
+ {:conditions => "((users.username LIKE '%ben') OR (users.name LIKE '%ben')) AND ((users.age IS NOT NULL) AND ((users.id > 10) AND (users.username LIKE 'ben%')))"}
end
end

0 comments on commit 0761c95

Please sign in to comment.