app/models/users/user.rb in reduced_fat_crm-0.14.0 vs app/models/users/user.rb in reduced_fat_crm-0.15.0.beta
- old
+ new
@@ -37,13 +37,10 @@
# suspended_at :datetime
# single_access_token :string(255)
#
class User < ActiveRecord::Base
- before_create :check_if_needs_approval
- before_destroy :check_if_current_user, :check_if_has_related_assets
-
has_one :avatar, as: :entity, dependent: :destroy # Personal avatar.
has_many :avatars # As owner who uploaded it, ex. Contact avatar.
has_many :comments, as: :commentable # As owner who created a comment.
has_many :accounts
has_many :campaigns
@@ -141,11 +138,13 @@
def to_xml(_options = nil)
[name].to_xml
end
- private
+ def destroyable?
+ check_if_current_user && !has_related_assets?
+ end
# Suspend newly created user if signup requires an approval.
#----------------------------------------------------------------------------
def check_if_needs_approval
self.suspended_at = Time.now if Setting.user_signup == :needs_approval && !admin
@@ -157,17 +156,19 @@
User.current_user.nil? || User.current_user != self
end
# Prevent deleting a user unless she has no artifacts left.
#----------------------------------------------------------------------------
- def check_if_has_related_assets
- artifacts = %w(Account Campaign Lead Contact Opportunity Comment Task).inject(0) do |sum, asset|
+ def has_related_assets?
+ sum = %w(Account Campaign Lead Contact Opportunity Comment Task).detect do |asset|
klass = asset.constantize
- sum += klass.assigned_to(self).count if asset != "Comment"
- sum += klass.created_by(self).count
+
+ asset != "Comment" && klass.assigned_to(self).exists? || klass.created_by(self).exists?
end
- artifacts == 0
+ !sum.nil?
end
+
+ private
# Define class methods
#----------------------------------------------------------------------------
class << self
def current_ability