Permalink
Browse files

Make sure association conditions are being type casted in the SearchP…

…roxy
  • Loading branch information...
1 parent 51d707c commit ef1e32a12b18d9e4a5663e4f58c200ab6153a112 @binarylogic committed Jun 10, 2009
Showing with 41 additions and 7 deletions.
  1. +14 −1 lib/searchlogic/named_scopes/associations.rb
  2. +27 −6 spec/search_proxy_spec.rb
@@ -1,6 +1,18 @@
module Searchlogic
module NamedScopes
module Associations
+ def condition?(name)
+ super || association_condition?(name) || association_alias_condition?(name)
+ end
+
+ def association_condition?(name)
+ !association_condition_details(name).nil?
+ end
+
+ def association_alias_condition?(name)
+ !association_alias_condition_details(name).nil?
+ end
+
private
def method_missing(name, *args, &block)
if details = association_condition_details(name)
@@ -57,6 +69,7 @@ def create_association_ordering_condition(association_name, order_as, column, ar
def association_condition_options(association_name, association_condition, args)
association = reflect_on_association(association_name.to_sym)
scope = association.klass.send(association_condition, *args)
+ scope_options = association.klass.named_scope_options(association_condition)
arity = association.klass.named_scope_arity(association_condition)
if !arity || arity == 0
@@ -72,7 +85,7 @@ def association_condition_options(association_name, association_condition, args)
proc_args = []
arity.times { |i| proc_args << "arg#{i}"}
eval <<-"end_eval"
- lambda { |#{proc_args.join(",")}|
+ searchlogic_lambda(:#{scope_options.searchlogic_arg_type}) { |#{proc_args.join(",")}|
options = association.klass.named_scope_options(association_condition).call(#{proc_args.join(",")})
add_left_outer_join(options, association)
options
View
@@ -39,12 +39,26 @@
search.username.should == "bjohnson"
end
- it "should allow setting conditions individually" do
+ it "should allow setting local column conditions individually" do
search = User.search
search.username_gt = "bjohnson"
search.username_gt.should == "bjohnson"
end
+ it "should allow setting association conditions" do
+ search = User.search
+ search.orders_total_gt = 10
+ search.orders_total_gt.should == 10
+ end
+
+ it "should allow using custom conditions" do
+ User.named_scope(:four_year_olds, { :conditions => { :age => 4 } })
+ search = User.search
+ search.four_year_olds = true
+ search.four_year_olds.should == true
+ search.proxy_options.should == User.four_year_olds.proxy_options
+ end
+
it "should not merge conflicting conditions into one value" do
# This class should JUST be a proxy. It should not do anything more than that.
# A user would be allowed to call both named scopes if they wanted.
@@ -157,6 +171,18 @@
search.id_equals_any = ["1", "2", "3"]
search.id_equals_any.should == [1, 2, 3]
end
+
+ it "should type cast association conditions" do
+ search = User.search
+ search.orders_total_gt = "10"
+ search.orders_total_gt.should == 10
+ end
+
+ it "should type cast deep association conditions" do
+ search = Company.search
+ search.users_orders_total_gt = "10"
+ search.users_orders_total_gt.should == 10
+ end
end
end
@@ -187,11 +213,6 @@
User.search(:username => "joe").proxy_options.should == User.username_equals("joe").proxy_options
end
- it "should recognize existing named scopes" do
- User.named_scope(:four_year_olds, { :conditions => { :age => 4 } })
- User.search(:four_year_olds => true).proxy_options.should == User.four_year_olds.proxy_options
- end
-
it "should recognize conditions with a value of true where the named scope has an arity of 0" do
User.search(:username_nil => true).proxy_options.should == User.username_nil.proxy_options
end

0 comments on commit ef1e32a

Please sign in to comment.