Skip to content

Instantly share code, notes, and snippets.

@abitdodgy
Last active December 2, 2015 17:28
Show Gist options
  • Save abitdodgy/d3f13598cacb58d4f92d to your computer and use it in GitHub Desktop.
Save abitdodgy/d3f13598cacb58d4f92d to your computer and use it in GitHub Desktop.
class UpdateDocument
def self.call_on_collection(documents, attributes)
Document.transaction do
documents.map { |document| new(document) }.each { |service| service.call_without_job(attributes) }
end.each(&:queue_job)
end
attr_reader :document
def initialize(document)
@document = document
end
def call(params)
call_without_job(params) && queue_job
end
def call_without_job(params)
document.update!(params)
end
def perform(changes)
document.create_activity_from_changes!(changes)
end
def queue_job
if document.previous_changes.any?
UpdateDocumentJob.perform_later(document, document.changes_for_activity)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment