Skip to content

Commit

Permalink
* Reworked how alias conditions are created on the fly, uses scope(:f…
Browse files Browse the repository at this point in the history
…ind) instead of proxy_options to create the scope. This allows using association alias named scopes.
  • Loading branch information
binarylogic committed Jul 28, 2009
1 parent 640bce2 commit 572981a
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 6 deletions.
4 changes: 4 additions & 0 deletions 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.
Expand Down
19 changes: 15 additions & 4 deletions lib/searchlogic/named_scopes/association_conditions.rb
Expand Up @@ -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}
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions lib/searchlogic/named_scopes/conditions.rb
Expand Up @@ -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} = ?")
Expand Down
4 changes: 4 additions & 0 deletions spec/named_scopes/association_conditions_spec.rb
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions spec/search_spec.rb
Expand Up @@ -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
Expand Down

0 comments on commit 572981a

Please sign in to comment.