Skip to content

Instantly share code, notes, and snippets.

@gogogarrett
Last active December 16, 2015 16:48
Show Gist options
  • Select an option

  • Save gogogarrett/5465349 to your computer and use it in GitHub Desktop.

Select an option

Save gogogarrett/5465349 to your computer and use it in GitHub Desktop.
class Forms::StudentWithProfileForm
extend ActiveModel::Naming
include ActiveModel::Conversion
include ActiveModel::Validations
def persisted?
false
end
ATTRIBUTES = [:first_name, :last_name, :email, :grade, :locale, :country]
attr_accessor *ATTRIBUTES
def initialize(attributes = {})
ATTRIBUTES.each do |attribute|
send("#{attribute}=", attributes[attribute])
end
end
validates :first_name, :last_name, :email, :grade, :locale, :country, presence: true
def student
@student ||= Student.new(first_name: first_name, last_name: last_name, email: email)
end
def student_profile
@student_profile ||= StudentProfile.new(grade: grade, locale: locale, country: country)
end
def save
return false unless valid?
if valid_objects?
Service::CreateStudentWithProfile.new(user, student_profile).run
Service::NotifyUserCreation.new(user).run
else
false
end
end
private
def valid_objects?
user.valid? && student_profile.valid?
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment