Skip to content

Commit

Permalink
It shouldnt cast ignored values, those changing them to non-ignored v…
Browse files Browse the repository at this point in the history
…alues
  • Loading branch information
binarylogic committed Feb 3, 2010
1 parent 049861f commit 4a0f4b1
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 24 deletions.
40 changes: 23 additions & 17 deletions lib/searchlogic/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ 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))
else
Expand All @@ -106,8 +107,11 @@ def method_missing(name, *args, &block)
scope = conditions_array.inject(klass.scoped(current_scope) || {}) do |scope, condition|
scope_name, value = condition

value.delete_if { |v| ignore_value?(scope_name, v) } if value.is_a?(Array)
if !ignore_value?(scope_name, value)
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)
Expand All @@ -123,8 +127,6 @@ def method_missing(name, *args, &block)
else
scope.send(scope_name, value)
end
else
klass.scoped({})
end
end
scope.send(name, *args, &block)
Expand Down Expand Up @@ -187,26 +189,30 @@ def type_cast(value, type)
when Range
Range.new(type_cast(value.first, type), type_cast(value.last, type))
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)
if 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)

if Time.zone && casted_value.is_a?(Time)
if value.is_a?(String)
(casted_value + (Time.zone.utc_offset * -1)).in_time_zone
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
else
casted_value.in_time_zone
casted_value
end
else
casted_value
end
end
end

def ignore_value?(scope_name, value)
mass_conditions.key?(scope_name.to_sym) && (value.is_a?(String) && value.blank?) || (value.is_a?(Array) && value.empty?)
def ignore_value?(value)
(value.is_a?(String) && value.blank?) || (value.is_a?(Array) && value.empty?)
end
end
end
20 changes: 13 additions & 7 deletions spec/search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@
search.username.should == "bjohnson"
end

it "should use custom scopes before normalizing" do
User.create(:username => "bjohnson")
User.named_scope :username, lambda { |value| {:conditions => {:username => value.reverse}} }
search1 = User.search(:username => "bjohnson")
search2 = User.search(:username => "nosnhojb")
search1.count.should == 0
search2.count.should == 1
end

# 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
Expand All @@ -77,13 +86,10 @@
search.username.should == ""
end

it "should use custom scopes before normalizing" do
User.create(:username => "bjohnson")
User.named_scope :username, lambda { |value| {:conditions => {:username => value.reverse}} }
search1 = User.search(:username => "bjohnson")
search2 = User.search(:username => "nosnhojb")
search1.count.should == 0
search2.count.should == 1
it "should not ignore blank values and should not cast them" do
search = User.search
search.conditions = {"id_equals" => ""}
search.id_equals.should == ""
end

it "should ignore blank values in arrays" do
Expand Down

0 comments on commit 4a0f4b1

Please sign in to comment.