Improve validation for `TopicStatusUpdate`.
This commit is contained in:
parent
cd59db5aa3
commit
71501feaf3
|
@ -969,7 +969,7 @@ SQL
|
||||||
topic_status_update.based_on_last_post = !based_on_last_post.blank?
|
topic_status_update.based_on_last_post = !based_on_last_post.blank?
|
||||||
|
|
||||||
if status_type == TopicStatusUpdate.types[:publish_to_category]
|
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
|
end
|
||||||
|
|
||||||
if topic_status_update.based_on_last_post
|
if topic_status_update.based_on_last_post
|
||||||
|
|
|
@ -9,6 +9,8 @@ class TopicStatusUpdate < ActiveRecord::Base
|
||||||
validates :execute_at, presence: true
|
validates :execute_at, presence: true
|
||||||
validates :status_type, presence: true
|
validates :status_type, presence: true
|
||||||
validates :status_type, uniqueness: { scope: [:topic_id, :deleted_at] }
|
validates :status_type, uniqueness: { scope: [:topic_id, :deleted_at] }
|
||||||
|
validates :category_id, presence: true, if: :publishing_to_category?
|
||||||
|
|
||||||
validate :ensure_update_will_happen
|
validate :ensure_update_will_happen
|
||||||
|
|
||||||
before_save do
|
before_save do
|
||||||
|
@ -93,6 +95,10 @@ class TopicStatusUpdate < ActiveRecord::Base
|
||||||
def schedule_auto_publish_to_category_job(time)
|
def schedule_auto_publish_to_category_job(time)
|
||||||
Jobs.enqueue_at(time, :publish_topic_to_category, topic_status_update_id: id)
|
Jobs.enqueue_at(time, :publish_topic_to_category, topic_status_update_id: id)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def publishing_to_category?
|
||||||
|
self.status_type.to_i == TopicStatusUpdate.types[:publish_to_category]
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# == Schema Information
|
# == Schema Information
|
||||||
|
|
|
@ -45,6 +45,34 @@ RSpec.describe TopicStatusUpdate, type: :model do
|
||||||
end
|
end
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
context 'callbacks' do
|
context 'callbacks' do
|
||||||
|
|
Loading…
Reference in New Issue