Module: Spree::UserMethods
- Extended by:
- ActiveSupport::Concern
- Includes:
- AdminUserMethods, Metafields, MultiSearchable, RansackableAttributes, UserPaymentSource, UserReporting, UserRoles
- Included in:
- LegacyUser
- Defined in:
- app/models/concerns/spree/user_methods.rb
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.
-
#default_wishlist_for_store(current_store) ⇒ Spree::Wishlist
Returns the default wishlist for the current store if no default wishlist exists, it creates one.
-
#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.
-
#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 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
Instance Method Details
#available_store_credits(store) ⇒ Array<Spree::Money>
Returns the available store credits for the current store per currency
119 120 121 122 123 124 125 |
# File 'app/models/concerns/spree/user_methods.rb', line 119 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
139 140 141 142 143 144 145 |
# File 'app/models/concerns/spree/user_methods.rb', line 139 def can_be_deleted? if role_users.where(resource: Spree::Store.current).exists? Spree::Store.current.users.where.not(id: id).exists? else orders.complete.none? end 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
131 132 133 134 135 |
# File 'app/models/concerns/spree/user_methods.rb', line 131 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 |
#full_name ⇒ String
Returns the full name of the user
156 157 158 |
# File 'app/models/concerns/spree/user_methods.rb', line 156 def full_name name&.full end |
#last_incomplete_spree_order(store, options = {}) ⇒ Spree::Order
Returns the last incomplete spree order for the current store
99 100 101 102 103 104 |
# File 'app/models/concerns/spree/user_methods.rb', line 99 def last_incomplete_spree_order(store, = {}) orders.where(store: store).incomplete.not_canceled. includes([:includes]). order('created_at DESC'). first end |
#to_csv(_store = nil) ⇒ Array<String>
Returns the CSV row representation of the user
150 151 152 |
# File 'app/models/concerns/spree/user_methods.rb', line 150 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
110 111 112 113 114 |
# File 'app/models/concerns/spree/user_methods.rb', line 110 def total_available_store_credit(currency = nil, store = nil) store ||= Store.default currency ||= store.default_currency store_credits.without_gift_card.for_store(store).where(currency: currency).reload.to_a.sum(&:amount_remaining) end |