Permalink
Browse files

* Reworked how alias conditions are created on the fly, uses scope(:f…

…ind) instead of proxy_options to create the scope. This allows using association alias named scopes.
  • Loading branch information...
1 parent 640bce2 commit 572981ad78fa3bd176509db9c71e959736ad41f3 @binarylogic committed Jul 28, 2009
View
4 CHANGELOG.rdoc
@@ -1,3 +1,7 @@
+== 2.1.11 released 2009-07-28
+
+* Reworked how alias conditions are created on the fly, uses scope(:find) instead of proxy_options to create the scope. This allows using association alias named scopes.
+
== 2.1.10 released 2009-07-28
* Ignore polymorphic associations when dynamically creating conditions on associations.
View
19 lib/searchlogic/named_scopes/association_conditions.rb
@@ -52,7 +52,13 @@ def association_condition_details(name)
assocs = non_polymorphic_associations
return nil if assocs.empty?
regexes = [association_searchlogic_regex(assocs, Conditions::PRIMARY_CONDITIONS)]
- assocs.each { |assoc| regexes << /^(#{assoc.name})_(#{assoc.klass.scopes.keys.join("|")})$/ }
+ assocs.each do |assoc|
+ scope_names = assoc.klass.scopes.keys + assoc.klass.alias_scopes.keys
+ scope_names.uniq!
+ scope_names.delete(:scoped)
+ next if scope_names.empty?
+ regexes << /^(#{assoc.name})_(#{scope_names.join("|")})$/
+ end
if !local_condition?(name) && regexes.any? { |regex| name.to_s =~ regex }
{:association => $1, :column => $2, :condition => $3}
@@ -66,7 +72,10 @@ def create_association_condition(association_name, column, condition, args)
end
def association_alias_condition_details(name)
- if !local_condition?(name) && name.to_s =~ association_searchlogic_regex(non_polymorphic_associations, Conditions::ALIAS_CONDITIONS)
+ assocs = non_polymorphic_associations
+ return nil if assocs.empty?
+
+ if !local_condition?(name) && name.to_s =~ association_searchlogic_regex(assocs, Conditions::ALIAS_CONDITIONS)
{:association => $1, :column => $2, :condition => $3}
end
end
@@ -96,7 +105,8 @@ def association_condition_options(association_name, association_condition, args)
if !arity || arity == 0
# The underlying condition doesn't require any parameters, so let's just create a simple
# named scope that is based on a hash.
- options = scope.proxy_options
+ options = scope.scope(:find)
+ options.delete(:readonly)
options[:joins] = options[:joins].blank? ? association.name : {association.name => options[:joins]}
options
else
@@ -121,7 +131,8 @@ def association_condition_options(association_name, association_condition, args)
eval <<-"end_eval"
searchlogic_lambda(:#{arg_type}) { |#{proc_args.join(",")}|
- options = association.klass.named_scope_options(association_condition).call(#{proc_args.join(",")})
+ options = association.klass.send(association_condition, #{proc_args.join(",")}).scope(:find)
+ options.delete(:readonly)
options[:joins] = options[:joins].blank? ? association.name : {association.name => options[:joins]}
options
}
View
4 lib/searchlogic/named_scopes/conditions.rb
@@ -140,8 +140,8 @@ def primary_condition_details(name)
def create_primary_condition(column, condition)
column_type = columns_hash[column.to_s].type
- match_keyword =
- ActiveRecord::Base.connection.adapter_name == "PostgreSQL" ? "ILIKE" : "LIKE"
+ match_keyword = ActiveRecord::Base.connection.adapter_name == "PostgreSQL" ? "ILIKE" : "LIKE"
+
scope_options = case condition.to_s
when /^equals/
scope_options(condition, column_type, "#{table_name}.#{column} = ?")
View
4 spec/named_scopes/association_conditions_spec.rb
@@ -13,6 +13,10 @@
Company.users_uname("bjohnson").proxy_options.should == User.uname("bjohnson").proxy_options.merge(:joins => :users)
end
+ it "should allow the use of foreign pre-existing alias scopes" do
+ Company.users_username_has("bjohnson").proxy_options.should == User.username_has("bjohnson").proxy_options.merge(:joins => :users)
+ end
+
it "should ignore polymorphic associations" do
lambda { Fee.owner_created_at_gt(Time.now) }.should raise_error(NoMethodError)
end
View
6 spec/search_spec.rb
@@ -109,6 +109,12 @@
search.orders_total_gt.should == 10
end
+ it "should allow setting pre-existing association conditions" do
+ search = Company.search
+ search.users_uname = "bjohnson"
+ search.users_uname.should == "bjohnson"
+ end
+
it "should allow using custom conditions" do
User.named_scope(:four_year_olds, { :conditions => { :age => 4 } })
search = User.search

0 comments on commit 572981a

Please sign in to comment.