Module: Spree::UserMethods
- Extended by:
- ActiveSupport::Concern
- Includes:
- Metadata, Metafields, PrefixedId, Publishable, RansackableAttributes, Searchable, UserPaymentSource, UserReporting, UserRoles
- Included in:
- LegacyUser
- Defined in:
- app/models/concerns/spree/user_methods.rb
Defined Under Namespace
Modules: SkipPasswordValidation
Constant Summary
Constants included from PrefixedId
Instance Method Summary collapse
-
#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_prefix ⇒ Object
- #event_serializer_class ⇒ Object
-
#full_name ⇒ String
Returns the full name of the user.
-
#last_incomplete_spree_order(store, options = {}) ⇒ Spree::Order
Returns the last incomplete spree order for the current store.
-
#newsletter_subscriber(store = Spree::Current.store) ⇒ Spree::NewsletterSubscriber?
This user's newsletter subscriber for the given store.
-
#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 Publishable
#event_context, #event_payload, #publish_event
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 Metadata
#metadata, #metadata=, #public_metadata=
Methods included from PrefixedId
#assign_attributes, decode_prefixed_id, #prefixed_id, prefixed_id?, #to_param
Instance Method Details
#available_store_credits(store) ⇒ Array<Spree::Money>
Returns the available store credits for the current store per currency
237 238 239 240 241 242 243 |
# File 'app/models/concerns/spree/user_methods.rb', line 237 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
258 259 260 |
# File 'app/models/concerns/spree/user_methods.rb', line 258 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
249 250 251 252 253 |
# File 'app/models/concerns/spree/user_methods.rb', line 249 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: Spree.t(:default_wishlist_name)) end end |
#event_prefix ⇒ Object
279 280 281 282 283 284 285 |
# File 'app/models/concerns/spree/user_methods.rb', line 279 def event_prefix if self.class == Spree.admin_user_class && Spree.admin_user_class != Spree.user_class 'admin' else 'user' end end |
#event_serializer_class ⇒ Object
275 276 277 |
# File 'app/models/concerns/spree/user_methods.rb', line 275 def event_serializer_class 'Spree::Api::V3::CustomerSerializer'.safe_constantize end |
#full_name ⇒ String
Returns the full name of the user
271 272 273 |
# File 'app/models/concerns/spree/user_methods.rb', line 271 def full_name name&.full end |
#last_incomplete_spree_order(store, options = {}) ⇒ Spree::Order
Returns the last incomplete spree order for the current store
206 207 208 209 210 211 |
# File 'app/models/concerns/spree/user_methods.rb', line 206 def last_incomplete_spree_order(store, = {}) 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.
289 290 291 292 293 |
# File 'app/models/concerns/spree/user_methods.rb', line 289 def (store = Spree::Current.store) return unless store Spree::NewsletterSubscriber.for_store(store).find_by(user_id: id) end |
#to_csv(_store = nil) ⇒ Array<String>
Returns the CSV row representation of the user
265 266 267 |
# File 'app/models/concerns/spree/user_methods.rb', line 265 def to_csv(_store = nil) Spree::CSV::CustomerPresenter.new(self).call end |
#total_available_store_credit(currency = nil, store = nil) ⇒ Float
Returns the total available store credit for the current store per currency
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'app/models/concerns/spree/user_methods.rb', line 217 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 |