4 def self.included(base
)
5 base
.extend ClassMethods
9 def remember
; self.class.remembered
<< self; end
14 def remembered
; @
@remembered ||= []; end
15 def rand
; @
@remembered.rand
; end
19 class ShapeExpression
< ActiveRecord
::Base
20 belongs_to
:shape, :polymorphic => true
21 belongs_to
:paint, :polymorphic => true
24 class Circle
< ActiveRecord
::Base
25 has_many
:shape_expressions, :as => :shape
28 class Square
< ActiveRecord
::Base
29 has_many
:shape_expressions, :as => :shape
32 class Triangle
< ActiveRecord
::Base
33 has_many
:shape_expressions, :as => :shape
36 class PaintColor
< ActiveRecord
::Base
37 has_many
:shape_expressions, :as => :paint
38 belongs_to
:non_poly, :foreign_key => "non_poly_one_id", :class_name => "NonPolyOne"
41 class PaintTexture
< ActiveRecord
::Base
42 has_many
:shape_expressions, :as => :paint
43 belongs_to
:non_poly, :foreign_key => "non_poly_two_id", :class_name => "NonPolyTwo"
46 class NonPolyOne
< ActiveRecord
::Base
47 has_many
:paint_colors
50 class NonPolyTwo
< ActiveRecord
::Base
51 has_many
:paint_textures
57 class EagerLoadPolyAssocsTest
< ActiveRecord
::TestCase
59 NUM_SHAPE_EXPRESSIONS
= 100
62 generate_test_object_graphs
66 [Circle
, Square
, Triangle
, PaintColor
, PaintTexture
,
67 ShapeExpression
, NonPolyOne
, NonPolyTwo
].each
do |c
|
74 def generate_test_object_graphs
75 1.upto(NUM_SIMPLE_OBJS
) do
76 [Circle
, Square
, Triangle
, NonPolyOne
, NonPolyTwo
].map(&:create!)
78 1.upto(NUM_SIMPLE_OBJS
) do
79 PaintColor
.create
!(:non_poly_one_id => NonPolyOne
.rand
.id
)
80 PaintTexture
.create
!(:non_poly_two_id => NonPolyTwo
.rand
.id
)
82 1.upto(NUM_SHAPE_EXPRESSIONS
) do
83 shape_type
= [Circle
, Square
, Triangle
].rand
84 paint_type
= [PaintColor
, PaintTexture
].rand
85 ShapeExpression
.create
!(:shape_type => shape_type
.to_s
, :shape_id => shape_type
.rand
.id
,
86 :paint_type => paint_type
.to_s
, :paint_id => paint_type
.rand
.id
)
90 def test_include_query
92 res
= ShapeExpression
.find
:all, :include => [ :shape, { :paint => :non_poly } ]
93 assert_equal NUM_SHAPE_EXPRESSIONS
, res
.size
96 assert_not_nil se
.paint
.non_poly
, "this is the association that was loading incorrectly before the change"
97 assert_not_nil se
.shape
, "just making sure other associations still work"