-
-
Save PerezIgnacio/da03bb1174565f63d392af570f78b592 to your computer and use it in GitHub Desktop.
User registration form object example
This file contains 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 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