Last active
June 27, 2017 18:47
-
-
Save ryenski/c43250e8e05a79ccb3c8869088798c9f to your computer and use it in GitHub Desktop.
Calling other service objects
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 TenantCreator | |
def initialize(user_name, email, company_name) | |
@tenant = Tenant.new(name: company_name) | |
@user = @tenant.users.new(name: user_name, email: email) | |
end | |
def call | |
if persist! | |
SignupMailer.welcome(user).deliver_now | |
AnalyticsTracker.new(tenant).call | |
SeedData.new(tenant).call | |
end | |
return result | |
end | |
private | |
attr_reader :success, :errors, :user, :tenant | |
def persist! | |
unless tenant.save | |
errors << tenant.errors.full_messages | |
return result | |
end | |
unless subscription_creator.call | |
errors << subscription_creator.errors | |
return result | |
end | |
end | |
def errors | |
(@errors ||= []) | |
end | |
def subscription_creator | |
Billing::SubscriptionCreator.new(tenant) | |
end | |
def result | |
OpenStruct.new(success?: !errors, errors: errors.try(:flatten), user: user, tenant: tenant) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment