Skip to content
This repository
Browse code

* Make sure the search objects clone properly.

  • Loading branch information...
commit 58e132691b4f46564c6ed3078f4d2571d7d8657d 1 parent 8563a4d
Ben Johnson authored June 17, 2009
1  CHANGELOG.rdoc
Source Rendered
@@ -2,6 +2,7 @@
2 2
 
3 3
 * Allow the chaining of conditions off of a search object. Ex: search.username_like("bjohnson").age_gt(20).all
4 4
 * Split out left outer join creation into its own method, allowing you to use it in your own named scopes.
  5
+* Make sure the search objects clone properly.
5 6
 
6 7
 == 2.0.0
7 8
 
5  lib/searchlogic/named_scopes/associations.rb
@@ -152,7 +152,10 @@ def add_left_outer_joins(options, association)
152 152
         #
153 153
         # Now your joins are consistent with Searchlogic allowing you to avoid SQL errors with duplicate joins.
154 154
         def left_outer_joins(association_name)
155  
-          ActiveRecord::Associations::ClassMethods::JoinDependency.new(self, association_name, nil).join_associations.collect { |assoc| assoc.association_join.strip }
  155
+          ActiveRecord::Associations::ClassMethods::JoinDependency.new(self, association_name, nil).join_associations.collect do |assoc|
  156
+            sql = assoc.association_join.strip
  157
+            sql.split(/LEFT OUTER JOIN/).delete_if { |join| join.strip.blank? }.collect { |join| "LEFT OUTER JOIN #{join.strip}"}
  158
+          end.flatten
156 159
         end
157 160
     end
158 161
   end
4  lib/searchlogic/search.rb
@@ -46,6 +46,10 @@ def initialize(klass, current_scope, conditions = {})
46 46
       self.conditions = conditions if conditions.is_a?(Hash)
47 47
     end
48 48
     
  49
+    def clone
  50
+      self.class.new(klass, current_scope.clone, conditions.clone)
  51
+    end
  52
+    
49 53
     # Returns a hash of the current conditions set.
50 54
     def conditions
51 55
       @conditions ||= {}
11  spec/search_spec.rb
@@ -29,6 +29,17 @@
29 29
     end
30 30
   end
31 31
   
  32
+  it "should clone properly" do
  33
+    company = Company.create
  34
+    user1 = company.users.create(:age => 5)
  35
+    user2 = company.users.create(:age => 25)
  36
+    search1 = company.users.search(:age_gt => 10)
  37
+    search2 = search1.clone
  38
+    search2.age_gt = 1
  39
+    search2.all.should == User.all
  40
+    search1.all.should == [user2]
  41
+  end
  42
+  
32 43
   context "conditions" do
33 44
     it "should set the conditions and be accessible individually" do
34 45
       search = User.search

0 notes on commit 58e1326

Please sign in to comment.
Something went wrong with that request. Please try again.