Created
January 3, 2018 13:48
-
-
Save rasmar/dcbff4fb7a056c419aae99800bcc005a to your computer and use it in GitHub Desktop.
FormObject
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
| def create | |
| @form_object = UserRegistration::RegistrationForm.new(user_params, params[:notify]) | |
| if @form_object.register | |
| ... #typically we flash and redirect here | |
| else | |
| render :new | |
| end | |
| end |
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
| module UserRegistration | |
| class RegistrationForm | |
| include ActiveModel::Model | |
| attr_accessor( | |
| :email, | |
| :name, | |
| :notify, | |
| ) | |
| validates :email, presence: true | |
| validates :name, presence: true | |
| def initialize(email, name, notify) | |
| @email = email | |
| @name = name | |
| @notify = notify | |
| end | |
| def register | |
| initialize_user | |
| return false unless valid? | |
| result = user.save | |
| send_notifications | |
| result ? success : failure | |
| end | |
| private | |
| def initialize_user | |
| @user = User.new(email: email, name: name) | |
| end | |
| def send_notifications | |
| return unless notify | |
| NotifyService::Administrator.call(params) | |
| NotifyService::UserGroups.call(params) | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment