Files
nairobi/app/models/verification/sms.rb
Javi Martín d0d681a44b Add and apply EmptyLineAfterGuardClause rule
We were inconsistent on this one. I consider it particularly useful when
a method starts with a `return` statement.

In other cases, we probably shouldn't have a guard rule in the middle of
a method in any case, but that's a different refactoring.
2019-10-24 17:56:03 +02:00

40 lines
842 B
Ruby

class Verification::Sms
include ActiveModel::Model
attr_accessor :user, :phone, :confirmation_code
validates :phone, presence: true
validates :phone, format: { with: /\A[\d \+]+\z/ }
validate :uniqness_phone
def uniqness_phone
errors.add(:phone, :taken) if User.where(confirmed_phone: phone).any?
end
def save
return false unless valid?
update_user_phone_information
send_sms
Lock.increase_tries(user)
end
def update_user_phone_information
user.update(unconfirmed_phone: phone, sms_confirmation_code: generate_confirmation_code)
end
def send_sms
SMSApi.new.sms_deliver(user.unconfirmed_phone, user.sms_confirmation_code)
end
def verified?
user.sms_confirmation_code == confirmation_code
end
private
def generate_confirmation_code
rand.to_s[2..5]
end
end