Class: FlexCommerce::CustomerAccount

Inherits:
FlexCommerceApi::ApiBase show all
Defined in:
app/models/customer_account.rb

Overview

A flex commerce Customer Account model

This model provides access to the flex commerce customer account and associated cart. It is used much like an active record model.

Examples:

# Creating an account

FlexCommerce::CustomerAccount.create #creates and returns a new account ready for use

# Fetching its cart

account.cart

# Finding an account

FlexCommerce::CustomerAccount.find(<<customer_account_id>>) # Finds the account with this unique id

Constant Summary

Constants inherited from FlexCommerceApi::BaseResource

FlexCommerceApi::BaseResource::PRIVATE_ATTRIBUTES, FlexCommerceApi::BaseResource::RELATED_META_RESOURCES

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from FlexCommerceApi::ApiBase

endpoint_version

Methods inherited from FlexCommerceApi::BaseResource

all, append_version, #as_json_api, capture_surrogate_keys, create!, endpoint_version, find, find_all, #freeze, #initialize, load, #meta_attribute, #method_missing, paginate, password, path, #public_attributes, reconfigure, reconfigure_all, reconfigure_api_base, reload_connection_if_required, #save!, username

Constructor Details

This class inherits a constructor from FlexCommerceApi::BaseResource

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class FlexCommerceApi::BaseResource

Class Method Details

.authenticate(attributes = {}) ⇒ Object



51
52
53
54
55
# File 'app/models/customer_account.rb', line 51

def self.authenticate(attributes = {})
  FlexCommerce::CustomerAccountAuthentication.create(attributes).
rescue ::FlexCommerceApi::Error::NotFound
  nil
end

.find_by_email(email) ⇒ Object



57
58
59
60
61
# File 'app/models/customer_account.rb', line 57

def self.find_by_email(email)
  requestor.custom("email:#{URI.encode_www_form_component(email)}", {request_method: :get}, {}).first
rescue ::FlexCommerceApi::Error::NotFound
  nil
end

.find_by_reference(reference) ⇒ Object



63
64
65
66
67
# File 'app/models/customer_account.rb', line 63

def self.find_by_reference(reference)
  requestor.custom("reference:#{reference}", {request_method: :get}, {}).first
rescue ::FlexCommerceApi::Error::NotFound
  nil
end

.find_by_token(token) ⇒ Object

Find customer account by password reset token provided in email’s link Used in reset password scenario



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

def self.find_by_token(token)
  requestor.custom("token:#{token}", {request_method: :get}, {}).first
rescue ::FlexCommerceApi::Error::NotFound
  nil
end

Instance Method Details

#cartObject

Provides access to the customers cart

Returns:

  • FlexCommerce::Cart



# File 'app/models/customer_account.rb', line 33

#create_note(attributes = {}) ⇒ Object



92
93
94
# File 'app/models/customer_account.rb', line 92

def create_note(attributes = {})
  ::FlexCommerce::Note.create(attributes.merge(attached_to_id: self.id, attached_to_type: self.class.name.demodulize))
end

#findFlexCommerce::CustomerAccount

Finds an account

Parameters:

  • spec (String)

Returns:

Raises:



# File 'app/models/customer_account.rb', line 27

#generate_token(attributes) ⇒ Object



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

def generate_token(attributes)
  ::FlexCommerce::PasswordRecovery.create(attributes.merge(customer_account_id: id))
end

#ordersObject



87
88
89
90
# File 'app/models/customer_account.rb', line 87

def orders
  return super if relationships[:orders].key?("data")
  get_related(:orders)
end

#reset_password(attributes) ⇒ Object



81
82
83
84
85
# File 'app/models/customer_account.rb', line 81

def reset_password(attributes)
  password_recovery.id = nil # because it is singletone resource, otherwise id is injected into path
  password_recovery.update(attributes.merge(customer_account_id: id))
  password_recovery
end