Skip to content

Commit

Permalink
added support for multiparameters attributes
Browse files Browse the repository at this point in the history
Signed-off-by: Ben Johnson <bjohnson@binarylogic.com>
  • Loading branch information
jhchabran authored and binarylogic committed Aug 10, 2009
1 parent ad3b20d commit 1818f1c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
22 changes: 21 additions & 1 deletion lib/searchlogic/search.rb
Expand Up @@ -63,9 +63,18 @@ def conditions
def conditions=(values)
values.each do |condition, value|
value.delete_if { |v| v.blank? } if value.is_a?(Array)
next if extract_multiparameter_conditions(condition, value)
next if value.blank?
send("#{condition}=", value)
end

@multi_parameters_conditions.try(:each) { |condition, values| ;send("#{condition}=", *values) }
end

def extract_multiparameter_conditions(condition, value)
return unless condition.to_s =~ /(\w+)\((\d)(\w)\)/ # ? value.send("to_" + $1) : value§
@multi_parameters_conditions ||= Hash.new{|h,k| h[k] = []}
@multi_parameters_conditions[$1][$2.to_i-1] = value.send("to_#{$3}") # first parameter is 1 ("(1i)")
end

# Delete a condition from the search. Since conditions map to named scopes,
Expand All @@ -83,7 +92,18 @@ def method_missing(name, *args, &block)
condition = $1.to_sym
scope_name = normalize_scope_name($1)
if scope?(scope_name)
conditions[condition] = type_cast(args.first, cast_type(scope_name))
if args.length == 1
conditions[condition] = type_cast(args.first, cast_type(scope_name))
else
case cast_type(scope_name)
when :time
raise 'NotImplemented'
when :date
conditions[condition] = Date.new(*args)
else
raise 'NotImplemented'
end
end
else
raise UnknownConditionError.new(name)
end
Expand Down
18 changes: 18 additions & 0 deletions spec/search_spec.rb
Expand Up @@ -320,5 +320,23 @@
it "should recognize the order condition" do
User.search(:order => "ascend_by_username").proxy_options.should == User.ascend_by_username.proxy_options
end

context "with multiple parameters" do
before(:each) do
@early_order = Order.create(:shipped_on => Date.civil(2008, 1, 1))
@late_order = Order.create(:shipped_on => Date.civil(2010, 1, 1))

@options = lambda { |attr| {"#{attr}(1i)" => "2009", "#{attr}(2i)" => "8", "#{attr}(3i)" => "1"}}
end
it "should have shipped_on after" do
Order.search(@options.call("shipped_on_after")).all.should == [@late_order]
end

it "should have shipped_on before" do
Order.search(@options.call("shipped_on_before")).all.should == [@early_order]
end

it "should test time"
end
end
end

0 comments on commit 1818f1c

Please sign in to comment.