Permalink
Browse files

* Added support for not_like, not_begin_with, not_end_with, and not_null

  • Loading branch information...
1 parent 4ee28bd commit 452cd7577feb1ef281565f4c5f1c7ea65d723e96 @binarylogic committed Jul 16, 2009
Showing with 38 additions and 1 deletion.
  1. +5 −1 CHANGELOG.rdoc
  2. +1 −0 Rakefile
  3. +12 −0 lib/searchlogic/named_scopes/conditions.rb
  4. +20 −0 spec/named_scopes/conditions_spec.rb
View
@@ -1,4 +1,8 @@
-== 2.1.7
+== 2.1.8
+
+* Added support for not_like, not_begin_with, not_end_with, and not_null
+
+== 2.1.7 released 2009-07-14
* Add support for time zones in the Search class when type casting to Time objects.
View
@@ -6,6 +6,7 @@ begin
Jeweler::Tasks.new do |gem|
gem.name = "searchlogic"
gem.summary = "Searchlogic provides common named scopes and object based searching for ActiveRecord."
+ gem.description = "Searchlogic provides common named scopes and object based searching for ActiveRecord."
gem.email = "bjohnson@binarylogic.com"
gem.homepage = "http://github.com/binarylogic/searchlogic"
gem.authors = ["Ben Johnson of Binary Logic"]
@@ -13,12 +13,16 @@ module Conditions
WILDCARD_CONDITIONS = {
:like => [:contains, :includes],
+ :not_like => [],
:begins_with => [:bw],
+ :not_begin_with => [:does_not_begin_with],
:ends_with => [:ew],
+ :not_end_with => [:does_not_end_with]
}
BOOLEAN_CONDITIONS = {
:null => [:nil],
+ :not_null => [:not_nil],
:empty => []
}
@@ -151,12 +155,20 @@ def create_primary_condition(column, condition)
scope_options(condition, column_type, "#{table_name}.#{column} > ?")
when /^like/
scope_options(condition, column_type, "#{table_name}.#{column} LIKE ?", :like)
+ when /^not_like/
+ scope_options(condition, column_type, "#{table_name}.#{column} NOT LIKE ?", :like)
when /^begins_with/
scope_options(condition, column_type, "#{table_name}.#{column} LIKE ?", :begins_with)
+ when /^not_begin_with/
+ scope_options(condition, column_type, "#{table_name}.#{column} NOT LIKE ?", :begins_with)
when /^ends_with/
scope_options(condition, column_type, "#{table_name}.#{column} LIKE ?", :ends_with)
+ when /^not_end_with/
+ scope_options(condition, column_type, "#{table_name}.#{column} NOT LIKE ?", :ends_with)
when "null"
{:conditions => "#{table_name}.#{column} IS NULL"}
+ when "not_null"
+ {:conditions => "#{table_name}.#{column} IS NOT NULL"}
when "empty"
{:conditions => "#{table_name}.#{column} = ''"}
end
@@ -49,15 +49,30 @@
User.username_like("john").all.should == User.find_all_by_username("bjohnson")
end
+ it "should have not like" do
+ %w(bjohnson thunt).each { |username| User.create(:username => username) }
+ User.username_not_like("john").all.should == User.find_all_by_username("thunt")
+ end
+
it "should have begins with" do
%w(bjohnson thunt).each { |username| User.create(:username => username) }
User.username_begins_with("bj").all.should == User.find_all_by_username("bjohnson")
end
+ it "should have not begin with" do
+ %w(bjohnson thunt).each { |username| User.create(:username => username) }
+ User.username_not_begin_with("bj").all.should == User.find_all_by_username("thunt")
+ end
+
it "should have ends with" do
%w(bjohnson thunt).each { |username| User.create(:username => username) }
User.username_ends_with("son").all.should == User.find_all_by_username("bjohnson")
end
+
+ it "should have not end with" do
+ %w(bjohnson thunt).each { |username| User.create(:username => username) }
+ User.username_not_end_with("son").all.should == User.find_all_by_username("thunt")
+ end
end
context "boolean conditions" do
@@ -66,6 +81,11 @@
User.username_null.all.should == User.find_all_by_username(nil)
end
+ it "should have not null" do
+ ["bjohnson", nil].each { |username| User.create(:username => username) }
+ User.username_not_null.all.should == User.find_all_by_username("bjohnson")
+ end
+
it "should have empty" do
["bjohnson", ""].each { |username| User.create(:username => username) }
User.username_empty.all.should == User.find_all_by_username("")

0 comments on commit 452cd75

Please sign in to comment.