Class: Gemgento::User

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

Overview

Author:

  • Gemgento LLC

Defined Under Namespace

Classes: AddressesController, BaseController, HomeController, OrdersController, PasswordsController, RecurringProfilesController, RegistrationSessionController, RegistrationsController, SavedCreditCardsController, SessionsController, WishlistItemsController

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#subscribeObject

Returns the value of attribute subscribe.



22
23
24
# File 'app/models/gemgento/user.rb', line 22

def subscribe
  @subscribe
end

#sync_neededObject

Returns the value of attribute sync_needed.



29
30
31
# File 'app/models/gemgento/user.rb', line 29

def sync_needed
  @sync_needed
end

Class Method Details

.is_valid_login(email, password) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/models/gemgento/user.rb', line 40

def self.(email, password)

  # If no customer with email is known, double check Magento
  if !Gemgento::User.exists?(email: email) && customer = Gemgento::Magento::CustomerAdapter.find_by(email: email)
    Gemgento::API::SOAP::Customer::Customer.fetch(customer[:customer_id])
  end

  user = User.find_by(email: email)

  if user.nil? || !user.is_valid_password(password)
    user = nil
  end

  user
end

Instance Method Details

#as_json(options = nil) ⇒ Object



112
113
114
115
116
117
# File 'app/models/gemgento/user.rb', line 112

def as_json(options = nil)
  result = super
  result['is_subscriber'] = self.is_subscriber?

  return result
end

#default_billing_addressObject



96
97
98
# File 'app/models/gemgento/user.rb', line 96

def default_billing_address
  self.addresses.find_by(is_billing: true)
end

#default_shipping_addressObject



100
101
102
# File 'app/models/gemgento/user.rb', line 100

def default_shipping_address
  self.addresses.find_by(is_shipping: true)
end

#is_subscriber?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'app/models/gemgento/user.rb', line 83

def is_subscriber?
  return !Subscriber.find_by(email: self.email).nil?
end

#is_valid_password(password) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'app/models/gemgento/user.rb', line 56

def is_valid_password(password)
  unless self.encrypted_password.blank?
    return self.valid_password?(password)
  else
    if self.magento_password.blank? || !self.magento_password.include?(':') # if we don't have any passwords, get them from Magento
      API::SOAP::Customer::Customer.fetch(self.magento_id)
      self.reload
    end

    salt = self.magento_password.split(':')[1]
    encrypted_password = Digest::MD5.hexdigest(salt + password)
    encrypted_password += ':' + salt

    if self.magento_password == encrypted_password
      self.magento_password = nil
      self.password = password
      self.password_confirmation = password
      self.sync_needed = false
      self.save

      return true
    else
      return false
    end
  end
end

#manage_subscribeObject



123
124
125
126
# File 'app/models/gemgento/user.rb', line 123

def manage_subscribe
  self.subscribe = false if self.subscribe == 0 || self.subscribe == '0'
  Subscriber.manage self, self.subscribe
end

#mark_deletedObject



87
88
89
# File 'app/models/gemgento/user.rb', line 87

def mark_deleted
  self.deleted_at = Time.now
end

#mark_deleted!Object



91
92
93
94
# File 'app/models/gemgento/user.rb', line 91

def mark_deleted!
  mark_deleted
  self.save
end

#nameObject



36
37
38
# File 'app/models/gemgento/user.rb', line 36

def name
  "#{self.first_name} #{self.last_name}".strip
end

#password_confirmation=(value) ⇒ Object



104
105
106
107
108
109
110
# File 'app/models/gemgento/user.rb', line 104

def password_confirmation=(value)
  @password_confirmation = value

  # password changes must be pushed to magento, so that
  # magento customer updates don't revert the password
  self.sync_needed = true
end