Class: Thredded::DatabaseSeeder::Posts

Inherits:
CollectionSeedData show all
Defined in:
lib/thredded/database_seeder.rb

Constant Summary collapse

MODEL_CLASS =
Post

Instance Attribute Summary

Attributes inherited from BaseSeedData

#seeder

Instance Method Summary collapse

Methods inherited from CollectionSeedData

#find

Methods inherited from BaseSeedData

create, #find_or_create, #initialize

Constructor Details

This class inherits a constructor from Thredded::DatabaseSeeder::BaseSeedData

Instance Method Details

#create(count: (1..1)) ⇒ Object



250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/thredded/database_seeder.rb', line 250

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
      FactoryBot.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



270
271
272
# File 'lib/thredded/database_seeder.rb', line 270

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



265
266
267
268
# File 'lib/thredded/database_seeder.rb', line 265

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