Skip to content

Instantly share code, notes, and snippets.

@rasmar
Created January 3, 2018 13:48
Show Gist options
  • Select an option

  • Save rasmar/dcbff4fb7a056c419aae99800bcc005a to your computer and use it in GitHub Desktop.

Select an option

Save rasmar/dcbff4fb7a056c419aae99800bcc005a to your computer and use it in GitHub Desktop.
FormObject
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
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