Class: Signup

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming
Includes:
ActiveModel::Conversion, ActiveModel::Validations
Defined in:
app/models/signup.rb

Overview

Responsible for handling the combo User/Account creation. Also deals with Account creation when signing in as an existing User.

Constant Summary collapse

FIELDS =
{
  :account => {
    :cardholder_name => :cardholder_name, 
    :billing_email => :billing_email, 
    :card_number => :card_number, 
    :expiration_month => :expiration_month, 
    :expiration_year => :expiration_year,
    :verification_code => :verification_code,
    :plan => :plan
  },
  :user => {
    :email    => :email,
    :password => :password,
  }
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Signup

Returns a new instance of Signup.



27
28
29
30
31
32
33
34
# File 'app/models/signup.rb', line 27

def initialize(attributes = {})
  if attributes
    attributes.each do |attribute, value|
      send(:"#{attribute}=", value)
    end
  end
  @check_password = true
end

Instance Attribute Details

#billing_emailObject

Returns the value of attribute billing_email.



24
25
26
# File 'app/models/signup.rb', line 24

def billing_email
  @billing_email
end

#card_numberObject

Returns the value of attribute card_number.



24
25
26
# File 'app/models/signup.rb', line 24

def card_number
  @card_number
end

#cardholder_nameObject

Returns the value of attribute cardholder_name.



24
25
26
# File 'app/models/signup.rb', line 24

def cardholder_name
  @cardholder_name
end

#emailObject

Returns the value of attribute email.



24
25
26
# File 'app/models/signup.rb', line 24

def email
  @email
end

#expiration_monthObject

Returns the value of attribute expiration_month.



24
25
26
# File 'app/models/signup.rb', line 24

def expiration_month
  @expiration_month
end

#expiration_yearObject

Returns the value of attribute expiration_year.



24
25
26
# File 'app/models/signup.rb', line 24

def expiration_year
  @expiration_year
end

#passwordObject

Returns the value of attribute password.



24
25
26
# File 'app/models/signup.rb', line 24

def password
  @password
end

#planObject

Returns the value of attribute plan.



24
25
26
# File 'app/models/signup.rb', line 24

def plan
  @plan
end

#verification_codeObject

Returns the value of attribute verification_code.



24
25
26
# File 'app/models/signup.rb', line 24

def verification_code
  @verification_code
end

Instance Method Details

#accountObject



64
65
66
# File 'app/models/signup.rb', line 64

def 
  @account ||= Account.new
end

#existing_userObject



81
82
83
# File 'app/models/signup.rb', line 81

def existing_user
  @existing_user ||= User.find_by_email(email)
end

#membershipObject



85
86
87
88
89
# File 'app/models/signup.rb', line 85

def membership
  @membership ||= Membership.new(:user    => user,
                                 :account => ,
                                 :admin   => true)
end

#new_userObject



77
78
79
# File 'app/models/signup.rb', line 77

def new_user
  @new_user ||= User.new
end

#persisted?Boolean

used by ActiveModel

Returns:

  • (Boolean)


37
38
39
# File 'app/models/signup.rb', line 37

def persisted?
  false
end

#saveObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/models/signup.rb', line 41

def save
  delegate_attributes_for(:account)
  

  if !existing_user
    delegate_attributes_for(:user) 
    populate_additional_user_fields
  end

  if valid?
    begin
      save!
      true
    rescue ActiveRecord::RecordNotSaved
      delegate_errors_for(:account)
      delegate_errors_for(:user)
      false
    end
  else
    false
  end
end

#userObject



68
69
70
# File 'app/models/signup.rb', line 68

def user
  existing_user || new_user
end

#user=(signed_in_user) ⇒ Object



72
73
74
75
# File 'app/models/signup.rb', line 72

def user=(signed_in_user)
  @check_password = false
  @existing_user  = signed_in_user
end

#valid?Boolean

Returns:

  • (Boolean)


91
92
93
94
95
# File 'app/models/signup.rb', line 91

def valid?
  errors.clear
  validate
  errors.empty?
end