Skip to content

Commit

Permalink
* Added a delete method to the Search class to allow the deleting of …
Browse files Browse the repository at this point in the history
…conditions off of the object.
  • Loading branch information
binarylogic committed Jun 20, 2009
1 parent 327105d commit 47efaeb
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.rdoc
@@ -1,4 +1,8 @@
== 2.0.1
== 2.0.2

* Added a delete method to the Search class to allow the deleting of conditions off of the object.

== 2.0.1 released 2009-06-20

* Allow the chaining of conditions off of a search object. Ex: search.username_like("bjohnson").age_gt(20).all
* Split out left outer join creation into its own method, allowing you to use it in your own named scopes.
Expand Down
2 changes: 1 addition & 1 deletion README.rdoc
Expand Up @@ -101,7 +101,7 @@ If you want to use the :include option, just specify it:

Obviously, only do this if you want to actually use the included objects.

Lastly, because we are using ActiveRecord, named scopes, combining conditions, and ordering based on associated columns, the decision was made to use left outer joins instead of inner joins. This allows us to be consistent, include optional associations when ordering, and avoid duplicate joins when eager loading associations. If we use an inner join and combine any of these things we will get an "ambiguous name" sql error for the table being joined twice. Just like anything, be mindful of the SQL being produced in your application. If you are joining beyond 4 or 5 levels deep then you might consider looking at the query and optimizing it. Part of that optimization may require the use of inner joins depending on the query.
Lastly, because we are using ActiveRecord, named scopes, combining conditions, and ordering based on associated columns, the decision was made to use left outer joins instead of inner joins. This allows us to be consistent, include optional associations when ordering, and avoid duplicate joins when eager loading associations. If we use an inner join and combine any of these things we will get an "ambiguous name" sql error for the table being joined twice. Just like anything, be mindful of the SQL being produced in your application. If you are joining beyond 4 or 5 levels deep then you might consider looking at the query and optimizing it. Part of that optimization may require the use of inner joins instead of left outer joins depending on the query.

== Make searching and ordering data in your application trivial

Expand Down
8 changes: 8 additions & 0 deletions lib/searchlogic/search.rb
Expand Up @@ -64,6 +64,14 @@ def conditions=(values)
end
end

# Delete a condition from the search. Since conditions map to named scopes,
# if a named scope accepts a parameter there is no way to actually delete
# the scope if you do not want it anymore. A nil value might be meaningful
# to that scope.
def delete(name)
@conditions.delete(name.to_sym)
end

private
def method_missing(name, *args, &block)
if name.to_s =~ /(\w+)=$/
Expand Down
6 changes: 6 additions & 0 deletions spec/search_spec.rb
Expand Up @@ -40,6 +40,12 @@
search1.all.should == [user2]
end

it "should delete the condition" do
search = User.search(:username_like => "bjohnson")
search.delete("username_like")
search.username_like.should be_nil
end

context "conditions" do
it "should set the conditions and be accessible individually" do
search = User.search
Expand Down

0 comments on commit 47efaeb

Please sign in to comment.