Last active
November 28, 2016 21:55
-
-
Save apneadiving/f6908137ccce1a549ebe943071beafac to your computer and use it in GitHub Desktop.
This file contains 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 AcceptInvitation | |
include Waterfall | |
include ActiveModel::Validations | |
CENTS_PAID_FOR_AFFILIATION = 100 | |
validate :ensure_invitation_not_accepted | |
def initialize(invitation) | |
@invitation = invitation | |
end | |
def call | |
when_falsy { valid? } | |
.dam { errors } | |
chain(user: :user) { CreateUserFromInvitation.new(invitation).call } | |
chain do | |
CreditUser.new( | |
user: invitation.inviter, | |
cents: CENTS_PAID_FOR_AFFILIATION | |
).call | |
end | |
chain { invitation.accept } | |
when_falsy { invitation.save } | |
.dam { invitation.errors } | |
end | |
private | |
def ensure_invitation_not_accepted | |
if invitation.accepted? | |
errors.add :invitation_status, 'Invitation already accepted' | |
end | |
end | |
attr_reader :invitation | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment