Last active
August 29, 2015 14:07
-
-
Save apneadiving/b1e9d30f08411e24eae6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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