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

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

And some minor refactoring.

diff --git a/lib/discourse_dev/post.rb b/lib/discourse_dev/post.rb
index 74d9bb8..76c8954 100644
--- a/lib/discourse_dev/post.rb
+++ b/lib/discourse_dev/post.rb
@@ -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)
diff --git a/lib/discourse_dev/record.rb b/lib/discourse_dev/record.rb
index 5713178..b8b07d2 100644
--- a/lib/discourse_dev/record.rb
+++ b/lib/discourse_dev/record.rb
@@ -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
-        end
+          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"
+          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
       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
 
diff --git a/lib/discourse_dev/topic.rb b/lib/discourse_dev/topic.rb
index 64bc86f..b15d7fc 100644
--- a/lib/discourse_dev/topic.rb
+++ b/lib/discourse_dev/topic.rb
@@ -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

GitHub sha: 74b37301438b3d31fa3a3215d9083c13fbd0d308

This commit appears in #13739 which was approved by techAPJ. It was merged by vinothkannans.