FIX: Callback undefined error when topic has been deleted.

This commit is contained in:
Guo Xiang Tan 2017-04-03 09:06:15 +08:00
parent 549aa9204c
commit 5cf75c67df
2 changed files with 68 additions and 36 deletions

View File

@ -70,21 +70,25 @@ class TopicStatusUpdate < ActiveRecord::Base
alias_method :cancel_auto_open_job, :cancel_auto_close_job
def schedule_auto_open_job(time)
topic.update_status('closed', true, user) if !topic.closed
if topic
topic.update_status('closed', true, user) if !topic.closed
Jobs.enqueue_at(time, :toggle_topic_closed,
topic_status_update_id: id,
state: false
)
Jobs.enqueue_at(time, :toggle_topic_closed,
topic_status_update_id: id,
state: false
)
end
end
def schedule_auto_close_job(time)
topic.update_status('closed', false, user) if topic.closed
if topic
topic.update_status('closed', false, user) if topic.closed
Jobs.enqueue_at(time, :toggle_topic_closed,
topic_status_update_id: id,
state: true
)
Jobs.enqueue_at(time, :toggle_topic_closed,
topic_status_update_id: id,
state: true
)
end
end
end

View File

@ -4,6 +4,10 @@ RSpec.describe TopicStatusUpdate, type: :model do
let(:topic_status_update) { Fabricate(:topic_status_update) }
let(:topic) { Fabricate(:topic) }
before do
Jobs::ToggleTopicClosed.jobs.clear
end
context "validations" do
describe '#status_type' do
it 'should ensure that only one active topic status update exists' do
@ -122,6 +126,56 @@ RSpec.describe TopicStatusUpdate, type: :model do
end
end
end
describe 'when a open topic status update is created for an open topic' do
let(:topic) { Fabricate(:topic, closed: false) }
let(:topic_status_update) do
Fabricate(:topic_status_update,
status_type: described_class.types[:open],
topic: topic
)
end
it 'should close the topic' do
topic_status_update
expect(topic.reload.closed).to eq(true)
end
describe 'when topic has been deleted' do
it 'should not queue the job' do
topic.trash!
topic_status_update
expect(Jobs::ToggleTopicClosed.jobs).to eq([])
end
end
end
describe 'when a close topic status update is created for a closed topic' do
let(:topic) { Fabricate(:topic, closed: true) }
let(:topic_status_update) do
Fabricate(:topic_status_update,
status_type: described_class.types[:close],
topic: topic
)
end
it 'should open the topic' do
topic_status_update
expect(topic.reload.closed).to eq(false)
end
describe 'when topic has been deleted' do
it 'should not queue the job' do
topic.trash!
topic_status_update
expect(Jobs::ToggleTopicClosed.jobs).to eq([])
end
end
end
end
describe '.ensure_consistency!' do
@ -158,30 +212,4 @@ RSpec.describe TopicStatusUpdate, type: :model do
expect(job_args["state"]).to eq(false)
end
end
describe 'when a open topic status update is created for an open topic' do
it 'should close the topic' do
topic = Fabricate(:topic, closed: false)
Fabricate(:topic_status_update,
status_type: described_class.types[:open],
topic: topic
)
expect(topic.reload.closed).to eq(true)
end
end
describe 'when a close topic status update is created for a closed topic' do
it 'should open the topic' do
topic = Fabricate(:topic, closed: true)
Fabricate(:topic_status_update,
status_type: described_class.types[:close],
topic: topic
)
expect(topic.reload.closed).to eq(false)
end
end
end