Permalink
Browse files

* Make sure the search objects clone properly.

  • Loading branch information...
1 parent 8563a4d commit 58e132691b4f46564c6ed3078f4d2571d7d8657d @binarylogic committed Jun 17, 2009
Showing with 20 additions and 1 deletion.
  1. +1 −0 CHANGELOG.rdoc
  2. +4 −1 lib/searchlogic/named_scopes/associations.rb
  3. +4 −0 lib/searchlogic/search.rb
  4. +11 −0 spec/search_spec.rb
View
@@ -2,6 +2,7 @@
* 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.
+* Make sure the search objects clone properly.
== 2.0.0
@@ -152,7 +152,10 @@ def add_left_outer_joins(options, association)
#
# Now your joins are consistent with Searchlogic allowing you to avoid SQL errors with duplicate joins.
def left_outer_joins(association_name)
- ActiveRecord::Associations::ClassMethods::JoinDependency.new(self, association_name, nil).join_associations.collect { |assoc| assoc.association_join.strip }
+ ActiveRecord::Associations::ClassMethods::JoinDependency.new(self, association_name, nil).join_associations.collect do |assoc|
+ sql = assoc.association_join.strip
+ sql.split(/LEFT OUTER JOIN/).delete_if { |join| join.strip.blank? }.collect { |join| "LEFT OUTER JOIN #{join.strip}"}
+ end.flatten
end
end
end
@@ -46,6 +46,10 @@ def initialize(klass, current_scope, conditions = {})
self.conditions = conditions if conditions.is_a?(Hash)
end
+ def clone
+ self.class.new(klass, current_scope.clone, conditions.clone)
+ end
+
# Returns a hash of the current conditions set.
def conditions
@conditions ||= {}
View
@@ -29,6 +29,17 @@
end
end
+ it "should clone properly" do
+ company = Company.create
+ user1 = company.users.create(:age => 5)
+ user2 = company.users.create(:age => 25)
+ search1 = company.users.search(:age_gt => 10)
+ search2 = search1.clone
+ search2.age_gt = 1
+ search2.all.should == User.all
+ search1.all.should == [user2]
+ end
+
context "conditions" do
it "should set the conditions and be accessible individually" do
search = User.search

0 comments on commit 58e1326

Please sign in to comment.