Sha256: dc0d2c49418d4b97a92d81517163b7d4a28537dfaff12f7fc18ef24e23c99929

Contents?: true

Size: 1.5 KB

Versions: 2

Compression:

Stored size: 1.5 KB

Contents

module ChangeManager
	class Manager
		def self.queue_change(owner, change_type, context, target)
			change_id = Change.new_change(owner, change_type, context, target)
			# Resque.enqueue(MakeChange, change_id)
			Resque.enqueue_in(30.seconds, MakeChange, change_id)
		end

		def self.notify(change_id)
			puts 'notify method called'
			unless Change.find(change_id).cancelled?
				change = Change.find(change_id)
				verified_changes = process_changes(change)
				notify_users(verified_changes) unless verified_changes.empty?
			end
		end

		private
		def self.process_changes(change)
			cancel_all_changes(group_similar_changes(change.owner, change.target))
		end

		def self.group_similar_changes(owner, target)
			similar_changes = Change.where(owner: owner, target: target, cancelled: false)
			if similar_changes.length > 1
				cancel_inverse_changes(similar_changes)
			end
			return similar_changes
		end

		def self.cancel_inverse_changes(similar_changes)
			similar_changes.each do |change|
				similar_changes.each do |next_change|
					if change.inverse_of?(next_change)
						change.cancel
						next_change.cancel
						similar_changes.delete_if { |change| change.cancelled? }
					end
				end
			end
			return similar_changes
		end

		def self.cancel_all_changes(verified_changes)
			verified_changes.each do |change|
				change.cancel
			end
			verified_changes
		end

		def self.notify_users(changes)
			mailer = ChangeManager::NotificationMailer
			mailer.send_email(mailer.construct_email(changes))
		end

	end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
change_manager-0.0.7 app/services/change_manager/manager.rb
change_manager-0.0.5 app/services/change_manager/manager.rb