Fixed bug: Invalid new states should be handled by ActiveRecord and don't cause Ticket#reset_pending_time to fail.

This commit is contained in:
Thorsten Eckel 2017-06-28 10:25:25 +02:00
parent b1f0bbe6e6
commit 92f227786f
2 changed files with 27 additions and 0 deletions

View file

@ -1094,6 +1094,10 @@ result
# ignore if no state has changed # ignore if no state has changed
return true if !changes['state_id'] return true if !changes['state_id']
# ignore if new state is blank and
# let handle ActiveRecord the error
return if state_id.blank?
# check if new state isn't pending* # check if new state isn't pending*
current_state = Ticket::State.lookup(id: state_id) current_state = Ticket::State.lookup(id: state_id)
current_state_type = Ticket::StateType.lookup(id: current_state.state_type_id) current_state_type = Ticket::StateType.lookup(id: current_state.state_type_id)

View file

@ -203,4 +203,27 @@ RSpec.describe Ticket do
end end
context 'callbacks' do
describe '#reset_pending_time' do
it 'resets the pending time on state change' do
ticket = create(:ticket,
state: Ticket::State.lookup(name: 'pending reminder'),
pending_time: Time.zone.now + 2.days)
expect(ticket.pending_time).not_to be nil
ticket.update_attribute(:state, Ticket::State.lookup(name: 'open'))
expect(ticket.pending_time).to be nil
end
it 'lets handle ActiveRecord nil as new value' do
ticket = create(:ticket)
expect do
ticket.update_attribute(:state, nil)
end.to raise_error(ActiveRecord::StatementInvalid)
end
end
end
end end