Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Revert recent blank condition changes. Include blank conditions when …

…calling the conditions method, everything else is as it was.
  • Loading branch information...
commit 5fae869d04e16b25cf0f41425f4588ab02935855 1 parent 0bcdfdd
@binarylogic binarylogic authored
Showing with 51 additions and 73 deletions.
  1. +42 −51 lib/searchlogic/search.rb
  2. +9 −22 spec/search_spec.rb
View
93 lib/searchlogic/search.rb
@@ -47,6 +47,7 @@ def initialize(condition)
def initialize(klass, current_scope, conditions = {})
self.klass = klass
self.current_scope = current_scope
+ @conditions ||= {}
self.conditions = conditions if conditions.is_a?(Hash)
end
@@ -56,18 +57,17 @@ def clone
# Returns a hash of the current conditions set.
def conditions
- @conditions ||= {}
+ mass_conditions.clone.merge(@conditions)
end
# Accepts a hash of conditions.
def conditions=(values)
- @setting_mass_conditions = true
- result = values.each do |condition, value|
+ values.each do |condition, value|
mass_conditions[condition.to_sym] = value
+ value.delete_if { |v| ignore_value?(v) } if value.is_a?(Array)
+ next if ignore_value?(value)
send("#{condition}=", value)
end
- @setting_mass_conditions = false
- result
end
# Delete a condition from the search. Since conditions map to named scopes,
@@ -86,19 +86,17 @@ def method_missing(name, *args, &block)
if setter?(name)
if scope?(scope_name)
- mass_conditions.delete(scope_name.to_sym) if !setting_mass_conditions?
-
if args.size == 1
- conditions[condition_name] = type_cast(args.first, cast_type(scope_name))
+ write_condition(condition_name, type_cast(args.first, cast_type(scope_name)))
else
- conditions[condition_name] = args
+ write_condition(condition_name, args)
end
else
raise UnknownConditionError.new(condition_name)
end
elsif scope?(scope_name) && args.size <= 1
if args.size == 0
- conditions[condition_name]
+ read_condition(condition_name)
else
send("#{condition_name}=", *args)
self
@@ -106,27 +104,20 @@ def method_missing(name, *args, &block)
else
scope = conditions_array.inject(klass.scoped(current_scope) || {}) do |scope, condition|
scope_name, value = condition
+ scope_name = normalize_scope_name(scope_name)
+ klass.send(scope_name, value) if !klass.respond_to?(scope_name)
+ arity = klass.named_scope_arity(scope_name)
- value.delete_if { |v| ignore_value?(v) } if mass_conditions.key?(scope_name.to_sym) && value.is_a?(Array)
-
- if mass_conditions.key?(scope_name.to_sym) && ignore_value?(value)
- klass.scoped({})
- else
- scope_name = normalize_scope_name(scope_name)
- klass.send(scope_name, value) if !klass.respond_to?(scope_name)
- arity = klass.named_scope_arity(scope_name)
-
- if !arity || arity == 0
- if value == true
- scope.send(scope_name)
- else
- scope
- end
- elsif arity == -1
- scope.send(scope_name, *(value.is_a?(Array) ? value : [value]))
+ if !arity || arity == 0
+ if value == true
+ scope.send(scope_name)
else
- scope.send(scope_name, value)
+ scope
end
+ elsif arity == -1
+ scope.send(scope_name, *(value.is_a?(Array) ? value : [value]))
+ else
+ scope.send(scope_name, value)
end
end
scope.send(name, *args, &block)
@@ -155,6 +146,18 @@ def condition_name(name)
condition ? condition[1].to_sym : nil
end
+ def write_condition(name, value)
+ @conditions[name] = value
+ end
+
+ def read_condition(name)
+ @conditions[name]
+ end
+
+ def mass_conditions
+ @mass_conditions ||= {}
+ end
+
def scope_name(condition_name)
condition_name && normalize_scope_name(condition_name)
end
@@ -174,14 +177,6 @@ def cast_type(name)
end
end
- def mass_conditions
- @mass_conditions ||= {}
- end
-
- def setting_mass_conditions?
- @setting_mass_conditions == true
- end
-
def type_cast(value, type)
case value
when Array
@@ -189,24 +184,20 @@ def type_cast(value, type)
when Range
Range.new(type_cast(value.first, type), type_cast(value.last, type))
else
- if setting_mass_conditions? && ignore_value?(value)
- value
- else
- # Let's leverage ActiveRecord's type casting, so that casting is consistent
- # with the other models.
- column_for_type_cast = ::ActiveRecord::ConnectionAdapters::Column.new("", nil)
- column_for_type_cast.instance_variable_set(:@type, type)
- casted_value = column_for_type_cast.type_cast(value)
+ # Let's leverage ActiveRecord's type casting, so that casting is consistent
+ # with the other models.
+ column_for_type_cast = ::ActiveRecord::ConnectionAdapters::Column.new("", nil)
+ column_for_type_cast.instance_variable_set(:@type, type)
+ casted_value = column_for_type_cast.type_cast(value)
- if Time.zone && casted_value.is_a?(Time)
- if value.is_a?(String)
- (casted_value + (Time.zone.utc_offset * -1)).in_time_zone
- else
- casted_value.in_time_zone
- end
+ if Time.zone && casted_value.is_a?(Time)
+ if value.is_a?(String)
+ (casted_value + (Time.zone.utc_offset * -1)).in_time_zone
else
- casted_value
+ casted_value.in_time_zone
end
+ else
+ casted_value
end
end
end
View
31 spec/search_spec.rb
@@ -80,22 +80,27 @@
# We ignore them upon execution. But we still want to accept the condition so that returning the conditions
# preserves the values.
- it "should not ignore blank values" do
+ it "should ignore blank values but still return on conditions" do
search = User.search
search.conditions = {"username" => ""}
- search.username.should == ""
+ search.username.should be_nil
+ search.conditions.should == {:username => ""}
end
it "should not ignore blank values and should not cast them" do
search = User.search
search.conditions = {"id_equals" => ""}
- search.id_equals.should == ""
+ search.id_equals.should be_nil
+ search.conditions.should == {:id_equals => ""}
end
it "should ignore blank values in arrays" do
search = User.search
search.conditions = {"username_equals_any" => [""]}
- search.username_equals_any.first.should be_blank
+ search.username_equals_any.should be_nil
+
+ search.conditions = {"id_equals_any" => ["", "1"]}
+ search.id_equals_any.should == [1]
end
end
@@ -375,24 +380,6 @@
s.created_at_after = Time.now
lambda { s.count }.should_not raise_error
end
-
- it "should ignore blank values" do
- search = User.search
- search.conditions = {"username_equals" => ""}
- search.proxy_options.should == {}
- end
-
- it "should not ignore blank values when explicitly set" do
- search = User.search
- search.username_equals = ""
- search.proxy_options.should == {:conditions => ["users.username = ?", ""]}
- end
-
- it "should ignore blank values in arrays" do
- search = User.search
- search.conditions = {"username_equals_any" => [""]}
- search.proxy_options.should == {}
- end
end
context "method delegation" do

0 comments on commit 5fae869

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