public
Description: The open source social networking platform in Ruby on Rails from the author of RailsSpace
Homepage: http://insoshi.com
Clone URL: git://github.com/insoshi/insoshi.git
Search Repo:
Fixed up sample data with forum topics with random pick
(patching rand along the way)
Fixed Forum post associations
Michael Hartl (author)
Mon Feb 25 19:33:31 -0800 2008
commit  e235a9d4c0c08a68ae796aaee2528b9d13aa86b5
tree    b6c1dd7fec8c48da5ed84bfc197b65826ff9dc25
parent  a03c18218a6cb76b9a93ef541b434022425ef008
...
1
2
3
4
 
 
5
6
7
...
1
2
3
 
4
5
6
7
8
0
@@ -1,7 +1,8 @@
0
 class Topic < ActiveRecord::Base
0
   belongs_to :forum, :counter_cache => true
0
   belongs_to :person
0
- has_many :posts, :dependent => :destroy
0
+ has_many :posts, :order => :created_at, :dependent => :destroy,
0
+ :class_name => "ForumPost"
0
   
0
   validates_presence_of :name, :forum, :person
0
   validates_length_of :name, :maximum => MAX_STRING_LENGTH
...
15
16
17
18
 
19
20
21
...
15
16
17
 
18
19
20
21
0
@@ -15,7 +15,7 @@
0
 
0
 <p>
0
   <b>Posts count:</b>
0
- <%=h @topic.posts_count %>
0
+ <%=h @topic.forum_posts_count %>
0
 </p>
0
 
0
 
...
1
2
 
...
1
2
3
0
@@ -1,3 +1,4 @@
0
 require 'digest/sha1'
0
 require 'ferret_mixin'
0
+require 'rand'
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
0
@@ -1 +1,503 @@
0
+#
0
+# = rand.rb -- library for picking random elements and shuffling
0
+#
0
+# Copyright (C) 2004 Ilmari Heikkinen
0
+#
0
+# Documentation:: Christian Neukirchen <mailto:chneukirchen@gmail.com>
0
+#
0
+
0
+
0
+module Enumerable
0
+ # Choose and return a random element of the Enumerable.
0
+ # [1, 2, 3, 4].pick #=> 2 (or 1, 3, 4)
0
+ def pick
0
+ entries.pick
0
+ end
0
+
0
+ # Return an array of the elements in random order.
0
+ # [1, 2, 3, 4].shuffle #=> [3, 4, 1, 2]
0
+ def shuffle
0
+ entries.shuffle
0
+ end
0
+
0
+ # Calls _block_ once for each element in _self_ in random order,
0
+ # passing that element as a parameter.
0
+ def each_random(&block)
0
+ shuffle.each(&block)
0
+ end
0
+
0
+ # Invokes _block_ once for each element of _self_ in random order.
0
+ # Creates a new array containing the values returned by the block.
0
+ def map_random(&block)
0
+ shuffle.map(&block)
0
+ end
0
+end
0
+
0
+
0
+class Array
0
+ # Choose and return a random element of _self_.
0
+ # [1, 2, 3, 4].pick #=> 2 (or 1, 3, 4)
0
+ def pick
0
+ self[pick_index]
0
+ end
0
+
0
+ # Deletes a random element of _self_, returning that element.
0
+ # a = [1, 2, 3, 4]
0
+ # a.pick #=> 2
0
+ # a #=> [1, 3, 4]
0
+ def pick!
0
+ i = pick_index
0
+ rv = self[i]
0
+ delete_at(i)
0
+ rv
0
+ end
0
+
0
+ # Return the index of an random element of _self_.
0
+ # ["foo", "bar", "baz"].pick_index #=> 1 (or 0, or 2)
0
+ def pick_index
0
+ Kernel.rand(size)
0
+ end
0
+
0
+ # Destructive pick_index. Delete a random element of _self_ and
0
+ # return its index.
0
+ # a = [11, 22, 33, 44]
0
+ # a.pick_index! #=> 2
0
+ # a #=> [11, 22, 44]
0
+ def pick_index!
0
+ i = pick_index
0
+ delete_at i
0
+ i
0
+ end
0
+
0
+ # Return a random element of _self_ with its index.
0
+ # a = ["a", "b", "c", "d"]
0
+ # a.pick_with_index #=> ["b", 1]
0
+ # a #=> ["a", "b", "c", "d"]
0
+ def pick_with_index
0
+ i = pick_index
0
+ [self[i], i]
0
+ end
0
+
0
+ # Delete and return a random element of _self_ with its index.
0
+ # a = ["a", "b", "c", "d"]
0
+ # a.pick_with_index! #=> ["b", 1]
0
+ # a #=> ["a", "c", "d"]
0
+ def pick_with_index!
0
+ rv = pick_with_index
0
+ delete_at rv[1]
0
+ rv
0
+ end
0
+
0
+ # Return an array of the elements in random order.
0
+ # [11, 22, 33, 44].shuffle #=> [33, 11, 44, 22]
0
+ def shuffle
0
+ dup.shuffle!
0
+ end
0
+
0
+ # Destructive shuffle. Arrange the elements of _self_ in new order.
0
+ # Using Fisher-Yates shuffle.
0
+ # a = [11, 22, 33, 44]
0
+ # a.shuffle!
0
+ # a #=> [33, 11, 44, 22]
0
+ def shuffle!
0
+ (size-1).downto(1) {|index|
0
+ other_index = Kernel.rand(index+1)
0
+ next if index == other_index
0
+ tmp = self[index]
0
+ self[index] = self[other_index]
0
+ self[other_index] = tmp
0
+ }
0
+ self
0
+ end
0
+end
0
+
0
+
0
+class Hash
0
+ # Choose and return a random key-value pair of _self_.
0
+ # {:one => 1, :two => 2, :three => 3}.pick #=> [:one, 1]
0
+ def pick
0
+ k = keys.pick
0
+ [k, self[k]]
0
+ end
0
+
0
+ # Deletes a random key-value pair of _self_, returning that pair.
0
+ # a = {:one => 1, :two => 2, :three => 3}
0
+ # a.pick #=> [:two, 2]
0
+ # a #=> {:one => 1, :three => 3}
0
+ def pick!
0
+ rv = pick
0
+ delete rv.first
0
+ rv
0
+ end
0
+
0
+ # Return a random key of _self_.
0
+ # {:one => 1, :two => 2, :three => 3}.pick_key #=> :three
0
+ def pick_key
0
+ keys.pick
0
+ end
0
+
0
+ # Return a random value of _self_.
0
+ # {:one => 1, :two => 2, :three => 3}.pick_value #=> 3
0
+ def pick_value
0
+ values.pick
0
+ end
0
+
0
+ # Delete a random key-value pair of _self_ and return the key.
0
+ # a = {:one => 1, :two => 2, :three => 3}
0
+ # a.pick_key! #=> :two
0
+ # a #=> {:one => 1, :three => 3}
0
+ def pick_key!
0
+ pick!.first
0
+ end
0
+
0
+ # Delete a random key-value pair of _self_ and return the value.
0
+ # a = {:one => 1, :two => 2, :three => 3}
0
+ # a.pick_value! #=> 2
0
+ # a #=> {:one => 1, :three => 3}
0
+ def pick_value!
0
+ pick!.last
0
+ end
0
+
0
+ # Return the key-value pairs of _self_ with _keys_ and _values_
0
+ # shuffled independedly.
0
+ # {:one => 1, :two => 2, :three => 3}.shuffle_hash_pairs
0
+ # #=> [[:one, 3], [:two, 1], [:three, 2]]
0
+ def shuffle_hash_pairs
0
+ keys.shuffle.zip(values.shuffle)
0
+ end
0
+
0
+ # Return a copy of _self_ with _values_ arranged in random order.
0
+ # {:one => 1, :two => 2, :three => 3}.shuffle_hash
0
+ # #=> {:two=>2, :three=>1, :one=>3}
0
+ def shuffle_hash
0
+ shuffled = {}
0
+ shuffle_hash_pairs.each{|k, v|
0
+ shuffled[k] = v
0
+ }
0
+ shuffled
0
+ end
0
+
0
+ # Destructive shuffle_hash. Arrange the values of _self_ in
0
+ # new, random order.
0
+ # h = {:one => 1, :two => 2, :three => 3}
0
+ # h.shuffle_hash!
0
+ # h #=> {:two=>2, :three=>1, :one=>3}
0
+ def shuffle_hash!
0
+ shuffle_hash_pairs.each{|k, v|
0
+ self[k] = v
0
+ }
0
+ self
0
+ end
0
+end
0
+
0
+
0
+class String
0
+
0
+ # Return the string with characters arranged in random order.
0
+ # "Ruby rules".shuffle_chars #=> "e lybRsuur"
0
+ def shuffle_chars
0
+ dup.shuffle_chars!
0
+ end
0
+
0
+ # Destructive shuffle_chars. Arrange the characters of the string
0
+ # in new, random order.
0
+ # s = "Ruby rules".shuffle_chars
0
+ # s.shuffle_chars!
0
+ # s #=> "e lybRsuur"
0
+ def shuffle_chars!
0
+ (0...size).each {|j|
0
+ i = Kernel.rand(size-j)
0
+ self[j], self[j+i] = self[j+i], self[j]
0
+ }
0
+ self
0
+ end
0
+
0
+ # Return a random byte of _self_.
0
+ # "Ruby rules".pick_byte #=> 121
0
+ def pick_byte
0
+ self[pick_index]
0
+ end
0
+
0
+ # Return a single-character string of a random character in _self_.
0
+ # "Ruby rules".pick_char #=> "y"
0
+ def pick_char
0
+ pick_byte.chr
0
+ end
0
+
0
+ # Destructive pick_char. Delete a random character of the string
0
+ # and return it as a single-character string.
0
+ # s = "Ruby rules"
0
+ # s.pick_char! #=> "y"
0
+ # s #=> "Rub rules"
0
+ def pick_char!
0
+ i = pick_index
0
+ rv = self[i,1]
0
+ self[i,1] = ""
0
+ rv
0
+ end
0
+
0
+ # Destructive pick_byte. Delete a random byte of _self_ and return it.
0
+ # s = "Ruby rules"
0
+ # s.pick_byte! #=> 121
0
+ # s #=> "Rub rules"
0
+ def pick_byte!
0
+ pick_char![0]
0
+ end
0
+
0
+ # Return a random byte index of _self_.
0
+ # "Ruby rules".pick_index #=> 3
0
+ def pick_index
0
+ Kernel.rand(size)
0
+ end
0
+
0
+ # Destructive pick_index. Delete a random byte of _self_ and
0
+ # return it's index.
0
+ # s = "Ruby rules"
0
+ # s.pick_index #=> 3
0
+ # s #=> "Rub rules"
0
+ def pick_index!
0
+ i = pick_index
0
+ self[i,1] = ""
0
+ i
0
+ end
0
+
0
+ # Return a two element array consisting of an random byte of _self_
0
+ # and it's index.
0
+ # "Ruby rules".pick_byte_with_index #=> [121, 3]
0
+ def pick_byte_with_index
0
+ i = pick_index
0
+ [self[i], i]
0
+ end
0
+
0
+ # Destructive pick_byte_with_index. Delete a random byte of _self_
0
+ # and return it and it's index.
0
+ # s = "Ruby rules"
0
+ # s.pick_byte_with index! #=> [121, 3]
0
+ # s #=> "Rub rules"
0
+ def pick_byte_with_index!
0
+ rv = pick_byte_with_index
0
+ delete_at(rv[0])
0
+ rv
0
+ end
0
+
0
+ # Return a single-character string of a random character in _self_
0
+ # and it's index.
0
+ # "Ruby rules".pick_char_with_index #=> ["y", 3]
0
+ def pick_char_with_index
0
+ byte, index = pick_byte_with_index
0
+ [byte.chr, index]
0
+ end
0
+
0
+ # Destructive pick_char_with_index. Delete a random character of
0
+ # the string and return it as a single-character string together
0
+ # with it's index.
0
+ # s = "Ruby rules"
0
+ # s.pick_char_with_index! #=> ["y", 3]
0
+ # s #=> "Rub rules"
0
+ def pick_char_with_index!
0
+ byte, index = pick_byte_with_index!
0
+ [byte.chr, index]
0
+ end
0
+
0
+ def pick
0
+ to_a.pick
0
+ end
0
+
0
+ def pick!
0
+ pick_with_index![0]
0
+ end
0
+
0
+ def pick_with_index
0
+ to_a.pick_with_index
0
+ end
0
+
0
+ def pick_with_index!
0
+ tokens = to_a
0
+ s, index = tokens.pick_with_index
0
+ start = tokens[0, index].join
0
+ self[start.size, s.size] = ""
0
+ [s, index]
0
+ end
0
+end
0
+
0
+
0
+if __FILE__ == $0
0
+
0
+require 'test/unit'
0
+
0
+module RandTestHelpers # :nodoc:
0
+ def picker(enum, enum_check, method, n=50)
0
+ (1..n).all?{ enum_check.include? enum.send(method) }
0
+ end
0
+
0
+ def try_shuffling(enum, enum_c, method)
0
+ rv = nil
0
+ 10.times{
0
+ rv = enum.send method
0
+ break if rv != enum_c
0
+ }
0
+ rv
0
+ end
0
+end
0
+
0
+
0
+class RandArrayTest < Test::Unit::TestCase # :nodoc:
0
+ include RandTestHelpers
0
+
0
+ def ar
0
+ (0..99).to_a
0
+ end
0
+
0
+ def test_pick
0
+ a = ar
0
+ results = (0...a.size).map{ a.pick }
0
+ assert true, results.all? {|r| a.include? r }
0
+ end
0
+
0
+ def test_pick!
0
+ a = ar
0
+ results = (0...a.size).map{ a.pick! }
0
+ assert true, results.sort == (0..99).to_a and a.empty?
0
+ end
0
+
0
+ def test_pick_index
0
+ a = ar
0
+ results = (0...a.size).map{ a.pick_index }
0
+ assert true, results.all? {|r| r.between?(0, a.size-1) }
0
+ end
0
+
0
+ def test_pick_index!
0
+ a = ar
0
+ # side-effect-relying block; a.size = a.size-1 after pick_index!,
0
+ # so the picked index max value is the new a.size
0
+ assert true, (0...a.size).all?{ a.pick_index!.between?(0, a.size) } and a.empty?
0
+ end
0
+
0
+ def test_shuffle
0
+ a = ar
0
+ shuffled = try_shuffling(a, a, :shuffle)
0
+ assert true, shuffled.sort == a and shuffled != a
0
+ end
0
+
0
+ def test_shuffle!
0
+ a = ar
0
+ try_shuffling(a, ar, :shuffle!)
0
+ assert true, a != ar and a.sort == ar
0
+ end
0
+end
0
+
0
+
0
+class RandHashTest < Test::Unit::TestCase # :nodoc:
0
+ include RandTestHelpers
0
+
0
+ def ha
0
+ Hash[*(1..100).to_a]
0
+ end
0
+
0
+ def test_pick
0
+ assert true, picker(ha, ha.entries, :pick)
0
+ end
0
+
0
+ def test_pick!
0
+ h = ha
0
+ assert true, picker(h, ha.entries, :pick!) and h.empty?
0
+ end
0
+
0
+ def test_pick_key
0
+ assert true, picker(ha, ha.keys, :pick_key)
0
+ end
0
+
0
+ def test_pick_key!
0
+ h = ha
0
+ assert true, picker(h, ha.keys, :pick_key!) and h.empty?
0
+ end
0
+
0
+ def test_pick_value
0
+ assert true, picker(ha, ha.values, :pick_value)
0
+ end
0
+
0
+ def test_pick_value!
0
+ h = ha
0
+ assert true, picker(h, ha.values, :pick_value!) and h.empty?
0
+ end
0
+
0
+ def test_shuffle_hash
0
+ h = ha
0
+ hs = try_shuffling(ha, h, :shuffle_hash)
0
+ assert true, hs != h and (hs.keys + hs.values).sort == (h.keys + h.values).sort
0
+ end
0
+
0
+ def test_shuffle_hash!
0
+ h = ha
0
+ hs = ha
0
+ try_shuffling(hs, h, :shuffle_hash!)
0
+ assert true, hs != h and (hs.keys + hs.values).sort == (h.keys + h.values).sort
0
+ end
0
+
0
+ def test_shuffle
0
+ h = ha
0
+ hs = try_shuffling(ha, h, :shuffle)
0
+ assert true, hs != h and hs.entries.sort == h.entries.sort
0
+ end
0
+end
0
+
0
+
0
+class RandStringTest < Test::Unit::TestCase # :nodoc:
0
+ include RandTestHelpers
0
+
0
+ def self.pick_tests(endings)
0
+ endings.each{|ending, compare_str_f|
0
+ define_method("test_pick#{ending}"){
0
+ s = str
0
+ assert true, picker(s, instance_eval(&compare_str_f), "pick#{ending}", s.size)
0
+ }
0
+ }
0
+ end
0
+
0
+ def self.pick_tests!(endings)
0
+ endings.each{|ending, compare_str_f|
0
+ define_method("test_pick#{ending}!"){
0
+ s = str
0
+ assert true, picker(s, instance_eval(&compare_str_f), "pick#{ending}!", s.size) and s.empty?
0
+ }
0
+ }
0
+ end
0
+
0
+ def str
0
+ (("a".."z").to_s + "\n") * 10
0
+ end
0
+
0
+ def test_shuffle
0
+ s = str
0
+ ss = try_shuffling(s, s.to_a, :shuffle)
0
+ assert true, ss != s.to_a and ss.sort == s.to_a.sort
0
+ end
0
+
0
+ def test_shuffle_chars
0
+ s = str
0
+ ss = try_shuffling(s, s.split(//), :shuffle_chars)
0
+ assert true, ss != s and ss.split(//).sort == s.split(//).sort
0
+ end
0
+
0
+ def test_shuffle_chars!
0
+ s = str
0
+ ss = str
0
+ try_shuffling(ss, s.split(//), :shuffle_chars!)
0
+ assert true, ss != s and ss.split(//).sort == s.split(//).sort
0
+ end
0
+
0
+ pick_tests({ "" => lambda{str.to_a},
0
+ :_char => lambda{str.split(//)},
0
+ :_byte => lambda{str.split(//).map{|c| c[0]}},
0
+ :_index => lambda{(0...str.size).to_a}
0
+ })
0
+
0
+ pick_tests!({ :_char => lambda{str.split(//)},
0
+ :_byte => lambda{str.split(//).map{|c| c[0]}},
0
+ :_index => lambda{(0...str.size).to_a}
0
+ })
0
+end
0
+
0
+
0
+end #if
...
2
3
4
5
6
7
8
9
10
11
12
13
14
...
15
16
17
18
 
19
20
 
 
21
22
23
24
...
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
 
 
59
60
61
...
70
71
72
 
 
 
 
 
 
 
 
 
 
 
73
74
75
...
2
3
4
 
 
 
 
 
 
5
6
7
8
...
9
10
11
 
12
13
 
14
15
16
17
18
19
...
37
38
39
 
 
 
40
41
42
43
44
 
 
 
 
 
 
45
46
47
48
49
...
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
0
@@ -2,12 +2,6 @@
0
 require 'active_record'
0
 require 'active_record/fixtures'
0
 
0
-class Array
0
- def shuffle
0
- sort_by { rand }
0
- end
0
-end
0
-
0
 DATA_DIRECTORY = File.join(RAILS_ROOT, "lib", "tasks", "sample_data")
0
 
0
 namespace :db do
0
0
@@ -15,9 +9,10 @@
0
   
0
     desc "Load sample data"
0
     task :load => :environment do |t|
0
- lipsum = File.open(File.join(DATA_DIRECTORY, "lipsum.txt")).read
0
+ @lipsum = File.open(File.join(DATA_DIRECTORY, "lipsum.txt")).read
0
       create_people
0
- make_messages(lipsum)
0
+ make_messages(@lipsum)
0
+ make_forum_posts
0
     end
0
       
0
     desc "Remove sample data"
0
0
@@ -42,20 +37,13 @@
0
     names = File.open(filename).readlines
0
     password = "foobar"
0
     photos = Dir.glob("lib/tasks/sample_data/#{pair[0]}_photos/*.jpg").shuffle
0
- descriptions_filename = File.join(DATA_DIRECTORY,
0
- "#{pair[0]}_descriptions.txt")
0
- descriptions = File.open(descriptions_filename).readlines.shuffle
0
     names.each_with_index do |name, i|
0
       name.strip!
0
       person = Person.create!(:email => "#{name.downcase}@michaelhartl.com",
0
                               :password => password,
0
                               :password_confirmation => password,
0
- :gender => pair[1])
0
- # For security, these attributes aren't attr_accessible, so they
0
- # have to be assigned separately.
0
- # Now make the person.
0
- person.update_attributes!(:description => descriptions[i],
0
- :name => name)
0
+ :name => name,
0
+ :description => @lipsum)
0
       Photo.create!(:uploaded_data => uploaded_file(photos[i], 'image/jpg'),
0
                     :person => person, :primary => true)
0
     end
0
@@ -70,6 +58,17 @@
0
                     :recipient => sender, :skip_send_mail => true)
0
     Message.create!(:content => text, :sender => sender,
0
                     :recipient => michael, :skip_send_mail => true)
0
+ end
0
+end
0
+
0
+def make_forum_posts
0
+ forum = Forum.find(1)
0
+ people = Person.find(:all)
0
+ %w[foo bar baz].each do |name|
0
+ topic = forum.topics.create(:name => name, :person => people.pick)
0
+ 10.times do
0
+ topic.posts.create(:body => @lipsum, :person => people.pick)
0
+ end
0
   end
0
 end
0
 

Comments

    No one has commented yet.