Last active
December 16, 2015 16:48
-
-
Save gogogarrett/5465349 to your computer and use it in GitHub Desktop.
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 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