2 require 'models/author'
4 require 'models/comment'
5 require 'models/category'
6 require 'models/categorization'
9 def self.included(base
)
10 base
.extend ClassMethods
12 after_create
:remember
14 def remember
; self.class.remembered
<< self; end
19 def remembered
; @
@remembered ||= []; end
20 def rand
; @
@remembered.rand
; end
24 class ShapeExpression
< ActiveRecord
::Base
25 belongs_to
:shape, :polymorphic => true
26 belongs_to
:paint, :polymorphic => true
29 class Circle
< ActiveRecord
::Base
30 has_many
:shape_expressions, :as => :shape
33 class Square
< ActiveRecord
::Base
34 has_many
:shape_expressions, :as => :shape
37 class Triangle
< ActiveRecord
::Base
38 has_many
:shape_expressions, :as => :shape
41 class PaintColor
< ActiveRecord
::Base
42 has_many
:shape_expressions, :as => :paint
43 belongs_to
:non_poly, :foreign_key => "non_poly_one_id", :class_name => "NonPolyOne"
46 class PaintTexture
< ActiveRecord
::Base
47 has_many
:shape_expressions, :as => :paint
48 belongs_to
:non_poly, :foreign_key => "non_poly_two_id", :class_name => "NonPolyTwo"
51 class NonPolyOne
< ActiveRecord
::Base
52 has_many
:paint_colors
55 class NonPolyTwo
< ActiveRecord
::Base
56 has_many
:paint_textures
62 class EagerLoadPolyAssocsTest
< ActiveRecord
::TestCase
64 NUM_SHAPE_EXPRESSIONS
= 100
67 generate_test_object_graphs
71 [Circle
, Square
, Triangle
, PaintColor
, PaintTexture
,
72 ShapeExpression
, NonPolyOne
, NonPolyTwo
].each
do |c
|
79 def generate_test_object_graphs
80 1.upto(NUM_SIMPLE_OBJS
) do
81 [Circle
, Square
, Triangle
, NonPolyOne
, NonPolyTwo
].map(&:create!)
83 1.upto(NUM_SIMPLE_OBJS
) do
84 PaintColor
.create
!(:non_poly_one_id => NonPolyOne
.rand
.id
)
85 PaintTexture
.create
!(:non_poly_two_id => NonPolyTwo
.rand
.id
)
87 1.upto(NUM_SHAPE_EXPRESSIONS
) do
88 shape_type
= [Circle
, Square
, Triangle
].rand
89 paint_type
= [PaintColor
, PaintTexture
].rand
90 ShapeExpression
.create
!(:shape_type => shape_type
.to_s
, :shape_id => shape_type
.rand
.id
,
91 :paint_type => paint_type
.to_s
, :paint_id => paint_type
.rand
.id
)
95 def test_include_query
97 res
= ShapeExpression
.find
:all, :include => [ :shape, { :paint => :non_poly } ]
98 assert_equal NUM_SHAPE_EXPRESSIONS
, res
.size
101 assert_not_nil se
.paint
.non_poly
, "this is the association that was loading incorrectly before the change"
102 assert_not_nil se
.shape
, "just making sure other associations still work"
108 class EagerLoadNestedIncludeWithMissingDataTest
< ActiveRecord
::TestCase
110 @davey_mcdave = Author
.create(:name => 'Davey McDave')
111 @first_post = @davey_mcdave.posts
.create(:title => 'Davey Speaks', :body => 'Expressive wordage')
112 @first_comment = @first_post.comments
.create(:body => 'Inflamatory doublespeak')
113 @first_categorization = @davey_mcdave.categorizations
.create(:category => Category
.first
, :post => @first_post)
117 @davey_mcdave.destroy
119 @first_comment.destroy
120 @first_categorization.destroy
123 def test_missing_data_in_a_nested_include_should_not_cause_errors_when_constructing_objects
124 assert_nothing_raised
do
125 # @davey_mcdave doesn't have any author_favorites
126 includes
= {:posts => :comments, :categorizations => :category, :author_favorites => :favorite_author }
127 Author
.all
:include => includes
, :conditions => {:authors => {:name => @davey_mcdave.name
}}, :order => 'categories.name'