Permalink
Browse files

add support for not_* for boolean columns

  • Loading branch information...
binarylogic committed Nov 24, 2009
1 parent 85ff2ff commit b9e6675dabdaab3056d659f0078a7de10a4a5a4b
Showing with 11 additions and 8 deletions.
  1. +4 −2 lib/searchlogic/named_scopes/conditions.rb
  2. +7 −6 spec/named_scopes/conditions_spec.rb
@@ -72,15 +72,17 @@ def local_condition?(name)
end
def boolean_condition?(name)
- columns_hash.key?(name.to_s) && columns_hash[name.to_s].type == :boolean
+ column = columns_hash[name.to_s] || columns_hash[name.to_s.gsub(/^not_/, "")]
+ column && column.type == :boolean
end
def method_missing(name, *args, &block)
if details = condition_details(name)
create_condition(details[:column], details[:condition], args)
send(name, *args)
elsif boolean_condition?(name)
- named_scope name, :conditions => {name => true}
+ column = name.to_s.gsub(/^not_/, "")
+ named_scope name, :conditions => {column => (name.to_s =~ /^not_/).nil?}
send(name)
else
super
@@ -19,12 +19,6 @@
User.age_equals([5, 7]).all.should == User.find_all_by_age([5, 7])
end
- it "should have equals for boolean columns" do
- female = User.create(:male => false)
- male = User.create(:male => true)
- User.male.should == [male]
- end
-
it "should have does not equal" do
(5..7).each { |age| User.create(:age => age) }
User.age_does_not_equal(6).all.should == User.find_all_by_age([5,7])
@@ -84,6 +78,13 @@
end
context "boolean conditions" do
+ it "should have scopes for boolean columns" do
+ female = User.create(:male => false)
+ male = User.create(:male => true)
+ User.male.all.should == [male]
+ User.not_male.all.should == [female]
+ end
+
it "should have null" do
["bjohnson", nil].each { |username| User.create(:username => username) }
User.username_null.all.should == User.find_all_by_username(nil)

0 comments on commit b9e6675

Please sign in to comment.