discourse/lib/discourse_dev/reviewable.rb
Osama Sayegh 5a55c9062a
DEV: Add reviewables:populate rake task (#30540)
Adds a new reviewables:populate rake task that works in a similar fashion to the existing *:populate rake tasks. The rake task creates pending reviewable of all core types, with possibility for plugins to extend the task to populate their own reviewable types.
2025-01-03 10:05:04 +08:00

63 lines
1.5 KiB
Ruby

# frozen_string_literal: true
require "discourse_dev/record"
require "faker"
module DiscourseDev
class Reviewable < Record
def initialize(users, topics, posts)
@users = users
@topics = topics
@posts = posts
end
def self.populate!
users = create_needed_users(10)
topics = create_needed_topics(5)
posts = create_needed_posts(10, topics)
(
[ReviewableFlaggedPost, ReviewableQueuedPost, ReviewablePost, ReviewableUser] +
DiscoursePluginRegistry.discourse_dev_populate_reviewable_types.to_a
).each { |klass| klass.new(users, topics, posts).populate! }
end
private
def self.create_needed_users(count)
users = ::User.human_users.limit(count).to_a
(count - users.size).times { users << User.new.create! } if users.size < count
users
end
def self.create_needed_topics(count)
topics =
::Topic
.listable_topics
.where("id NOT IN (?)", ::Category.pluck(:topic_id))
.limit(count)
.to_a
(count - topics.size).times { topics << Topic.new.create! } if topics.size < count
topics
end
def self.create_needed_posts(count, topics)
per_topic = count / topics.size
posts = []
topics.each do |topic|
current_count = topic.posts.where("post_number > 1").count
(count - current_count).times { Post.new(topic, 1).create! } if current_count < count
posts.push(*topic.posts.where("post_number > 1").limit(per_topic).to_a)
end
posts
end
end
end