Skip to content

Instantly share code, notes, and snippets.

@apneadiving
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save apneadiving/b1e9d30f08411e24eae6 to your computer and use it in GitHub Desktop.
Save apneadiving/b1e9d30f08411e24eae6 to your computer and use it in GitHub Desktop.
Wf.new
.chain { CloseProject.new(project) }
.chain { NotifyProjectClosed(project) }
.chain { Log.new(:project_closed, project) }
.chain { render json: project }
.on_dam { |error_pool| render json: {errors: error_pool}, status: 422 }
class CloseProject
include Waterfall
attr_reader :project
def initialize(project)
@project = project
end
def call
self
.chain { CreditTransaction.new({ payable: project.owner, amount: project.cost, source: project }) }
.chain { close }
.chain { project }
end
def close
dam project.errors unless project.close
end
end
class CreditTransaction
include Waterfall
attr_reader :payable, :amount, :source
def initialize(options)
source = options.fetch(:source)
payable = options.fetch(:payable)
amount = options.fetch(:amount)
end
def call
self
.when_falsy { credit.save }
.dam { credit.errors }
.chain { credit }
end
def credit
@credit ||= payable.credit_transactions.create(amount: amount, source: source})
end
end
class NotifyProjectClosed
include Waterfall
attr_reader :project
def initialize(project)
@project = project
end
def call
self
.chain { notify_owner }
.chain { notify_manager }
end
def notify_owner
ProjectMailer.delay.notify_owner_on_close(project)
end
def notify_manager
ProjectMailer.delay.notify_manager_on_close(project)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment