Class: AuctionFunCore::Operations::UserContext::RegistrationOperation
- Defined in:
- lib/auction_fun_core/operations/user_context/registration_operation.rb
Overview
Operation class for create new users.
Class Method Summary collapse
Instance Method Summary collapse
- #call(attributes) ⇒ Object
-
#confirmation_attributes(attrs) ⇒ Hash
Adds confirmation parameters to the given attributes.
-
#encrypt_password(attrs) ⇒ Hash
Transforms the password attribute, encrypting it to be saved in the database.
-
#persist(result) ⇒ ROM::Struct::User
Calls the user repository class to persist the attributes in the database.
-
#publish_user_registration(user_id) ⇒ Dry::Monads::Result::Success
Triggers the publication of event users.registration.
-
#send_welcome_email(user_id) ⇒ Dry::Monads::Result::Success
Schedule the asynchronous sending of a welcome email.
-
#validate_contract(attrs) ⇒ Dry::Monads::Result::Success, Dry::Monads::Result::Failure
Calls registration contract class to perform the validation of the informed attributes.
Class Method Details
.call(attributes, &block) ⇒ Object
13 14 15 16 17 18 19 |
# File 'lib/auction_fun_core/operations/user_context/registration_operation.rb', line 13 def self.call(attributes, &block) operation = new.call(attributes) return operation unless block Dry::Matcher::ResultMatcher.call(operation, &block) end |
Instance Method Details
#call(attributes) ⇒ Object
Add custom doc
22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/auction_fun_core/operations/user_context/registration_operation.rb', line 22 def call(attributes) values = yield validate_contract(attributes) values_with_encrypt_password = yield encrypt_password(values) values_with_confirmation_attributes = yield confirmation_attributes(values_with_encrypt_password) user_repository.transaction do |_t| @user = yield persist(values_with_confirmation_attributes) yield publish_user_registration(@user.id) yield send_welcome_email(@user.id) end Success(@user) end |
#confirmation_attributes(attrs) ⇒ Hash
Adds confirmation parameters to the given attributes.
63 64 65 |
# File 'lib/auction_fun_core/operations/user_context/registration_operation.rb', line 63 def confirmation_attributes(attrs) Success({**attrs, email_confirmation_token: generate_email_token}) end |
#encrypt_password(attrs) ⇒ Hash
Transforms the password attribute, encrypting it to be saved in the database.
52 53 54 55 56 57 58 |
# File 'lib/auction_fun_core/operations/user_context/registration_operation.rb', line 52 def encrypt_password(attrs) attributes = attrs.to_h.except(:password, :password_confirmation) Success( {**attributes, password_digest: BCrypt::Password.create(attrs[:password])} ) end |
#persist(result) ⇒ ROM::Struct::User
Calls the user repository class to persist the attributes in the database.
70 71 72 |
# File 'lib/auction_fun_core/operations/user_context/registration_operation.rb', line 70 def persist(result) Success(user_repository.create(result)) end |
#publish_user_registration(user_id) ⇒ Dry::Monads::Result::Success
Triggers the publication of event users.registration.
77 78 79 80 81 |
# File 'lib/auction_fun_core/operations/user_context/registration_operation.rb', line 77 def publish_user_registration(user_id) user = user_repository.by_id!(user_id) Success(Application[:event].publish("users.registration", user.info)) end |
#send_welcome_email(user_id) ⇒ Dry::Monads::Result::Success
Schedule the asynchronous sending of a welcome email.
86 87 88 |
# File 'lib/auction_fun_core/operations/user_context/registration_operation.rb', line 86 def send_welcome_email(user_id) Success(registration_mailer_job.perform_async(user_id)) end |
#validate_contract(attrs) ⇒ Dry::Monads::Result::Success, Dry::Monads::Result::Failure
Calls registration contract class to perform the validation of the informed attributes.
41 42 43 44 45 46 47 |
# File 'lib/auction_fun_core/operations/user_context/registration_operation.rb', line 41 def validate_contract(attrs) contract = registration_contract.call(attrs) return Failure(contract.errors.to_h) if contract.failure? Success(contract.to_h) end |