Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Support recent 4.0.0 active_record -> base_klass

  • Loading branch information...
commit 821626891d9336c2de77b85e3c3245e7d00a7091 1 parent ee612f4
@ernie ernie authored
View
9 lib/polyamorous/join_association.rb
@@ -6,6 +6,9 @@ def self.included(base)
alias_method_chain :initialize, :polymorphism
alias_method :equality_without_polymorphism, :==
alias_method :==, :equality_with_polymorphism
+ if base.method_defined?(:active_record)
+ alias_method :base_klass, :active_record
+ end
if ActiveRecord::VERSION::STRING =~ /^3\.0\./
alias_method_chain :association_join, :polymorphism
@@ -35,7 +38,7 @@ def swapping_reflection_klass(reflection, klass)
end
def equality_with_polymorphism(other)
- equality_without_polymorphism(other) && active_record == other.active_record
+ equality_without_polymorphism(other) && base_klass == other.base_klass
end
def build_constraint_with_polymorphism(reflection, table, key, foreign_table, foreign_key)
@@ -60,7 +63,7 @@ def association_join_with_polymorphism
parent_table = Arel::Table.new(parent.table_name, :as => parent.aliased_table_name,
:engine => arel_engine,
- :columns => parent.active_record.columns)
+ :columns => parent.base_klas.columns)
@join << parent_table[reflection.options[:foreign_type]].eq(reflection.klass.name)
end
@@ -69,4 +72,4 @@ def association_join_with_polymorphism
end
end
-end
+end
View
7 lib/polyamorous/join_dependency.rb
@@ -5,6 +5,9 @@ def self.included(base)
base.class_eval do
alias_method_chain :build, :polymorphism
alias_method_chain :graft, :polymorphism
+ if base.method_defined?(:active_record)
+ alias_method :base_klass, :active_record
+ end
end
end
@@ -58,7 +61,7 @@ def find_join_association_respecting_polymorphism(reflection, parent, klass)
unless reflection.options[:polymorphic]
association
else
- association if association.active_record == klass
+ association if association.base_klass == klass
end
end
end
@@ -72,4 +75,4 @@ def build_join_association_respecting_polymorphism(reflection, parent, klass)
end
end
-end
+end
View
2  lib/polyamorous/version.rb
@@ -1,3 +1,3 @@
module Polyamorous
- VERSION = "0.6.0"
+ VERSION = "0.6.1"
end
View
23 spec/polyamorous/join_association_spec.rb
@@ -2,5 +2,26 @@
module Polyamorous
describe JoinAssociation do
+ let(:join_dependency) { new_join_dependency Note, {} }
+ let(:parent) { join_dependency.join_base }
+ let(:reflection) { Note.reflect_on_association(:notable) }
+ subject {
+ join_dependency.build_join_association_respecting_polymorphism(
+ reflection, parent, Person
+ )
+ }
+
+ it 'respects polymorphism on equality test' do
+ subject.should eq(
+ join_dependency.build_join_association_respecting_polymorphism(
+ reflection, parent, Person
+ )
+ )
+ subject.should_not eq(
+ join_dependency.build_join_association_respecting_polymorphism(
+ reflection, parent, Article
+ )
+ )
+ end
end
-end
+end
View
10 spec/polyamorous/join_dependency_spec.rb
@@ -37,6 +37,14 @@ module Polyamorous
specify { subject.join_associations.should have(1).association }
specify { subject.join_associations.should be_all { |a| a.join_type == Arel::InnerJoin } }
specify { subject.join_associations.first.table_name.should eq 'people' }
+
+ it 'finds a join association respecting polymorphism' do
+ parent = subject.join_base
+ reflection = Note.reflect_on_association(:notable)
+ subject.find_join_association_respecting_polymorphism(
+ reflection, parent, Person
+ ).should eq subject.join_associations.first
+ end
end
context 'with polymorphic belongs_to join and nested symbol join' do
@@ -49,4 +57,4 @@ module Polyamorous
end
end
-end
+end

2 comments on commit 8216268

@jonatack
Owner

Thanks, Ernie. Perhaps a typo on line 66 of the first file, lib/polyamorous/join_association.rb:

:columns => parent.base_klas.columns)

might need to be

:columns => parent.base_klass.columns)
@ernie
Owner

Good catch. Fixed.

Please sign in to comment.
Something went wrong with that request. Please try again.