Skip to content

Instantly share code, notes, and snippets.

@PerezIgnacio
Last active February 5, 2020 15:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PerezIgnacio/da03bb1174565f63d392af570f78b592 to your computer and use it in GitHub Desktop.
Save PerezIgnacio/da03bb1174565f63d392af570f78b592 to your computer and use it in GitHub Desktop.
User registration form object example
class UserRegistrationForm
include ActiveModel::Model
attr_accessor :name, :email, :phone, :password, :password_confirmation, :min_price, :max_price,
:rating, :discount, :provider_ids, :warranty, :keywords
validates :phone, presence: true
def save
return false unless valid?
save_models
# ...
# additional code related to user creation if necessary, such as sending notifications or log event
true
end
def user
@user ||= User.new(name: name, email: email, phone: phone, password: password,
password_confirmation: password_confirmation, user_request: user_request)
end
private
def save_models
ActiveRecord::Base.transaction do
user_request.save!
user.save!
end
end
def user_request
@user_request ||= UserRequest.new(min_price: min_price, max_price: max_price, rating: rating,
discount: discount, provider_ids: provider_ids,
warranty: warranty, keywords: keywords)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment