# Recreate Coalition Object Key on Update
class UpdateCoalitionJob
	@queue = :feed

	# Public: Recreate Coaltition Object Key on Update
	#
	# This job re-creates a coalition object key when a coalition
	# model instance is updated so that the feed stays in
	# sync with the database.
	#
	# id - The id of the coalition
	#
	# Returns nothing. It's a worker task
	def self.perform(id)
		coalition = Coalition.find(id)
		obj = self.new(coalition)
		obj.recreate_object_key
	end

	def initialize(coalition)
		@coalition = coalition
	end

	# On model update, destroy the current object and recreate it
	def recreate_object_key
		$feed.unrecord("coalition:#{@coalition.id}")
		data = { id: @coalition.permalink, name: @coalition.display_name }
		data[:photo] = @coalition.profile_photo_url(:thumb) if @coalition.profile_photo
		$feed.record("coalition:#{@coalition.id}", data)
	end
end