From 92f227786f298bad1ccaf92d4478a7062ea6a49f Mon Sep 17 00:00:00 2001 From: Thorsten Eckel Date: Wed, 28 Jun 2017 10:25:25 +0200 Subject: [PATCH] Fixed bug: Invalid new states should be handled by ActiveRecord and don't cause Ticket#reset_pending_time to fail. --- app/models/ticket.rb | 4 ++++ spec/models/ticket_spec.rb | 23 +++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/app/models/ticket.rb b/app/models/ticket.rb index 292894b29..4d874528e 100644 --- a/app/models/ticket.rb +++ b/app/models/ticket.rb @@ -1094,6 +1094,10 @@ result # ignore if no state has changed 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* current_state = Ticket::State.lookup(id: state_id) current_state_type = Ticket::StateType.lookup(id: current_state.state_type_id) diff --git a/spec/models/ticket_spec.rb b/spec/models/ticket_spec.rb index dd5b9edcc..f90bf2029 100644 --- a/spec/models/ticket_spec.rb +++ b/spec/models/ticket_spec.rb @@ -203,4 +203,27 @@ RSpec.describe Ticket do 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