Permalink
Browse files

added support for multiparameters attributes

Signed-off-by: Ben Johnson <bjohnson@binarylogic.com>
  • Loading branch information...
1 parent ad3b20d commit 1818f1c48ab704982ccb3a7bbf21834a9cbae45f @jhchabran jhchabran committed with Aug 7, 2009
Showing with 39 additions and 1 deletion.
  1. +21 −1 lib/searchlogic/search.rb
  2. +18 −0 spec/search_spec.rb
View
@@ -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,
@@ -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
View
@@ -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.