DEV: return populated data at the end of the method. (#13739)

And some minor refactoring.
This commit is contained in:
Vinoth Kannan 2021-07-15 17:45:32 +05:30 committed by GitHub
parent 5cd447695e
commit 74b3730143
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 23 deletions

View File

@ -38,6 +38,7 @@ module DiscourseDev
post = PostCreator.new(user, data).create!
topic.reload
generate_likes(post)
post
end
def generate_likes(post)
@ -63,9 +64,11 @@ module DiscourseDev
def populate!
generate_likes(topic.first_post)
@count.times do
create!
end
super(ignore_current_count: true)
end
def current_count
topic.posts_count - 1
end
def self.add_replies!(args)

View File

@ -29,31 +29,33 @@ module DiscourseDev
record
end
def populate!
if current_count >= @count
puts "Already have #{current_count} #{type} records"
def populate!(ignore_current_count: false)
unless ignore_current_count
if current_count >= @count
puts "Already have #{current_count} #{type} records"
Rake.application.top_level_tasks.each do |task_name|
Rake::Task[task_name].reenable
Rake.application.top_level_tasks.each do |task_name|
Rake::Task[task_name].reenable
end
Rake::Task['dev:repopulate'].invoke
return
elsif current_count > 0
@count -= current_count
puts "There are #{current_count} #{type} records. Creating #{@count} more."
else
puts "Creating #{@count} sample #{type} records"
end
Rake::Task['dev:repopulate'].invoke
return
elsif current_count > 0
@count -= current_count
puts "There are #{current_count} #{type} records. Creating #{@count} more."
else
puts "Creating #{@count} sample #{type} records"
end
records = []
@count.times do
records << create!
putc "."
putc "." unless type == :post
end
puts unless type == :post
DiscourseEvent.trigger(:after_populate_dev_records, records, type)
puts
records
end

View File

@ -67,21 +67,24 @@ module DiscourseDev
def create!
@category = Category.random
user = self.user
topic = Faker::DiscourseMarkdown.with_user(user.id) { data }
post = PostCreator.new(user, topic).create!
topic_data = Faker::DiscourseMarkdown.with_user(user.id) { data }
post = PostCreator.new(user, topic_data).create!
if override = @settings.dig(:replies, :overrides).find { |o| o[:title] == topic[:title] }
if override = @settings.dig(:replies, :overrides).find { |o| o[:title] == topic_data[:title] }
reply_count = override[:count]
else
reply_count = Faker::Number.between(from: @settings.dig(:replies, :min), to: @settings.dig(:replies, :max))
end
Post.new(post.topic, reply_count).populate!
topic = post.topic
Post.new(topic, reply_count).populate!
topic
end
def populate!
super
topics = super
delete_unwanted_sidekiq_jobs
topics
end
def user