Module: Spree::CustomerMethods
- Extended by:
- ActiveSupport::Concern
- Includes:
- HasCustomFields, PrefixedId, Publishable, RansackableAttributes, SanitizableRichText, Searchable, SignedIdAttachments, UserAddress, UserPaymentSource, UserReporting, UserRoles
- Included in:
- Customer
- Defined in:
- app/models/concerns/spree/customer_methods.rb
Constant Summary
Constants included from Publishable
Publishable::LIFECYCLE_ACTIONS
Constants included from RansackableAttributes
RansackableAttributes::RESTRICTED_AUDIENCES
Constants included from PrefixedId
Instance Method Summary collapse
-
#anonymized? ⇒ Boolean
Whether this customer's personal data has been erased.
-
#available_store_credits(store) ⇒ Array<Spree::Money>
Returns the available store credits for the current store per currency.
-
#can_be_deleted? ⇒ Boolean
Returns true if the user can be deleted Customers can be deleted if they have no completed orders.
-
#default_wishlist_for_store(current_store) ⇒ Spree::Wishlist
Returns the default wishlist for the current store if no default wishlist exists, it creates one.
- #event_serializer_class ⇒ Object
-
#full_name ⇒ String
Returns the full name of the user.
-
#last_incomplete_spree_order(store, options = {}) ⇒ Spree::Order
deprecated
Deprecated.
Carts are Cart since 6.0, so an incomplete order is a backoffice draft rather than the customer's cart. Use
carts.where(store: store)for carts, ororders.draftsfor drafts. Removed in Spree 6.3. -
#newsletter_subscriber(store = Spree::Current.store) ⇒ Spree::NewsletterSubscriber?
This user's newsletter subscriber for the given store.
-
#scramble_email_and_names ⇒ Object
deprecated
Deprecated.
Superseded by Spree::Customers::Anonymize, which erases every column carrying personal data rather than three on this row — the address book, the address snapshots on past orders, gateway profiles, OAuth identities and live sessions all outlived this method. Removed in Spree 6.3.
-
#to_csv(store = nil) ⇒ Array<String>
Returns the CSV row representation of the user.
-
#total_available_store_credit(currency = nil, store = nil) ⇒ Float
Returns the total available store credit for the current store per currency.
Methods included from SanitizableRichText
Methods included from Publishable
#event_context, #event_payload, #event_payload_for, #event_prefix, #publish_event, #touch
Methods included from RansackableAttributes
restricted_audience?, scope_type_from_signature, signature_hidden?
Methods included from UserReporting
#amount_spent_in, #average_order_value, #completed_orders_for_store, #display_amount_spent_in, #lifetime_value, #order_count, #report_values_for
Methods included from DisplayMoney
Methods included from PrefixedId
#assign_attributes, decode_prefixed_id, #prefixed_id, prefixed_id?, #to_param
Instance Method Details
#anonymized? ⇒ Boolean
Whether this customer's personal data has been erased.
407 408 409 |
# File 'app/models/concerns/spree/customer_methods.rb', line 407 def anonymized? anonymized_at.present? end |
#available_store_credits(store) ⇒ Array<Spree::Money>
Returns the available store credits for the current store per currency
339 340 341 342 343 344 345 |
# File 'app/models/concerns/spree/customer_methods.rb', line 339 def available_store_credits(store) store ||= Store.default store_credits.for_store(store).pluck(:currency).uniq.each_with_object([]) do |currency, arr| arr << Spree::Money.new(total_available_store_credit(currency, store), currency: currency) end end |
#can_be_deleted? ⇒ Boolean
Returns true if the user can be deleted Customers can be deleted if they have no completed orders
360 361 362 |
# File 'app/models/concerns/spree/customer_methods.rb', line 360 def can_be_deleted? orders.complete.none? end |
#default_wishlist_for_store(current_store) ⇒ Spree::Wishlist
Returns the default wishlist for the current store if no default wishlist exists, it creates one
351 352 353 354 355 |
# File 'app/models/concerns/spree/customer_methods.rb', line 351 def default_wishlist_for_store(current_store) wishlists.find_by(is_default: true, store_id: current_store.id) || ActiveRecord::Base.connected_to(role: :writing) do wishlists.create!(store: current_store, is_default: true, name: I18n.t('spree.default_wishlist_name')) end end |
#event_serializer_class ⇒ Object
380 381 382 |
# File 'app/models/concerns/spree/customer_methods.rb', line 380 def event_serializer_class 'Spree::Api::V3::CustomerSerializer'.safe_constantize end |
#full_name ⇒ String
Returns the full name of the user
376 377 378 |
# File 'app/models/concerns/spree/customer_methods.rb', line 376 def full_name name&.full end |
#last_incomplete_spree_order(store, options = {}) ⇒ Spree::Order
Carts are Spree::Cart since 6.0, so an incomplete order is
a backoffice draft rather than the customer's cart. Use
carts.where(store: store) for carts, or orders.drafts for drafts.
Removed in Spree 6.3.
Returns the last incomplete spree order for the current store
303 304 305 306 307 308 309 310 311 312 313 |
# File 'app/models/concerns/spree/customer_methods.rb', line 303 def last_incomplete_spree_order(store, = {}) Spree::Deprecation.warn( 'Spree::CustomerMethods#last_incomplete_spree_order is deprecated and will be removed in Spree 6.3. ' \ 'Carts are Spree::Cart since 6.0 and this returns draft orders; use #carts for the customer\'s carts.' ) orders.where(store: store).incomplete.not_canceled. includes([:includes]). order('created_at DESC'). first end |
#newsletter_subscriber(store = Spree::Current.store) ⇒ Spree::NewsletterSubscriber?
Returns this user's newsletter subscriber for the given store.
386 387 388 389 390 |
# File 'app/models/concerns/spree/customer_methods.rb', line 386 def (store = Spree::Current.store) return unless store Spree::NewsletterSubscriber.for_store(store).find_by(customer_id: id) end |
#scramble_email_and_names ⇒ Object
Superseded by Spree::Customers::Anonymize, which erases every column carrying personal data rather than three on this row — the address book, the address snapshots on past orders, gateway profiles, OAuth identities and live sessions all outlived this method. Removed in Spree 6.3.
397 398 399 400 401 402 403 |
# File 'app/models/concerns/spree/customer_methods.rb', line 397 def scramble_email_and_names Spree::Deprecation.warn( 'Spree::CustomerMethods#scramble_email_and_names is deprecated and will be removed in Spree 6.3. Use Spree::Customers::Anonymize instead.' ) Spree.customer_anonymize_workflow.call(customer: self, store: Spree::Current.store).success? end |
#to_csv(store = nil) ⇒ Array<String>
Returns the CSV row representation of the user
370 371 372 |
# File 'app/models/concerns/spree/customer_methods.rb', line 370 def to_csv(store = nil) Spree::CSV::CustomerPresenter.new(self, store || Spree::Current.store).call end |
#total_available_store_credit(currency = nil, store = nil) ⇒ Float
Returns the total available store credit for the current store per currency
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 |
# File 'app/models/concerns/spree/customer_methods.rb', line 319 def total_available_store_credit(currency = nil, store = nil) store ||= Store.default currency ||= store.default_currency # When the association is already loaded (e.g. preloaded via the admin # API's scope_includes), filter in memory to avoid an N+1 — but mirror # the query branch exactly: non-gift-card credits, scoped to the store # and currency. A gift card is identified by its originator_type. if store_credits.loaded? store_credits. select { |sc| sc.originator_type != 'Spree::GiftCard' && sc.store_id.to_s == store.id.to_s && sc.currency == currency }. sum(&:amount_remaining) else store_credits.without_gift_card.for_store(store).where(currency: currency).to_a.sum(&:amount_remaining) end end |