diff --git a/CHANGELOG.rdoc b/CHANGELOG.rdoc index 59976b85..1de6ef2d 100644 --- a/CHANGELOG.rdoc +++ b/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 * Everything, completely rewritten. diff --git a/README.rdoc b/README.rdoc index 4d9afcf7..843e9c59 100644 --- a/README.rdoc +++ b/README.rdoc @@ -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. diff --git a/lib/searchlogic/search.rb b/lib/searchlogic/search.rb index d94e41d1..e8c0b60e 100644 --- a/lib/searchlogic/search.rb +++ b/lib/searchlogic/search.rb @@ -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 diff --git a/spec/search_spec.rb b/spec/search_spec.rb index 8997fe76..81997c90 100644 --- a/spec/search_spec.rb +++ b/spec/search_spec.rb @@ -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