Class: Thredded::DatabaseSeeder
- Inherits:
-
Object
- Object
- Thredded::DatabaseSeeder
- 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
- #create_additional_messageboards ⇒ Object
- #first_messageboard ⇒ Object
- #first_user ⇒ Object
- #follow_some_topics(count: (5..10), count_users: (1..5)) ⇒ Object
- #follow_some_topics_by_user(user, count: (1..10)) ⇒ Object
- #log(message) ⇒ Object
- #posts(count: (1..1)) ⇒ Object
- #private_posts(count: (1..1)) ⇒ Object
- #private_topics(count: 1) ⇒ Object
- #read_some_topics(count: (5..10), count_users: (1..5)) ⇒ Object
- #read_some_topics_by_user(user, count: (1..10)) ⇒ Object
- #read_topic(topic, user_id) ⇒ Object
- #seed(users: 200, topics: 55, posts: (1..60)) ⇒ Object
- #topics(count: 1) ⇒ Object
- #users(count: 1) ⇒ Object
Class Method Details
.run(users: 200, topics: 55, posts: (1..60)) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/thredded/database_seeder.rb', line 19 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_messageboards ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/thredded/database_seeder.rb', line 61 def = MessageboardGroup.create!(name: 'Meta').id = [ ['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.', ], ['Praise', 'Want to tell us how great we are? This is the place.', ] ] log "Creating #{.length} additional messageboards..." .each do |(name, description, group_id)| = Messageboard.create!(name: name, description: description, messageboard_group_id: group_id) FactoryGirl.create_list(:topic, 1 + rand(3), messageboard: , with_posts: 1) end end |
#first_messageboard ⇒ Object
57 58 59 |
# File 'lib/thredded/database_seeder.rb', line 57 def @first_messageboard ||= FirstMessageboard.new(self).find_or_create end |
#first_user ⇒ Object
49 50 51 |
# File 'lib/thredded/database_seeder.rb', line 49 def first_user @first_user ||= FirstUser.new(self).find_or_create end |
#follow_some_topics(count: (5..10), count_users: (1..5)) ⇒ Object
92 93 94 95 96 97 98 99 100 101 |
# File 'lib/thredded/database_seeder.rb', line 92 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
103 104 105 106 107 |
# File 'lib/thredded/database_seeder.rb', line 103 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
45 46 47 |
# File 'lib/thredded/database_seeder.rb', line 45 def log() STDERR.puts "- #{}" end |
#posts(count: (1..1)) ⇒ Object
84 85 86 |
# File 'lib/thredded/database_seeder.rb', line 84 def posts(count: (1..1)) @posts ||= Posts.new(self).find_or_create(count: count) end |
#private_posts(count: (1..1)) ⇒ Object
88 89 90 |
# File 'lib/thredded/database_seeder.rb', line 88 def private_posts(count: (1..1)) @private_posts ||= PrivatePosts.new(self).find_or_create(count: count) end |
#private_topics(count: 1) ⇒ Object
80 81 82 |
# File 'lib/thredded/database_seeder.rb', line 80 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
109 110 111 112 113 114 115 116 117 118 |
# File 'lib/thredded/database_seeder.rb', line 109 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
120 121 122 123 124 |
# File 'lib/thredded/database_seeder.rb', line 120 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
126 127 128 129 |
# File 'lib/thredded/database_seeder.rb', line 126 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
33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/thredded/database_seeder.rb', line 33 def seed(users: 200, topics: 55, posts: (1..60)) users(count: users) topics(count: topics) private_topics(count: topics) posts(count: posts) private_posts(count: posts) follow_some_topics read_some_topics end |
#topics(count: 1) ⇒ Object
76 77 78 |
# File 'lib/thredded/database_seeder.rb', line 76 def topics(count: 1) @topics ||= Topics.new(self).find_or_create(count: count) end |
#users(count: 1) ⇒ Object
53 54 55 |
# File 'lib/thredded/database_seeder.rb', line 53 def users(count: 1) @users ||= Users.new(self).find_or_create(count: count) end |