Class: UserRegistration::UsingBirthday

Inherits:
Base
  • Object
show all
Includes:
Notifications, SubscribeVoucherReward
Defined in:
app/my_lib/user_registration/using_birthday.rb

Overview

User registration using birthday data

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SubscribeVoucherReward

#voucher_reward_subscription

Methods included from Notifications

#send_registration_email, #send_registration_sms

Methods inherited from Base

#method_missing

Constructor Details

#initialize(user_params = {}) ⇒ UsingBirthday

Returns a new instance of UsingBirthday.



14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/my_lib/user_registration/using_birthday.rb', line 14

def initialize(user_params = {})
  @user = if user_params.is_a? ::User
            user_params
          else
            User.new(user_params.merge(username: user_params[:name]))
          end
  @user.password = SecureRandom.alphanumeric(16) if @user.birthday.blank?
  self.send_sms = false
  @required_attrs = []
  @saved_errors = []
  @error_reporting = true
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class UserRegistration::Base

Instance Attribute Details

#error_reportingObject

Returns the value of attribute error_reporting.



13
14
15
# File 'app/my_lib/user_registration/using_birthday.rb', line 13

def error_reporting
  @error_reporting
end

#send_smsObject

Returns the value of attribute send_sms.



13
14
15
# File 'app/my_lib/user_registration/using_birthday.rb', line 13

def send_sms
  @send_sms
end

#userObject

Returns the value of attribute user.



13
14
15
# File 'app/my_lib/user_registration/using_birthday.rb', line 13

def user
  @user
end

Instance Method Details

#generate_passwordObject



56
57
58
59
60
61
# File 'app/my_lib/user_registration/using_birthday.rb', line 56

def generate_password
  @required_attrs.push :birthday

  user.password = user.password_confirmation = user.birthday.strftime('%d%m%Y') if user.birthday.present?
  nil
end

#require_phone_numberObject



63
64
65
# File 'app/my_lib/user_registration/using_birthday.rb', line 63

def require_phone_number
  @required_attrs.push :phone
end

#saveObject



27
28
29
30
31
32
33
34
35
36
# File 'app/my_lib/user_registration/using_birthday.rb', line 27

def save
  save!
rescue UserRegistration::UserRegistrationError => e
  @saved_errors.push e
  # APMErrorHandler.report e if error_reporting
  false
rescue => e
  report_error_to_rollbar(e)
  false
end

#save!(sneaky = false) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/my_lib/user_registration/using_birthday.rb', line 38

def save!(sneaky = false)
  generate_password if user.birthday.present?
  raise(UserRegistration::UserRegistrationError, user.errors.full_messages.to_s) unless valid?

  if sneaky
    user.sneaky_save!
  else
    user.save!
  end

  send_registration_email if user.birthday.present?
  voucher_reward_subscription

  send_registration_sms if send_sms

  true
end

#valid?Boolean

Returns:

  • (Boolean)


67
68
69
70
71
72
73
74
75
# File 'app/my_lib/user_registration/using_birthday.rb', line 67

def valid?
  @required_attrs.each do |attr|
    if user.send(attr.to_sym).blank?
      errors.add(:base, "#{attr.to_s.upcase} is empty")
      return false
    end
  end
  user.valid?
end