Permalink
Browse files

* Ignore polymorphic associations when dynamically creating condition…

…s on associations.
  • Loading branch information...
1 parent 83d9321 commit 9a2a3e5ac7535b186ae737558763b8f035e782a7 @binarylogic committed Jul 28, 2009
View
@@ -1,3 +1,7 @@
+== 2.1.10 released 2009-07-28
+
+* Ignore polymorphic associations when dynamically creating conditions on associations.
+
== 2.1.9 released 2009-07-28
* Fixed bug when cloning with no scope
@@ -49,11 +49,10 @@ def method_missing(name, *args, &block)
end
def association_condition_details(name)
- regexes = [/^(#{reflect_on_all_associations.collect(&:name).join("|")})_(\w+)_(#{Conditions::PRIMARY_CONDITIONS.join("|")})$/]
- reflect_on_all_associations.each do |assoc|
- regexes << /^(#{assoc.name})_(#{assoc.klass.scopes.keys.join("|")})$/
- end
-
+ 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("|")})$/ }
if !local_condition?(name) && regexes.any? { |regex| name.to_s =~ regex }
{:association => $1, :column => $2, :condition => $3}
@@ -67,12 +66,19 @@ def create_association_condition(association_name, column, condition, args)
end
def association_alias_condition_details(name)
- associations = reflect_on_all_associations.collect { |assoc| assoc.name }
- if !local_condition?(name) && name.to_s =~ /^(#{associations.join("|")})_(\w+)_(#{Conditions::ALIAS_CONDITIONS.join("|")})$/
+ if !local_condition?(name) && name.to_s =~ association_searchlogic_regex(non_polymorphic_associations, Conditions::ALIAS_CONDITIONS)
{:association => $1, :column => $2, :condition => $3}
end
end
+ def non_polymorphic_associations
+ reflect_on_all_associations.reject { |assoc| assoc.options[:polymorphic] }
+ end
+
+ def association_searchlogic_regex(assocs, condition_names)
+ /^(#{assocs.collect(&:name).join("|")})_(\w+)_(#{condition_names.join("|")})$/
+ end
+
def create_association_alias_condition(association, column, condition, args)
primary_condition = primary_condition(condition)
alias_name = "#{association}_#{column}_#{condition}"
@@ -13,6 +13,10 @@
Company.users_uname("bjohnson").proxy_options.should == User.uname("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
+
it "should not allow named scopes on non existent association columns" do
lambda { User.users_whatever_like("bjohnson") }.should raise_error(NoMethodError)
end
View
@@ -27,6 +27,12 @@
t.integer :age
end
+ create_table :carts do |t|
+ t.datetime :created_at
+ t.datetime :updated_at
+ t.integer :user_id
+ end
+
create_table :orders do |t|
t.datetime :created_at
t.datetime :updated_at
@@ -36,6 +42,14 @@
t.float :total
end
+ create_table :fees do |t|
+ t.datetime :created_at
+ t.datetime :updated_at
+ t.string :owner_type
+ t.integer :owner_id
+ t.float :cost
+ end
+
create_table :line_items do |t|
t.datetime :created_at
t.datetime :updated_at
@@ -66,6 +80,10 @@ class Order < ActiveRecord::Base
has_many :line_items, :dependent => :destroy
end
+ class Fee < ActiveRecord::Base
+ belongs_to :owner, :polymorphic => true
+ end
+
class LineItem < ActiveRecord::Base
belongs_to :order
end

0 comments on commit 9a2a3e5

Please sign in to comment.