Module: Spree::UserMethods

Extended by:
ActiveSupport::Concern
Includes:
UserAddressBook, UserApiAuthentication, UserReporting
Included in:
LegacyUser
Defined in:
app/models/concerns/spree/user_methods.rb

Instance Method Summary collapse

Methods included from UserAddressBook

#bill_address=, #bill_address_attributes=, #mark_default_bill_address, #mark_default_ship_address, #persist_order_address, #remove_from_address_book, #save_in_address_book, #ship_address=, #ship_address_attributes=

Methods included from UserReporting

#average_order_value, #lifetime_value, #order_count

Methods included from DisplayMoney

#money_methods

Methods included from UserApiAuthentication

#clear_spree_api_key, #clear_spree_api_key!, #generate_spree_api_key, #generate_spree_api_key!

Instance Method Details

#auto_generate_spree_api_keyObject



48
49
50
51
52
53
54
# File 'app/models/concerns/spree/user_methods.rb', line 48

def auto_generate_spree_api_key
  return if !respond_to?(:spree_api_key) || spree_api_key.present?

  if Spree::Config.generate_api_key_for_all_roles || (spree_roles.map(&:name) & Spree::Config.roles_for_auto_api_key).any?
    generate_spree_api_key!
  end
end

#available_store_credit_total(currency:) ⇒ Object



68
69
70
71
72
# File 'app/models/concerns/spree/user_methods.rb', line 68

def available_store_credit_total(currency:)
  store_credits.to_a.
    select { |credit| credit.currency == currency }.
    sum(&:amount_remaining)
end

#can_be_deleted?Boolean

Restrict to delete users with existing orders

Override this in your user model class to add another logic.

Ie. to allow to delete users with incomplete orders add:

orders.complete.none?

Returns:

  • (Boolean)


89
90
91
# File 'app/models/concerns/spree/user_methods.rb', line 89

def can_be_deleted?
  orders.none?
end

#display_available_store_credit_total(currency:) ⇒ Object



74
75
76
77
78
79
# File 'app/models/concerns/spree/user_methods.rb', line 74

def display_available_store_credit_total(currency:)
  Spree::Money.new(
    available_store_credit_total(currency: currency),
    currency: currency,
  )
end

#has_spree_role?(role_in_question) ⇒ Boolean

has_spree_role? simply needs to return true or false whether a user has a role or not.

Returns:

  • (Boolean)


44
45
46
# File 'app/models/concerns/spree/user_methods.rb', line 44

def has_spree_role?(role_in_question)
  spree_roles.any? { |role| role.name == role_in_question.to_s }
end

#last_incomplete_spree_order(store: nil, only_frontend_viewable: true) ⇒ Spree::Order

since the customer’s last complete order.

Returns:

  • (Spree::Order)

    the most-recently-created incomplete order



58
59
60
61
62
63
64
65
66
# File 'app/models/concerns/spree/user_methods.rb', line 58

def last_incomplete_spree_order(store: nil, only_frontend_viewable: true)
  self_orders = orders
  self_orders = self_orders.where(frontend_viewable: true) if only_frontend_viewable
  self_orders = self_orders.where(store: store) if store
  self_orders = self_orders.where('updated_at > ?', Spree::Config.completable_order_updated_cutoff_days.days.ago) if Spree::Config.completable_order_updated_cutoff_days
  self_orders = self_orders.where('created_at > ?', Spree::Config.completable_order_created_cutoff_days.days.ago) if Spree::Config.completable_order_created_cutoff_days
  last_order = self_orders.order(:created_at).last
  last_order unless last_order.try!(:completed?)
end

#update_spree_roles(given_roles, ability:) ⇒ Object

Updates the roles in keeping with the given ability’s permissions

Roles that are not accessible to the given ability will be ignored. It also ensure not to remove non accessible roles when assigning new accessible ones.

Parameters:



101
102
103
104
105
106
# File 'app/models/concerns/spree/user_methods.rb', line 101

def update_spree_roles(given_roles, ability:)
  accessible_roles = Spree::Role.accessible_by(ability)
  non_accessible_roles = Spree::Role.all - accessible_roles
  new_accessible_roles = given_roles - non_accessible_roles
  self.spree_roles = spree_roles - accessible_roles + new_accessible_roles
end

#walletObject



39
40
41
# File 'app/models/concerns/spree/user_methods.rb', line 39

def wallet
  @wallet ||= Spree::Wallet.new(self)
end