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
- #auto_generate_spree_api_key ⇒ Object
- #available_store_credit_total(currency:) ⇒ Object
-
#can_be_deleted? ⇒ Boolean
Restrict to delete users with existing orders.
- #display_available_store_credit_total(currency:) ⇒ Object
-
#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.
-
#last_incomplete_spree_order(store: nil, only_frontend_viewable: true) ⇒ Spree::Order
since the customer’s last complete order.
-
#update_spree_roles(given_roles, ability:) ⇒ Object
Updates the roles in keeping with the given ability’s permissions.
- #wallet ⇒ Object
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
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_key ⇒ Object
86 87 88 89 90 91 92 |
# File 'app/models/concerns/spree/user_methods.rb', line 86 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
106 107 108 109 110 |
# File 'app/models/concerns/spree/user_methods.rb', line 106 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?
127 128 129 |
# File 'app/models/concerns/spree/user_methods.rb', line 127 def can_be_deleted? orders.none? end |
#display_available_store_credit_total(currency:) ⇒ Object
112 113 114 115 116 117 |
# File 'app/models/concerns/spree/user_methods.rb', line 112 def display_available_store_credit_total(currency:) Spree::Money.new( available_store_credit_total(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.
82 83 84 |
# File 'app/models/concerns/spree/user_methods.rb', line 82 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.
96 97 98 99 100 101 102 103 104 |
# File 'app/models/concerns/spree/user_methods.rb', line 96 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:) 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.
139 140 141 142 143 144 |
# File 'app/models/concerns/spree/user_methods.rb', line 139 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 |