Class: Customer

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/customer.rb

Overview

The Customer model handles customers.

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Object) find_by_auth_token(authentication_token)

Get the customer object via the authentication token.

Parameters:

  • authentication_token (String)

    The authentication token.



68
69
70
71
72
73
74
75
76
# File 'app/models/customer.rb', line 68

def self.find_by_auth_token(authentication_token)
  authorization = Authorization.where(:provider => 'devise', :token => authentication_token).first

  unless authorization.blank?
    unless authorization.customer.blank?
      authorization.customer
    end
  end
end

+ (Object) from_facebook(auth_token, user_data)

Authenticate the user via facebook.

Parameters:

  • auth_toke (Symbol)

    The authentication token.

  • user_data (Symbol)

    The user data.



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'app/models/customer.rb', line 123

def self.from_facebook(auth_token, user_data)
  authorization = Authorization.where(:provider => 'facebook', :uid => user_data['id'].to_s, :token => auth_token).first_or_initialize

  if authorization.customer.blank?
    customer = Customer.where('email = ?', user_data['email']).first

    if customer.blank?
      customer = Customer.new
      customer.password = Devise.friendly_token
      customer.first_name = user_data['first_name']
      customer.last_name = user_data['last_name']
      customer.email = user_data['email']
      customer.save
    end

    authorization.username = user_data['username']
    authorization.customer_id = customer.id
    authorization.save
  end
  authorization.customer
end

+ (Object) from_omniauth(auth, current_customer)

Authenticate the user via omni auth.

Parameters:

  • auth (Symbol)

    The symbol.

  • current_customer (Symbol)

    The current customer.



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'app/models/customer.rb', line 99

def self.from_omniauth(auth, current_customer)
  authorization = Authorization.where(:provider => auth.provider, :uid => auth.uid.to_s, :token => auth.credentials.token, :secret => auth.credentials.secret).first_or_initialize
  if authorization.customer.blank?
    customer = current_customer.nil? ? Customer.where('email = ?', auth['info']['email']).first : current_customer
    if customer.blank?
      customer = Customer.new
      customer.password = Devise.friendly_token
      customer.first_name = auth['info']['first_name']
      customer.last_name = auth['info']['last_name']
      customer.email = auth.info.email
      auth.provider == 'twitter' ?  customer.save(:validate => false) :  customer.save
    end
    authorization.username = auth.info.nickname
    authorization.customer_id = customer.id
    authorization.save
  end
  authorization.customer
end

+ (Object) new_with_session(params, session)

Create a new session.

Parameters:

  • params (Symbol)

    The parameters.

  • session (Symbol)

    The session.



83
84
85
86
87
88
89
90
91
92
# File 'app/models/customer.rb', line 83

def self.new_with_session(params, session)
  if session['devise.user_attributes']
    new(session['devise.user_attributes'], without_protection: true) do |customer|
      customer.attributes = params
      customer.valid?
    end
  else
    super
  end
end

Instance Method Details

- (Object) ensure_authentication_token

Generate a new authentication token for the customer.



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

def ensure_authentication_token
  authorization = Authorization.where(:provider => 'devise', :customer_id => id.to_s).first_or_initialize

  if authorization.token.blank?
      #generate new token
      authorization.token = Devise.friendly_token
  end

  authorization.save
end

- (Object) get_customer_name

Get the full customer name



21
22
23
# File 'app/models/customer.rb', line 21

def get_customer_name
  first_name + ' ' + last_name
end

- (Object) get_devise_auth_token

Get the authentication token for the customer.



40
41
42
43
44
45
46
# File 'app/models/customer.rb', line 40

def get_devise_auth_token
  authorization = Authorization.where(:provider => 'devise', :customer_id => id.to_s).first

  unless authorization.blank?
    authorization.token
  end
end

- (Object) remove_devise_auth_token(authentication_token)

Remove the authentication token for the customer.

Parameters:

  • authentication_token (String)

    The authentication token.



52
53
54
55
56
57
58
59
60
61
62
# File 'app/models/customer.rb', line 52

def remove_devise_auth_token(authentication_token)
  authorization = Authorization.where(:provider => 'devise', :token => authentication_token).first

  unless authorization.blank?
    if authorization.destroy
      return true
    end
  end

  return false
end