DEV: create a sample solved topic for `dev:populate` rake task. (#149)

DEV: create a sample solved topic for dev:populate rake task. (#149)

Solved feature will be enabled on a random category and a random post will get marked as “accepted answer” on it.

diff --git a/plugin.rb b/plugin.rb
index 6ce40da..afff86d 100644
--- a/plugin.rb
+++ b/plugin.rb
@@ -615,6 +615,41 @@ SQL
     options[:refresh_stream] = true if old_category_allows != new_category_allows
   end
 
+  on(:after_populate_dev_records) do |records, type|
+    next unless SiteSetting.solved_enabled
+
+    if type == :category
+      next if SiteSetting.allow_solved_on_all_topics
+
+      solved_category = DiscourseDev::Record.random(Category.where(read_restricted: false, id: records.pluck(:id), parent_category_id: nil))
+      CategoryCustomField.create!(category_id: solved_category.id, name: "enable_accepted_answers", value: "true")
+      puts "discourse-solved enabled on category '#{solved_category.name}' (#{solved_category.id})."
+    elsif type == :topic
+      topics = Topic.where(id: records.pluck(:id))
+
+      unless SiteSetting.allow_solved_on_all_topics
+        solved_category_id = CategoryCustomField.where(name: "enable_accepted_answers", value: "true").first.category_id
+
+        unless topics.exists?(category_id: solved_category_id)
+          topics.last.update(category_id: solved_category_id)
+        end
+
+        topics = topics.where(category_id: solved_category_id)
+      end
+
+      solved_topic = DiscourseDev::Record.random(topics)
+      post = nil
+
+      if solved_topic.posts_count > 1
+        post = DiscourseDev::Record.random(solved_topic.posts.where.not(post_number: 1))
+      else
+        post = DiscourseDev::Post.new(solved_topic, 1).create!
+      end
+
+      DiscourseSolved.accept_answer!(post, post.topic.user, topic: post.topic)
+    end
+  end
+
   query = "
     WITH x AS (SELECT
       u.id user_id,

GitHub sha: 5e0ef4bd3670143cbbed536f812ed9a4bbb9f546

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