Sha256: c7c27a596fac2086a3b0ed0cb6719139943b1fbed535066bbd859eda5a9728a2

Contents?: true

Size: 1.63 KB

Versions: 3

Compression:

Stored size: 1.63 KB

Contents

module ChangeManager
	module Manager
		def queue_change(owner, change_type, context, target)
			change_id = Change.new_change(owner, change_type, context, target)
			Resque.enqueue(ChangeManager::BeginChange, change_id)
			# Resque.enqueue_in(5.minutes, ChangeManager::BeginChange, change_id)
		end

		def process_change(change_id)
			unless Change.find(change_id).cancelled?
				change = Change.find change_id
				verified_changes = process_changes change
				notify verified_changes unless verified_changes.empty?
			end
		end

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

		def 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 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
						puts 'cancelled inverse changes ' + change.change_type + ' and ' + next_change.change_type
						similar_changes.delete_if { |change| change.cancelled? }
					end
				end
			end
			return similar_changes
		end

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

		def notify(changes)
			mailer = ChangeManager::NotificationMailer
			if mailer.send_email(mailer.construct_email(changes))
				changes.each do |change|
					change.notify
				end
				return true
			end
			false
		end
	end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
change_manager-0.3.1 app/services/change_manager/manager.rb
change_manager-0.3.0 app/services/change_manager/manager.rb
change_manager-0.2.0 app/services/change_manager/manager.rb