Class: Thredded::DatabaseSeeder

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

Defined Under Namespace

Classes: BaseSeedData, CollectionSeedData, FirstMessageboard, FirstSeedData, FirstUser, Posts, PrivatePosts, PrivateTopics, Topics, Users

Constant Summary collapse

SKIP_CALLBACKS =
[
  [Thredded::Post, :commit, :after, :auto_follow_and_notify],
  [Thredded::PrivatePost, :commit, :after, :notify_users],
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.run(users: 200, topics: 55, posts: (1..60)) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/thredded/database_seeder.rb', line 21

def self.run(users: 200, topics: 55, posts: (1..60))
  STDERR.puts 'Seeding the database...'
  # Disable callbacks to avoid creating notifications and performing unnecessary updates
  SKIP_CALLBACKS.each { |(klass, *args)| klass.skip_callback(*args) }
  s = new
  Messageboard.transaction do
    s.seed(users: users, topics: topics, posts: posts)
    s.log 'Running after_commit callbacks'
  end
ensure
  # Re-enable callbacks
  SKIP_CALLBACKS.each { |(klass, *args)| klass.set_callback(*args) }
end

Instance Method Details

#create_additional_messageboardsObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/thredded/database_seeder.rb', line 63

def create_additional_messageboards
  meta_group_id = MessageboardGroup.create!(name: 'Meta').id
  additional_messageboards = [
    ['Off-Topic', "Talk about whatever here, it's all good."],
    ['Help, Bugs, and Suggestions',
     'Need help using the forum? Want to report a bug or make a suggestion? This is the place.', meta_group_id],
    ['Praise', 'Want to tell us how great we are? This is the place.', meta_group_id]
  ]
  log "Creating #{additional_messageboards.length} additional messageboards..."
  additional_messageboards.each do |(name, description, group_id)|
    messageboard = Messageboard.create!(name: name, description: description, messageboard_group_id: group_id)
    FactoryBot.create_list(:topic, 1 + rand(3), messageboard: messageboard, with_posts: 1)
  end
end

#first_messageboardObject



59
60
61
# File 'lib/thredded/database_seeder.rb', line 59

def first_messageboard
  @first_messageboard ||= FirstMessageboard.new(self).find_or_create
end

#first_userObject



51
52
53
# File 'lib/thredded/database_seeder.rb', line 51

def first_user
  @first_user ||= FirstUser.new(self).find_or_create
end

#follow_some_topics(count: (5..10), count_users: (1..5)) ⇒ Object



94
95
96
97
98
99
100
101
102
103
# File 'lib/thredded/database_seeder.rb', line 94

def follow_some_topics(count: (5..10), count_users: (1..5))
  log 'Following some topics...'
  posts.each do |post|
    Thredded::UserTopicFollow.create_unless_exists(post.user_id, post.postable_id, :posted) if post.user_id
  end
  follow_some_topics_by_user(first_user, count: count)
  users.sample(count_users.min + rand(count_users.max - count_users.min + 2)).each do |user|
    follow_some_topics_by_user(user, count: count)
  end
end

#follow_some_topics_by_user(user, count: (1..10)) ⇒ Object



105
106
107
108
109
# File 'lib/thredded/database_seeder.rb', line 105

def follow_some_topics_by_user(user, count: (1..10))
  topics.sample(count.min + rand(count.max - count.min + 2)).each do |topic|
    Thredded::UserTopicFollow.create_unless_exists(user.id, topic.id)
  end
end

#log(message) ⇒ Object



47
48
49
# File 'lib/thredded/database_seeder.rb', line 47

def log(message)
  STDERR.puts "- #{message}"
end

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



86
87
88
# File 'lib/thredded/database_seeder.rb', line 86

def posts(count: (1..1))
  @posts ||= Posts.new(self).find_or_create(count: count)
end

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



90
91
92
# File 'lib/thredded/database_seeder.rb', line 90

def private_posts(count: (1..1))
  @private_posts ||= PrivatePosts.new(self).find_or_create(count: count)
end

#private_topics(count: 1) ⇒ Object



82
83
84
# File 'lib/thredded/database_seeder.rb', line 82

def private_topics(count: 1)
  @private_topics ||= PrivateTopics.new(self).find_or_create(count: count)
end

#read_some_topics(count: (5..10), count_users: (1..5)) ⇒ Object



111
112
113
114
115
116
117
118
119
120
# File 'lib/thredded/database_seeder.rb', line 111

def read_some_topics(count: (5..10), count_users: (1..5))
  log 'Reading some topics...'
  topics.each do |topic|
    read_topic(topic, topic.last_user_id) if topic.last_user_id
  end
  read_some_topics_by_user(first_user, count: count)
  @users.sample(count_users.min + rand(count_users.max - count_users.min + 2)).each do |user|
    read_some_topics_by_user(user, count: count)
  end
end

#read_some_topics_by_user(user, count: (1..10)) ⇒ Object



122
123
124
125
126
# File 'lib/thredded/database_seeder.rb', line 122

def read_some_topics_by_user(user, count: (1..10))
  topics.sample(count.min + rand(count.max - count.min + 2)).each do |topic|
    read_topic(topic, user.id)
  end
end

#read_topic(topic, user_id) ⇒ Object



128
129
130
131
# File 'lib/thredded/database_seeder.rb', line 128

def read_topic(topic, user_id)
  Thredded::UserTopicReadState.find_or_initialize_by(user_id: user_id, postable_id: topic.id)
    .update!(read_at: topic.updated_at, page: 1)
end

#seed(users: 200, topics: 55, posts: (1..60)) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/thredded/database_seeder.rb', line 35

def seed(users: 200, topics: 55, posts: (1..60))
  users(count: users)
  first_messageboard
  topics(count: topics)
  private_topics(count: topics)
  posts(count: posts)
  private_posts(count: posts)
  create_additional_messageboards
  follow_some_topics
  read_some_topics
end

#topics(count: 1) ⇒ Object



78
79
80
# File 'lib/thredded/database_seeder.rb', line 78

def topics(count: 1)
  @topics ||= Topics.new(self).find_or_create(count: count)
end

#users(count: 1) ⇒ Object



55
56
57
# File 'lib/thredded/database_seeder.rb', line 55

def users(count: 1)
  @users ||= Users.new(self).find_or_create(count: count)
end