diff --git a/app/models/topic.rb b/app/models/topic.rb index 12198addaaf..29d47aa8515 100644 --- a/app/models/topic.rb +++ b/app/models/topic.rb @@ -969,7 +969,7 @@ SQL topic_status_update.based_on_last_post = !based_on_last_post.blank? if status_type == TopicStatusUpdate.types[:publish_to_category] - topic_status_update.category_id = category_id + topic_status_update.category = Category.find_by(category_id) end if topic_status_update.based_on_last_post diff --git a/app/models/topic_status_update.rb b/app/models/topic_status_update.rb index 1e87540bde9..3a10d19c9ce 100644 --- a/app/models/topic_status_update.rb +++ b/app/models/topic_status_update.rb @@ -9,6 +9,8 @@ class TopicStatusUpdate < ActiveRecord::Base validates :execute_at, presence: true validates :status_type, presence: true validates :status_type, uniqueness: { scope: [:topic_id, :deleted_at] } + validates :category_id, presence: true, if: :publishing_to_category? + validate :ensure_update_will_happen before_save do @@ -93,6 +95,10 @@ class TopicStatusUpdate < ActiveRecord::Base def schedule_auto_publish_to_category_job(time) Jobs.enqueue_at(time, :publish_topic_to_category, topic_status_update_id: id) end + + def publishing_to_category? + self.status_type.to_i == TopicStatusUpdate.types[:publish_to_category] + end end # == Schema Information diff --git a/spec/models/topic_status_update_spec.rb b/spec/models/topic_status_update_spec.rb index ebf368a472d..6880ddc2ec9 100644 --- a/spec/models/topic_status_update_spec.rb +++ b/spec/models/topic_status_update_spec.rb @@ -45,6 +45,34 @@ RSpec.describe TopicStatusUpdate, type: :model do end end end + + describe '#category_id' do + describe 'when #status_type is publish_to_category' do + describe 'when #category_id is not present' do + it 'should not be valid' do + topic_status_update = Fabricate.build(:topic_status_update, + status_type: TopicStatusUpdate.types[:publish_to_category] + ) + + expect(topic_status_update).to_not be_valid + expect(topic_status_update.errors.keys).to include(:category_id) + end + end + + describe 'when #category_id is present' do + it 'should be valid' do + topic_status_update = Fabricate.build(:topic_status_update, + status_type: TopicStatusUpdate.types[:publish_to_category], + category_id: Fabricate(:category).id, + user: Fabricate(:user), + topic: Fabricate(:topic) + ) + + expect(topic_status_update).to be_valid + end + end + end + end end context 'callbacks' do