Froze rails gems
[depot.git] / vendor / rails / activerecord / test / cases / associations / eager_load_nested_include_test.rb
1 require 'cases/helper'
2
3 module Remembered
4 def self.included(base)
5 base.extend ClassMethods
6 base.class_eval do
7 after_create :remember
8 protected
9 def remember; self.class.remembered << self; end
10 end
11 end
12
13 module ClassMethods
14 def remembered; @@remembered ||= []; end
15 def rand; @@remembered.rand; end
16 end
17 end
18
19 class ShapeExpression < ActiveRecord::Base
20 belongs_to :shape, :polymorphic => true
21 belongs_to :paint, :polymorphic => true
22 end
23
24 class Circle < ActiveRecord::Base
25 has_many :shape_expressions, :as => :shape
26 include Remembered
27 end
28 class Square < ActiveRecord::Base
29 has_many :shape_expressions, :as => :shape
30 include Remembered
31 end
32 class Triangle < ActiveRecord::Base
33 has_many :shape_expressions, :as => :shape
34 include Remembered
35 end
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"
39 include Remembered
40 end
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"
44 include Remembered
45 end
46 class NonPolyOne < ActiveRecord::Base
47 has_many :paint_colors
48 include Remembered
49 end
50 class NonPolyTwo < ActiveRecord::Base
51 has_many :paint_textures
52 include Remembered
53 end
54
55
56
57 class EagerLoadPolyAssocsTest < ActiveRecord::TestCase
58 NUM_SIMPLE_OBJS = 50
59 NUM_SHAPE_EXPRESSIONS = 100
60
61 def setup
62 generate_test_object_graphs
63 end
64
65 def teardown
66 [Circle, Square, Triangle, PaintColor, PaintTexture,
67 ShapeExpression, NonPolyOne, NonPolyTwo].each do |c|
68 c.delete_all
69 end
70
71 end
72
73
74 def generate_test_object_graphs
75 1.upto(NUM_SIMPLE_OBJS) do
76 [Circle, Square, Triangle, NonPolyOne, NonPolyTwo].map(&:create!)
77 end
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)
81 end
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)
87 end
88 end
89
90 def test_include_query
91 res = 0
92 res = ShapeExpression.find :all, :include => [ :shape, { :paint => :non_poly } ]
93 assert_equal NUM_SHAPE_EXPRESSIONS, res.size
94 assert_queries(0) do
95 res.each do |se|
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"
98 end
99 end
100 end
101 end