From 6d02578d6117a4cb6a9a0cd6ec338bcd98cccf5c Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Thu, 14 Apr 2016 09:17:13 +0200 Subject: [PATCH] Moved to global Observer::Transaction.commit which executes model based transitions. --- app/controllers/application_controller.rb | 2 +- app/models/channel/email_parser.rb | 4 +- app/models/job.rb | 8 +- app/models/observer/ticket/notification.rb | 4 +- app/models/observer/transaction.rb | 10 ++ app/models/ticket.rb | 10 +- lib/facebook.rb | 4 +- lib/tweet_base.rb | 4 +- test/unit/history_test.rb | 4 +- test/unit/online_notifiaction_test.rb | 44 ++++---- test/unit/ticket_notification_test.rb | 112 ++++++++++----------- 11 files changed, 109 insertions(+), 97 deletions(-) create mode 100644 app/models/observer/transaction.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 078c7fa7d..51c705347 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -51,7 +51,7 @@ class ApplicationController < ActionController::Base # execute events def trigger_events - Observer::Ticket::Notification.transaction + Observer::Transaction.commit end # Finds the User with the ID stored in the session with the key diff --git a/app/models/channel/email_parser.rb b/app/models/channel/email_parser.rb index 6ee7d83ba..797b06cb7 100644 --- a/app/models/channel/email_parser.rb +++ b/app/models/channel/email_parser.rb @@ -486,8 +486,8 @@ retrns end end - # execute ticket notification events - Observer::Ticket::Notification.transaction + # execute object transaction + Observer::Transaction.commit # run postmaster post filter Setting.where(area: 'Postmaster::PostFilter').order(:name).each {|setting| diff --git a/app/models/job.rb b/app/models/job.rb index 70bb39b7b..9c9bf9fd6 100644 --- a/app/models/job.rb +++ b/app/models/job.rb @@ -60,10 +60,10 @@ class Job < ApplicationModel next if !changed ticket.save - # execute ticket notification events - if !job.disable_notification - Observer::Ticket::Notification.transaction - end + # execute ticket transaction + Observer::Ticket::Transaction.commit( + disable_notification: job.disable_notification + ) end end end diff --git a/app/models/observer/ticket/notification.rb b/app/models/observer/ticket/notification.rb index dab86a86e..35ce83c91 100644 --- a/app/models/observer/ticket/notification.rb +++ b/app/models/observer/ticket/notification.rb @@ -6,7 +6,9 @@ require 'notification_factory' class Observer::Ticket::Notification < ActiveRecord::Observer observe :ticket, 'ticket::_article' - def self.transaction + def self.transaction(params) + + return if params[:disable_notification] # return if we run import mode return if Setting.get('import_mode') diff --git a/app/models/observer/transaction.rb b/app/models/observer/transaction.rb new file mode 100644 index 000000000..c3a813c68 --- /dev/null +++ b/app/models/observer/transaction.rb @@ -0,0 +1,10 @@ +class Observer::Transaction + + def self.commit(params = {}) + + # execute ticket transactions + Observer::Ticket::Notification.transaction(params) + + end + +end diff --git a/app/models/ticket.rb b/app/models/ticket.rb index b89d8b317..b51fa5c5d 100644 --- a/app/models/ticket.rb +++ b/app/models/ticket.rb @@ -157,8 +157,8 @@ returns ticket.save! # we do not have an destructor at this point, so we need to - # execute ticket events manually - Observer::Ticket::Notification.transaction + # execute object transaction manually + Observer::Transaction.commit result.push ticket } @@ -473,7 +473,7 @@ condition example end # validate pre_condition values - return nil if selector['pre_condition'] && selector['pre_condition'] !~ /^(set|current_user\.|specific)/ + return nil if selector['pre_condition'] && selector['pre_condition'] !~ /^(not_set|current_user\.|specific)/ # get attributes attributes = attribute.split(/\./) @@ -484,7 +484,7 @@ condition example end if selector['operator'] == 'is' - if selector['pre_condition'] == 'set' + if selector['pre_condition'] == 'not_set' if attributes[1] =~ /^(created_by|updated_by|owner|customer|user)_id/ query += "#{attribute} NOT IN (?)" bind_params.push 1 @@ -511,7 +511,7 @@ condition example # rubocop:enable Style/IfInsideElse end elsif selector['operator'] == 'is not' - if selector['pre_condition'] == 'set' + if selector['pre_condition'] == 'not_set' if attributes[1] =~ /^(created_by|updated_by|owner|customer|user)_id/ query += "#{attribute} IN (?)" bind_params.push 1 diff --git a/lib/facebook.rb b/lib/facebook.rb index 5df6bf5fc..f334d0e00 100644 --- a/lib/facebook.rb +++ b/lib/facebook.rb @@ -296,8 +296,8 @@ result end to_article(post, ticket, page) - # execute ticket events - Observer::Ticket::Notification.transaction + # execute object transaction + Observer::Transaction.commit end ticket diff --git a/lib/tweet_base.rb b/lib/tweet_base.rb index b606fee4e..86e739707 100644 --- a/lib/tweet_base.rb +++ b/lib/tweet_base.rb @@ -235,8 +235,8 @@ class TweetBase raise "Unknown tweet type '#{tweet.class}'" end - # execute ticket events - Observer::Ticket::Notification.transaction + # execute object transaction + Observer::Transaction.commit end if @connection_type == 'stream' diff --git a/test/unit/history_test.rb b/test/unit/history_test.rb index b8b9947b5..29e147506 100644 --- a/test/unit/history_test.rb +++ b/test/unit/history_test.rb @@ -171,8 +171,8 @@ class HistoryTest < ActiveSupport::TestCase end end - # execute ticket events - Observer::Ticket::Notification.transaction + # execute object transaction + Observer::Transaction.commit # execute background jobs Delayed::Worker.new.work_off diff --git a/test/unit/online_notifiaction_test.rb b/test/unit/online_notifiaction_test.rb index 0e345ac4f..c1ad1fa05 100644 --- a/test/unit/online_notifiaction_test.rb +++ b/test/unit/online_notifiaction_test.rb @@ -60,8 +60,8 @@ class OnlineNotificationTest < ActiveSupport::TestCase tickets = [] tickets.push ticket1 - # execute ticket transaction - Observer::Ticket::Notification.transaction + # execute object transaction + Observer::Transaction.commit #puts Delayed::Job.all.inspect Delayed::Worker.new.work_off @@ -79,8 +79,8 @@ class OnlineNotificationTest < ActiveSupport::TestCase updated_by_id: customer_user.id, ) - # execute ticket transaction - Observer::Ticket::Notification.transaction + # execute object transaction + Observer::Transaction.commit #puts Delayed::Job.all.inspect Delayed::Worker.new.work_off @@ -117,8 +117,8 @@ class OnlineNotificationTest < ActiveSupport::TestCase tickets = [] tickets.push ticket2 - # execute ticket transaction - Observer::Ticket::Notification.transaction + # execute object transaction + Observer::Transaction.commit #puts Delayed::Job.all.inspect Delayed::Worker.new.work_off @@ -136,8 +136,8 @@ class OnlineNotificationTest < ActiveSupport::TestCase updated_by_id: customer_user.id, ) - # execute ticket transaction - Observer::Ticket::Notification.transaction + # execute object transaction + Observer::Transaction.commit #puts Delayed::Job.all.inspect Delayed::Worker.new.work_off @@ -173,8 +173,8 @@ class OnlineNotificationTest < ActiveSupport::TestCase # remember ticket tickets.push ticket3 - # execute ticket transaction - Observer::Ticket::Notification.transaction + # execute object transaction + Observer::Transaction.commit #puts Delayed::Job.all.inspect Delayed::Worker.new.work_off @@ -192,8 +192,8 @@ class OnlineNotificationTest < ActiveSupport::TestCase updated_by_id: customer_user.id, ) - # execute ticket transaction - Observer::Ticket::Notification.transaction + # execute object transaction + Observer::Transaction.commit #puts Delayed::Job.all.inspect Delayed::Worker.new.work_off @@ -217,8 +217,8 @@ class OnlineNotificationTest < ActiveSupport::TestCase internal: false ) - # execute ticket transaction - Observer::Ticket::Notification.transaction + # execute object transaction + Observer::Transaction.commit #puts Delayed::Job.all.inspect Delayed::Worker.new.work_off @@ -256,8 +256,8 @@ class OnlineNotificationTest < ActiveSupport::TestCase # remember ticket tickets.push ticket4 - # execute ticket transaction - Observer::Ticket::Notification.transaction + # execute object transaction + Observer::Transaction.commit #puts Delayed::Job.all.inspect Delayed::Worker.new.work_off @@ -275,8 +275,8 @@ class OnlineNotificationTest < ActiveSupport::TestCase updated_by_id: customer_user.id, ) - # execute ticket transaction - Observer::Ticket::Notification.transaction + # execute object transaction + Observer::Transaction.commit #puts Delayed::Job.all.inspect Delayed::Worker.new.work_off @@ -312,8 +312,8 @@ class OnlineNotificationTest < ActiveSupport::TestCase # remember ticket tickets.push ticket5 - # execute ticket transaction - Observer::Ticket::Notification.transaction + # execute object transaction + Observer::Transaction.commit #puts Delayed::Job.all.inspect Delayed::Worker.new.work_off @@ -331,8 +331,8 @@ class OnlineNotificationTest < ActiveSupport::TestCase updated_by_id: customer_user.id, ) - # execute ticket transaction - Observer::Ticket::Notification.transaction + # execute object transaction + Observer::Transaction.commit #puts Delayed::Job.all.inspect Delayed::Worker.new.work_off diff --git a/test/unit/ticket_notification_test.rb b/test/unit/ticket_notification_test.rb index 3e017a54f..b9cca9088 100644 --- a/test/unit/ticket_notification_test.rb +++ b/test/unit/ticket_notification_test.rb @@ -85,9 +85,9 @@ class TicketNotificationTest < ActiveSupport::TestCase ) assert(ticket1) - # execute ticket transaction + # execute object transaction Rails.configuration.webserver_is_active = nil - Observer::Ticket::Notification.transaction + Observer::Transaction.commit #puts Delayed::Job.all.inspect Delayed::Worker.new.work_off @@ -120,9 +120,9 @@ class TicketNotificationTest < ActiveSupport::TestCase ) assert(ticket1) - # execute ticket transaction + # execute object transaction Rails.configuration.webserver_is_active = true - Observer::Ticket::Notification.transaction + Observer::Transaction.commit #puts Delayed::Job.all.inspect Delayed::Worker.new.work_off @@ -158,9 +158,9 @@ class TicketNotificationTest < ActiveSupport::TestCase ) assert( ticket1, 'ticket created - ticket notification simple' ) - # execute ticket transaction + # execute object transaction Rails.configuration.webserver_is_active = true - Observer::Ticket::Notification.transaction + Observer::Transaction.commit #puts Delayed::Job.all.inspect Delayed::Worker.new.work_off @@ -173,8 +173,8 @@ class TicketNotificationTest < ActiveSupport::TestCase ticket1.priority = Ticket::Priority.lookup(name: '3 high') ticket1.save - # execute ticket transaction - Observer::Ticket::Notification.transaction + # execute object transaction + Observer::Transaction.commit #puts Delayed::Job.all.inspect Delayed::Worker.new.work_off @@ -195,8 +195,8 @@ class TicketNotificationTest < ActiveSupport::TestCase created_by_id: agent1.id, ) - # execute ticket transaction - Observer::Ticket::Notification.transaction + # execute object transaction + Observer::Transaction.commit #puts Delayed::Job.all.inspect Delayed::Worker.new.work_off @@ -220,8 +220,8 @@ class TicketNotificationTest < ActiveSupport::TestCase created_by_id: agent1.id, ) - # execute ticket transaction - Observer::Ticket::Notification.transaction + # execute object transaction + Observer::Transaction.commit #puts Delayed::Job.all.inspect Delayed::Worker.new.work_off @@ -254,8 +254,8 @@ class TicketNotificationTest < ActiveSupport::TestCase created_by_id: agent1.id, ) - # execute ticket transaction - Observer::Ticket::Notification.transaction + # execute object transaction + Observer::Transaction.commit #puts Delayed::Job.all.inspect Delayed::Worker.new.work_off assert(ticket2, 'ticket created') @@ -270,8 +270,8 @@ class TicketNotificationTest < ActiveSupport::TestCase ticket2.priority = Ticket::Priority.lookup(name: '3 high') ticket2.save - # execute ticket transaction - Observer::Ticket::Notification.transaction + # execute object transaction + Observer::Transaction.commit #puts Delayed::Job.all.inspect Delayed::Worker.new.work_off @@ -285,8 +285,8 @@ class TicketNotificationTest < ActiveSupport::TestCase ticket2.priority = Ticket::Priority.lookup(name: '2 normal') ticket2.save - # execute ticket transaction - Observer::Ticket::Notification.transaction + # execute object transaction + Observer::Transaction.commit #puts Delayed::Job.all.inspect Delayed::Worker.new.work_off @@ -319,8 +319,8 @@ class TicketNotificationTest < ActiveSupport::TestCase created_by_id: agent2.id, ) - # execute ticket transaction - Observer::Ticket::Notification.transaction + # execute object transaction + Observer::Transaction.commit #puts Delayed::Job.all.inspect Delayed::Worker.new.work_off assert(ticket3, 'ticket created') @@ -335,8 +335,8 @@ class TicketNotificationTest < ActiveSupport::TestCase ticket3.priority = Ticket::Priority.lookup(name: '3 high') ticket3.save - # execute ticket transaction - Observer::Ticket::Notification.transaction + # execute object transaction + Observer::Transaction.commit #puts Delayed::Job.all.inspect Delayed::Worker.new.work_off @@ -350,8 +350,8 @@ class TicketNotificationTest < ActiveSupport::TestCase ticket3.priority = Ticket::Priority.lookup(name: '2 normal') ticket3.save - # execute ticket transaction - Observer::Ticket::Notification.transaction + # execute object transaction + Observer::Transaction.commit #puts Delayed::Job.all.inspect Delayed::Worker.new.work_off @@ -363,8 +363,8 @@ class TicketNotificationTest < ActiveSupport::TestCase article_inbound.internal = true article_inbound.save - # execute ticket transaction - Observer::Ticket::Notification.transaction + # execute object transaction + Observer::Transaction.commit #puts Delayed::Job.all.inspect Delayed::Worker.new.work_off @@ -425,9 +425,9 @@ class TicketNotificationTest < ActiveSupport::TestCase created_by_id: customer.id, ) - # execute ticket transaction + # execute object transaction Rails.configuration.webserver_is_active = false - Observer::Ticket::Notification.transaction + Observer::Transaction.commit #puts Delayed::Job.all.inspect Delayed::Worker.new.work_off @@ -440,8 +440,8 @@ class TicketNotificationTest < ActiveSupport::TestCase ticket1.priority = Ticket::Priority.lookup(name: '3 high') ticket1.save - # execute ticket transaction - Observer::Ticket::Notification.transaction + # execute object transaction + Observer::Transaction.commit #puts Delayed::Job.all.inspect Delayed::Worker.new.work_off @@ -474,8 +474,8 @@ class TicketNotificationTest < ActiveSupport::TestCase created_by_id: customer.id, ) - # execute ticket transaction - Observer::Ticket::Notification.transaction + # execute object transaction + Observer::Transaction.commit #puts Delayed::Job.all.inspect Delayed::Worker.new.work_off @@ -488,8 +488,8 @@ class TicketNotificationTest < ActiveSupport::TestCase ticket2.priority = Ticket::Priority.lookup(name: '3 high') ticket2.save - # execute ticket transaction - Observer::Ticket::Notification.transaction + # execute object transaction + Observer::Transaction.commit #puts Delayed::Job.all.inspect Delayed::Worker.new.work_off @@ -522,8 +522,8 @@ class TicketNotificationTest < ActiveSupport::TestCase created_by_id: customer.id, ) - # execute ticket transaction - Observer::Ticket::Notification.transaction + # execute object transaction + Observer::Transaction.commit #puts Delayed::Job.all.inspect Delayed::Worker.new.work_off @@ -536,8 +536,8 @@ class TicketNotificationTest < ActiveSupport::TestCase ticket3.priority = Ticket::Priority.lookup(name: '3 high') ticket3.save - # execute ticket transaction - Observer::Ticket::Notification.transaction + # execute object transaction + Observer::Transaction.commit #puts Delayed::Job.all.inspect Delayed::Worker.new.work_off @@ -587,9 +587,9 @@ class TicketNotificationTest < ActiveSupport::TestCase created_by_id: customer.id, ) - # execute ticket transaction + # execute object transaction Rails.configuration.webserver_is_active = false - Observer::Ticket::Notification.transaction + Observer::Transaction.commit #puts Delayed::Job.all.inspect Delayed::Worker.new.work_off @@ -602,8 +602,8 @@ class TicketNotificationTest < ActiveSupport::TestCase ticket4.priority = Ticket::Priority.lookup(name: '3 high') ticket4.save - # execute ticket transaction - Observer::Ticket::Notification.transaction + # execute object transaction + Observer::Transaction.commit #puts Delayed::Job.all.inspect Delayed::Worker.new.work_off @@ -653,9 +653,9 @@ class TicketNotificationTest < ActiveSupport::TestCase created_by_id: customer.id, ) - # execute ticket transaction + # execute object transaction Rails.configuration.webserver_is_active = false - Observer::Ticket::Notification.transaction + Observer::Transaction.commit #puts Delayed::Job.all.inspect Delayed::Worker.new.work_off @@ -668,8 +668,8 @@ class TicketNotificationTest < ActiveSupport::TestCase ticket5.priority = Ticket::Priority.lookup(name: '3 high') ticket5.save - # execute ticket transaction - Observer::Ticket::Notification.transaction + # execute object transaction + Observer::Transaction.commit #puts Delayed::Job.all.inspect Delayed::Worker.new.work_off @@ -720,9 +720,9 @@ class TicketNotificationTest < ActiveSupport::TestCase created_by_id: customer.id, ) - # execute ticket transaction + # execute object transaction Rails.configuration.webserver_is_active = false - Observer::Ticket::Notification.transaction + Observer::Transaction.commit #puts Delayed::Job.all.inspect Delayed::Worker.new.work_off @@ -737,8 +737,8 @@ class TicketNotificationTest < ActiveSupport::TestCase ticket6.priority = Ticket::Priority.lookup(name: '3 high') ticket6.save - # execute ticket transaction - Observer::Ticket::Notification.transaction + # execute object transaction + Observer::Transaction.commit #puts Delayed::Job.all.inspect Delayed::Worker.new.work_off @@ -799,9 +799,9 @@ class TicketNotificationTest < ActiveSupport::TestCase created_by_id: customer.id, ) - # execute ticket transaction + # execute object transaction Rails.configuration.webserver_is_active = false - Observer::Ticket::Notification.transaction + Observer::Transaction.commit #puts Delayed::Job.all.inspect Delayed::Worker.new.work_off @@ -816,8 +816,8 @@ class TicketNotificationTest < ActiveSupport::TestCase ticket7.priority = Ticket::Priority.lookup(name: '3 high') ticket7.save - # execute ticket transaction - Observer::Ticket::Notification.transaction + # execute object transaction + Observer::Transaction.commit #puts Delayed::Job.all.inspect Delayed::Worker.new.work_off @@ -856,8 +856,8 @@ class TicketNotificationTest < ActiveSupport::TestCase ) assert(ticket1, 'ticket created') - # execute ticket transaction - Observer::Ticket::Notification.transaction + # execute object transaction + Observer::Transaction.commit # update ticket attributes ticket1.title = "#{ticket1.title} - #2"