MErge user class extensions into the User class

This commit is contained in:
Luis Ramos
2020-08-19 09:45:06 +01:00
parent 737fc699ed
commit ebf4175662
2 changed files with 17 additions and 26 deletions

View File

@@ -7,6 +7,13 @@ module Spree
belongs_to :ship_address, foreign_key: 'ship_address_id', class_name: 'Spree::Address'
belongs_to :bill_address, foreign_key: 'bill_address_id', class_name: 'Spree::Address'
has_and_belongs_to_many :spree_roles,
join_table: 'spree_roles_users',
foreign_key: "user_id",
class_name: "Spree::Role"
has_many :spree_orders, foreign_key: "user_id", class_name: "Spree::Order"
before_validation :set_login
before_destroy :check_completed_orders
@@ -41,10 +48,16 @@ module Spree
User.admin.count > 0
end
# Whether a user has a role or not.
def has_spree_role?(role_in_question)
spree_roles.where(name: role_in_question.to_s).any?
end
def admin?
has_spree_role?('admin')
end
# handle_asynchronously will define send_reset_password_instructions_with_delay.
# If handle_asynchronously is called twice, we get an infinite job loop.
handle_asynchronously :send_reset_password_instructions unless method_defined? :send_reset_password_instructions_with_delay
@@ -125,6 +138,10 @@ module Spree
save!
end
def last_incomplete_spree_order
spree_orders.incomplete.where(created_by_id: id).order('created_at DESC').first
end
protected
def password_required?

View File

@@ -1,26 +0,0 @@
# frozen_string_literal: true
Spree::Core::Engine.config.to_prepare do
# has_spree_role? simply needs to return true or false whether a user has a role or not.
Spree.user_class&.class_eval do
include Spree::Core::UserBanners
has_and_belongs_to_many :spree_roles,
join_table: 'spree_roles_users',
foreign_key: "user_id",
class_name: "Spree::Role"
has_many :spree_orders, foreign_key: "user_id", class_name: "Spree::Order"
belongs_to :ship_address, class_name: 'Spree::Address'
belongs_to :bill_address, class_name: 'Spree::Address'
# has_spree_role? simply needs to return true or false whether a user has a role or not.
def has_spree_role?(role_in_question)
spree_roles.where(name: role_in_question.to_s).any?
end
def last_incomplete_spree_order
spree_orders.incomplete.where(created_by_id: id).order('created_at DESC').first
end
end
end