Skip to content
This repository
Browse code

It shouldnt cast ignored values, those changing them to non-ignored v…

…alues
  • Loading branch information...
commit 4a0f4b16d3410bb63912b2db6e12974a8d1664ea 1 parent 049861f
Ben Johnson authored February 02, 2010
40  lib/searchlogic/search.rb
@@ -87,6 +87,7 @@ def method_missing(name, *args, &block)
87 87
         if setter?(name)
88 88
           if scope?(scope_name)
89 89
             mass_conditions.delete(scope_name.to_sym) if !setting_mass_conditions?
  90
+            
90 91
             if args.size == 1
91 92
               conditions[condition_name] = type_cast(args.first, cast_type(scope_name))
92 93
             else
@@ -106,8 +107,11 @@ def method_missing(name, *args, &block)
106 107
           scope = conditions_array.inject(klass.scoped(current_scope) || {}) do |scope, condition|
107 108
             scope_name, value = condition
108 109
             
109  
-            value.delete_if { |v| ignore_value?(scope_name, v) } if value.is_a?(Array)
110  
-            if !ignore_value?(scope_name, value)
  110
+            value.delete_if { |v| ignore_value?(v) } if mass_conditions.key?(scope_name.to_sym) && value.is_a?(Array)
  111
+            
  112
+            if mass_conditions.key?(scope_name.to_sym) && ignore_value?(value)
  113
+              klass.scoped({})
  114
+            else
111 115
               scope_name = normalize_scope_name(scope_name)
112 116
               klass.send(scope_name, value) if !klass.respond_to?(scope_name)
113 117
               arity = klass.named_scope_arity(scope_name)
@@ -123,8 +127,6 @@ def method_missing(name, *args, &block)
123 127
               else
124 128
                 scope.send(scope_name, value)
125 129
               end
126  
-            else
127  
-              klass.scoped({})
128 130
             end
129 131
           end
130 132
           scope.send(name, *args, &block)
@@ -187,26 +189,30 @@ def type_cast(value, type)
187 189
         when Range
188 190
           Range.new(type_cast(value.first, type), type_cast(value.last, type))
189 191
         else
190  
-          # Let's leverage ActiveRecord's type casting, so that casting is consistent
191  
-          # with the other models.
192  
-          column_for_type_cast = ::ActiveRecord::ConnectionAdapters::Column.new("", nil)
193  
-          column_for_type_cast.instance_variable_set(:@type, type)
194  
-          casted_value = column_for_type_cast.type_cast(value)
  192
+          if ignore_value?(value)
  193
+            value
  194
+          else
  195
+            # Let's leverage ActiveRecord's type casting, so that casting is consistent
  196
+            # with the other models.
  197
+            column_for_type_cast = ::ActiveRecord::ConnectionAdapters::Column.new("", nil)
  198
+            column_for_type_cast.instance_variable_set(:@type, type)
  199
+            casted_value = column_for_type_cast.type_cast(value)
195 200
           
196  
-          if Time.zone && casted_value.is_a?(Time)
197  
-            if value.is_a?(String)
198  
-              (casted_value + (Time.zone.utc_offset * -1)).in_time_zone
  201
+            if Time.zone && casted_value.is_a?(Time)
  202
+              if value.is_a?(String)
  203
+                (casted_value + (Time.zone.utc_offset * -1)).in_time_zone
  204
+              else
  205
+                casted_value.in_time_zone
  206
+              end
199 207
             else
200  
-              casted_value.in_time_zone
  208
+              casted_value
201 209
             end
202  
-          else
203  
-            casted_value
204 210
           end
205 211
         end
206 212
       end
207 213
       
208  
-      def ignore_value?(scope_name, value)
209  
-        mass_conditions.key?(scope_name.to_sym) && (value.is_a?(String) && value.blank?) || (value.is_a?(Array) && value.empty?)
  214
+      def ignore_value?(value)
  215
+        (value.is_a?(String) && value.blank?) || (value.is_a?(Array) && value.empty?)
210 216
       end
211 217
   end
212 218
 end
20  spec/search_spec.rb
@@ -69,6 +69,15 @@
69 69
       search.username.should == "bjohnson"
70 70
     end
71 71
     
  72
+    it "should use custom scopes before normalizing" do
  73
+      User.create(:username => "bjohnson")
  74
+      User.named_scope :username, lambda { |value| {:conditions => {:username => value.reverse}} }
  75
+      search1 = User.search(:username => "bjohnson")
  76
+      search2 = User.search(:username => "nosnhojb")
  77
+      search1.count.should == 0
  78
+      search2.count.should == 1
  79
+    end
  80
+    
72 81
     # We ignore them upon execution. But we still want to accept the condition so that returning the conditions
73 82
     # preserves the values.
74 83
     it "should not ignore blank values" do
@@ -77,13 +86,10 @@
77 86
       search.username.should == ""
78 87
     end
79 88
     
80  
-    it "should use custom scopes before normalizing" do
81  
-      User.create(:username => "bjohnson")
82  
-      User.named_scope :username, lambda { |value| {:conditions => {:username => value.reverse}} }
83  
-      search1 = User.search(:username => "bjohnson")
84  
-      search2 = User.search(:username => "nosnhojb")
85  
-      search1.count.should == 0
86  
-      search2.count.should == 1
  89
+    it "should not ignore blank values and should not cast them" do
  90
+      search = User.search
  91
+      search.conditions = {"id_equals" => ""}
  92
+      search.id_equals.should == ""
87 93
     end
88 94
     
89 95
     it "should ignore blank values in arrays" do

0 notes on commit 4a0f4b1

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