Last active
August 29, 2015 13:56
-
-
Save RichardJordan/9060483 to your computer and use it in GitHub Desktop.
Registration of user and account: the Registrar model, shepherding the registration process
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
# see https://gist.github.com/RichardJordan/9059633 for explanation | |
# ----------------------------------------------------------------- | |
require_relative '../../lib/abilities/roles_admin' | |
class Registrar < Struct.new(:listener) | |
attr_writer :admin_user_source, :registration_source, :responsibilities | |
delegate :account, :fullname, :shortname, :email, :password, | |
:password_confirmation, :user, to: :registration | |
def register(registration) | |
@registration = registration | |
if registration.valid? | |
complete_registration | |
else | |
listener.create_registration_failed(registration) | |
end | |
end | |
private | |
def admin_user_source | |
@admin_user_source ||= CustomerSupportUser.public_method(:new) | |
end | |
def build_account; end | |
def build_user; end | |
def complete_registration | |
build_account | |
build_user | |
persist! | |
listener.create_registration_succeeded(registration) | |
end | |
def grant_role | |
roles_admin.grant_role!("owner", on_account: account, to_user: user) | |
end | |
def persist! | |
grant_role if registration.save! | |
end | |
def registration | |
@registration ||= registration_source.call | |
end | |
def registration_source | |
@registration_source ||= Registration.public_method(:new) | |
end | |
def responsibilities | |
@responsibilities ||= RolesAdmin | |
end | |
def roles_admin | |
@roles_admin ||= admin_user_source.call.extend(responsibilities) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment