Class: Thredded::DatabaseSeeder::Posts
Constant Summary
collapse
- MODEL_CLASS =
Post
Instance Attribute Summary
Attributes inherited from BaseSeedData
#seeder
Instance Method Summary
collapse
#find
#find_or_create, #initialize
Instance Method Details
#create(count: (1..1)) ⇒ Object
240
241
242
243
244
245
246
247
248
249
250
251
252
253
|
# File 'lib/thredded/database_seeder.rb', line 240
def create(count: (1..1))
log "Creating #{count} additional posts in each topic..."
seeder.topics.flat_map do |topic|
last_post_at = random_duration(0..72.hours).ago
posts_count = (count.min + rand(count.max + 1))
posts = range_of_dates_in_order(up_to: last_post_at, count: posts_count).map.with_index do |written_at, i|
author = i.zero? ? topic.user : seeder.users.sample
FactoryGirl.create(:post, postable: topic, messageboard: seeder.first_messageboard,
user: author, created_at: written_at, updated_at: written_at)
end
topic.update!(last_user_id: posts.last.user.id, updated_at: last_post_at, last_post_at: last_post_at)
posts
end
end
|
#random_duration(range) ⇒ Object
260
261
262
|
# File 'lib/thredded/database_seeder.rb', line 260
def random_duration(range)
(range.min.to_i + rand(range.max.to_i)).seconds
end
|
#range_of_dates_in_order(up_to: Time.zone.now, count: 1) ⇒ Object
255
256
257
258
|
# File 'lib/thredded/database_seeder.rb', line 255
def range_of_dates_in_order(up_to: Time.zone.now, count: 1)
written = up_to
Array.new(count - 1) { written -= random_duration(10.minutes..6.hours) }.reverse + [up_to]
end
|