Skip to content

Instantly share code, notes, and snippets.

@apneadiving
Last active November 28, 2016 21:55
Show Gist options
  • Save apneadiving/f6908137ccce1a549ebe943071beafac to your computer and use it in GitHub Desktop.
Save apneadiving/f6908137ccce1a549ebe943071beafac to your computer and use it in GitHub Desktop.
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