Permalink
Browse files

If the named scope has an arity of -1 expand array values, otherwise …

…pass them straight
  • Loading branch information...
binarylogic committed Jan 19, 2010
1 parent 31a3ed3 commit b5dfc0e383911c87c7e4daebf890e5d937324b80
Showing with 29 additions and 13 deletions.
  1. +7 −5 lib/searchlogic/search.rb
  2. +22 −8 spec/search_spec.rb
View
@@ -92,12 +92,12 @@ def method_missing(name, *args, &block)
else
raise UnknownConditionError.new(condition_name)
end
- elsif scope?(scope_name)
- if args.size > 0
+ elsif scope?(scope_name) && args.size <= 1
+ if args.size == 0
+ conditions[condition_name]
+ else
send("#{condition_name}=", *args)
self
- else
- conditions[condition_name]
end
else
scope = conditions.inject(klass.scoped(current_scope) || {}) do |scope, condition|
@@ -112,8 +112,10 @@ def method_missing(name, *args, &block)
else
scope
end
+ elsif arity == -1
+ scope.send(scope_name, *(value.is_a?(Array) ? value : [value]))
else
- scope.send(scope_name, *value)
+ scope.send(scope_name, value)
end
end
scope.send(name, *args, &block)
View
@@ -340,13 +340,27 @@
User.search(:order => "ascend_by_username").proxy_options.should == User.ascend_by_username.proxy_options
end
- it "should pass array values as multiple arguments" do
+ it "should pass array values as multiple arguments with arity -1" do
User.named_scope(:multiple_args, lambda { |*args|
raise "This should not be an array, it should be 1" if args.first.is_a?(Array)
{:conditions => ["id IN (?)", args]}
})
User.search(:multiple_args => [1,2]).proxy_options.should == User.multiple_args(1,2).proxy_options
end
+
+ it "should pass array as a single value with arity >= 0" do
+ User.named_scope(:multiple_args, lambda { |args|
+ raise "This should be an array" if !args.is_a?(Array)
+ {:conditions => ["id IN (?)", args]}
+ })
+ User.search(:multiple_args => [1,2]).proxy_options.should == User.multiple_args(1,2).proxy_options
+ end
+
+ it "should not split out dates or times (big fix)" do
+ s = User.search
+ s.created_at_after = Time.now
+ lambda { s.count }.should_not raise_error
+ end
end
context "method delegation" do
@@ -366,12 +380,12 @@
search2.empty?.should == true
end
- it "should delegate to named scopes with arity > 1" do
- User.named_scope :paged, lambda {|start, limit| { :limit => limit, :offset => start }}
- User.create(:username => "bjohnson")
- search = User.search(:username => "bjohnson")
- search.paged(0, 1).count.should == 1
- search.paged(0, 0).count.should == 0
- end
+ it "should delegate to named scopes with arity > 1" do
+ User.named_scope :paged, lambda {|start, limit| { :limit => limit, :offset => start }}
+ User.create(:username => "bjohnson")
+ search = User.search(:username => "bjohnson")
+ search.paged(0, 1).count.should == 1
+ search.paged(0, 0).count.should == 0
+ end
end
end

0 comments on commit b5dfc0e

Please sign in to comment.