Skip to content

Commit

Permalink
* Allow the chaining of conditions off of a search object. Ex: search…
Browse files Browse the repository at this point in the history
….username_like("bjohnson").age_gt(20).all
  • Loading branch information
binarylogic committed Jun 16, 2009
1 parent c2bea31 commit 1cb9ecb
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.rdoc
@@ -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 == 2.0.0


* Everything, completely rewritten. * Everything, completely rewritten.
Expand Down
8 changes: 4 additions & 4 deletions README.rdoc
Expand Up @@ -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... 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: You also get named scopes for any of your associations:


Expand Down Expand Up @@ -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: 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_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 those string 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 accept an array 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. This is great for checkbox filters, etc. Where you can pass an array right from your form to this condition.


Expand Down
7 changes: 6 additions & 1 deletion lib/searchlogic/search.rb
Expand Up @@ -71,7 +71,12 @@ def method_missing(name, *args, &block)
raise UnknownConditionError.new(name) raise UnknownConditionError.new(name)
end end
elsif scope?(normalize_scope_name(name)) elsif scope?(normalize_scope_name(name))
conditions[name] if args.size > 0
send("#{name}=", *args)
self
else
conditions[name]
end
else else
scope = conditions.inject(klass.scoped(current_scope)) do |scope, condition| scope = conditions.inject(klass.scoped(current_scope)) do |scope, condition|
scope_name, value = condition scope_name, value = condition
Expand Down
8 changes: 8 additions & 0 deletions spec/search_spec.rb
Expand Up @@ -68,6 +68,14 @@
search.username_gt.should == "bjohnson" search.username_gt.should == "bjohnson"
end 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 it "should allow setting association conditions" do
search = User.search search = User.search
search.orders_total_gt = 10 search.orders_total_gt = 10
Expand Down

0 comments on commit 1cb9ecb

Please sign in to comment.