public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
remember created records and select a random one instead of relying on 
sequential id values starting at 1

Signed-off-by: Michael Koziarski <michael@koziarski.com>
S. Brent Faulkner (author)
Sun Aug 17 17:43:15 -0700 2008
NZKoz (committer)
Thu Aug 21 10:33:26 -0700 2008
commit  bbedb6a624a3d9eb02e0470f31cda8112df06d75
tree    c405416746a160e0fd5c86cef24a60e77f7d93e3
parent  aee14630d4dc0856e597794cc731fac68c2d2e34
...
1
2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
4
5
...
8
9
10
 
11
12
13
 
14
15
16
 
17
18
19
20
 
21
22
23
24
 
25
26
27
 
28
29
30
 
31
32
33
...
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
 
 
 
65
66
67
68
 
 
 
 
 
69
70
71
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
...
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
...
71
72
73
 
 
 
 
 
 
74
75
76
77
 
 
 
78
79
80
81
 
 
 
82
83
84
85
86
87
88
89
0
@@ -1,5 +1,20 @@
0
 require 'cases/helper'
0
 
0
+module Remembered
0
+  def self.included(base)
0
+    base.extend ClassMethods
0
+    base.class_eval do
0
+      after_create :remember
0
+    protected
0
+      def remember; self.class.remembered << self; end
0
+    end
0
+  end
0
+
0
+  module ClassMethods
0
+    def remembered; @@remembered ||= []; end
0
+    def rand; @@remembered.rand; end
0
+  end
0
+end
0
 
0
 class ShapeExpression < ActiveRecord::Base
0
   belongs_to :shape, :polymorphic => true
0
@@ -8,26 +23,33 @@ end
0
 
0
 class Circle < ActiveRecord::Base
0
   has_many :shape_expressions, :as => :shape
0
+  include Remembered
0
 end
0
 class Square < ActiveRecord::Base
0
   has_many :shape_expressions, :as => :shape
0
+  include Remembered
0
 end
0
 class Triangle < ActiveRecord::Base
0
   has_many :shape_expressions, :as => :shape
0
+  include Remembered
0
 end
0
 class PaintColor  < ActiveRecord::Base
0
   has_many   :shape_expressions, :as => :paint
0
   belongs_to :non_poly, :foreign_key => "non_poly_one_id", :class_name => "NonPolyOne"
0
+  include Remembered
0
 end
0
 class PaintTexture < ActiveRecord::Base
0
   has_many   :shape_expressions, :as => :paint
0
   belongs_to :non_poly, :foreign_key => "non_poly_two_id", :class_name => "NonPolyTwo"
0
+  include Remembered
0
 end
0
 class NonPolyOne < ActiveRecord::Base
0
   has_many :paint_colors
0
+  include Remembered
0
 end
0
 class NonPolyTwo < ActiveRecord::Base
0
   has_many :paint_textures
0
+  include Remembered
0
 end
0
 
0
 
0
@@ -49,23 +71,19 @@ class EagerLoadPolyAssocsTest < ActiveRecord::TestCase
0
   end
0
 
0
 
0
-  # meant to be supplied as an ID, never returns 0
0
-  def rand_simple
0
-    val = (NUM_SIMPLE_OBJS * rand).round
0
-    val == 0 ? 1 : val
0
-  end
0
-
0
   def generate_test_object_graphs
0
     1.upto(NUM_SIMPLE_OBJS) do
0
       [Circle, Square, Triangle, NonPolyOne, NonPolyTwo].map(&:create!)
0
     end
0
-    1.upto(NUM_SIMPLE_OBJS) do |i|
0
-      PaintColor.create!(:non_poly_one_id => rand_simple)
0
-      PaintTexture.create!(:non_poly_two_id => rand_simple)
0
+    1.upto(NUM_SIMPLE_OBJS) do
0
+      PaintColor.create!(:non_poly_one_id => NonPolyOne.rand.id)
0
+      PaintTexture.create!(:non_poly_two_id => NonPolyTwo.rand.id)
0
     end
0
-    1.upto(NUM_SHAPE_EXPRESSIONS) do |i|
0
-      ShapeExpression.create!(:shape_type => [Circle, Square, Triangle].rand.to_s, :shape_id => rand_simple,
0
-                              :paint_type => [PaintColor, PaintTexture].rand.to_s, :paint_id => rand_simple)
0
+    1.upto(NUM_SHAPE_EXPRESSIONS) do
0
+      shape_type = [Circle, Square, Triangle].rand
0
+      paint_type = [PaintColor, PaintTexture].rand
0
+      ShapeExpression.create!(:shape_type => shape_type.to_s, :shape_id => shape_type.rand.id,
0
+                              :paint_type => paint_type.to_s, :paint_id => paint_type.rand.id)
0
     end
0
   end
0
 

Comments