Skip to content

Instantly share code, notes, and snippets.

@ryenski
Last active June 27, 2017 18:47
Show Gist options
  • Save ryenski/c43250e8e05a79ccb3c8869088798c9f to your computer and use it in GitHub Desktop.
Save ryenski/c43250e8e05a79ccb3c8869088798c9f to your computer and use it in GitHub Desktop.
Calling other service objects
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