Permalink
Browse files

* Allow the chaining of conditions off of a search object. Ex: search…

….username_like("bjohnson").age_gt(20).all
  • Loading branch information...
1 parent c2bea31 commit 1cb9ecba5f721b05816b9415bc2172ffff6e5150 @binarylogic committed Jun 16, 2009
Showing with 22 additions and 5 deletions.
  1. +4 −0 CHANGELOG.rdoc
  2. +4 −4 README.rdoc
  3. +6 −1 lib/searchlogic/search.rb
  4. +8 −0 spec/search_spec.rb
View
@@ -1,3 +1,7 @@
+== 2.0.1
+
+* Allow the chaining of conditions off of a search object. Ex: search.username_like("bjohnson").age_gt(20).all
+
== 2.0.0
* Everything, completely rewritten.
View
@@ -68,7 +68,7 @@ Any named scope Searchlogic creates is dynamic and created via method_missing. M
That's all pretty standard, but here's where Searchlogic starts to get interesting...
-== Search using conditions on association columns
+== Search using conditions on associated columns
You also get named scopes for any of your associations:
@@ -159,9 +159,9 @@ What's great about this is that you can do just about anything you want. If Sear
Every condition you've seen in this readme also has 2 related conditions that you can use. Example:
- User.username_like_any("bjohnson", "thunt") # will return any users that have either of those strings in their username
- User.username_like_all("bjohnson", "thunt") # will return any users that have all of those string in their username
- User.username_like_any(["bjohnson", "thunt"]) # also accept an array
+ User.username_like_any("bjohnson", "thunt") # will return any users that have either of the strings in their username
+ User.username_like_all("bjohnson", "thunt") # will return any users that have all of the strings in their username
+ User.username_like_any(["bjohnson", "thunt"]) # also accepts an array
This is great for checkbox filters, etc. Where you can pass an array right from your form to this condition.
@@ -71,7 +71,12 @@ def method_missing(name, *args, &block)
raise UnknownConditionError.new(name)
end
elsif scope?(normalize_scope_name(name))
- conditions[name]
+ if args.size > 0
+ send("#{name}=", *args)
+ self
+ else
+ conditions[name]
+ end
else
scope = conditions.inject(klass.scoped(current_scope)) do |scope, condition|
scope_name, value = condition
View
@@ -68,6 +68,14 @@
search.username_gt.should == "bjohnson"
end
+ it "should allow chainging conditions" do
+ user = User.create(:username => "bjohnson", :age => 20)
+ User.create(:username => "bjohnson", :age => 5)
+ search = User.search
+ search.username_equals("bjohnson").age_gt(10)
+ search.all.should == [user]
+ end
+
it "should allow setting association conditions" do
search = User.search
search.orders_total_gt = 10

0 comments on commit 1cb9ecb

Please sign in to comment.