FIX: Posts in the approval queue were sometimes dropping category

FIX: Posts in the approval queue were sometimes dropping category

diff --git a/app/models/reviewable.rb b/app/models/reviewable.rb
index d022a4c..aaa5f18 100644
--- a/app/models/reviewable.rb
+++ b/app/models/reviewable.rb
@@ -70,6 +70,13 @@ class Reviewable < ActiveRecord::Base
     %w[ReviewableFlaggedPost ReviewableQueuedPost ReviewableUser]
   end
 
+  def created_new!
+    self.created_new = true
+    self.topic = target.topic if topic.blank? && target.is_a?(Post)
+    self.target_created_by_id = target.is_a?(Post) ? target.user_id : nil
+    self.category_id = topic.category_id if category_id.blank? && topic.present?
+  end
+
   # Create a new reviewable, or if the target has already been reviewed return it to the
   # pending state and re-use it.
   #
@@ -90,11 +97,7 @@ class Reviewable < ActiveRecord::Base
       payload: payload,
       potential_spam: potential_spam
     )
-    reviewable.created_new = true
-    reviewable.topic = target.topic if reviewable.topic.blank? && target.is_a?(Post)
-    reviewable.target_created_by_id = target.is_a?(Post) ? target.user_id : nil
-    reviewable.category_id = reviewable.topic.category_id if reviewable.topic.present?
-
+    reviewable.created_new!
     reviewable.save!
     reviewable
 
diff --git a/lib/new_post_manager.rb b/lib/new_post_manager.rb
index 8d65064..714a3fe 100644
--- a/lib/new_post_manager.rb
+++ b/lib/new_post_manager.rb
@@ -206,6 +206,7 @@ class NewPostManager
     )
     reviewable.payload['title'] = @args[:title] if @args[:title].present?
     reviewable.category_id = args[:category] if args[:category].present?
+    reviewable.created_new!
 
     create_options = reviewable.create_options
 
diff --git a/spec/components/new_post_manager_spec.rb b/spec/components/new_post_manager_spec.rb
index 047785e..be0a4f2 100644
--- a/spec/components/new_post_manager_spec.rb
+++ b/spec/components/new_post_manager_spec.rb
@@ -276,7 +276,8 @@ describe NewPostManager do
         topic.user,
         raw: 'to the handler I say enqueue me!',
         title: 'this is the title of the queued post',
-        tags: ['hello', 'world']
+        tags: ['hello', 'world'],
+        category: topic.category_id
       )
 
       result = manager.perform
@@ -288,6 +289,7 @@ describe NewPostManager do
       expect(reviewable.reviewable_scores).to be_present
       expect(reviewable.score).to eq(20.5)
       expect(reviewable.reviewable_by_moderator?).to eq(true)
+      expect(reviewable.category).to be_present
       expect(reviewable.payload['tags']).to eq(['hello', 'world'])
       expect(result.action).to eq(:enqueued)
       expect(result).to be_success
@@ -301,9 +303,13 @@ describe NewPostManager do
       manager = NewPostManager.new(
         topic.user,
         raw: 'another post by this user queue me',
-        title: 'cool title in another topic'
+        topic_id: topic.id
       )
       result = manager.perform
+      reviewable = result.reviewable
+
+      expect(reviewable.topic).to be_present
+      expect(reviewable.category).to be_present
       expect(result.pending_count).to eq(1)
     end

GitHub sha: 0f01c9d2

1 Like