We were getting a few errors when trying out Zeitwerk: ``` expected file lib/sms_api.rb to define constant SmsApi expected file app/components/layout/common_html_attributes_component.rb to define constant Layout::CommonHtmlAttributesComponent ``` In these cases, we aren't using an inflection because we also define the `Verification::SmsController` and a few migrations containing `Html` in their class name, and none of them would work if we defined the inflection. We were also getting an error regarding classes containing WYSIWYG in its name: ``` NameError: uninitialized constant WYSIWYGSanitizer Did you mean? WysiwygSanitizer ``` In this case, adding the acronym is easier, since we never use "Wysiwyg" in the code but we use "WYSIWYG" in many places.
40 lines
842 B
Ruby
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
|