Sha256: d3953be4debfd5a8497b1434a147fa903134b905d362aed3d85c89aebd527974

Contents?: true

Size: 1.37 KB

Versions: 5

Compression:

Stored size: 1.37 KB

Contents

# frozen_string_literal: true

# Copyright (c) 2008-2013 Michael Dvorkin and contributors.
#
# Fat Free CRM is freely distributable under the terms of MIT license.
# See MIT-LICENSE file or http://www.opensource.org/licenses/mit-license.php
#------------------------------------------------------------------------------
class EntityObserver < ActiveRecord::Observer
  observe :account, :contact, :lead, :opportunity

  def after_create(item)
    send_notification_to_assignee(item) if current_user != item.assignee
  end

  def after_update(item)
    send_notification_to_assignee(item) if item.saved_change_to_assigned_to? && item.assignee != current_user
  end

  private

  def send_notification_to_assignee(item)
    UserMailer.assigned_entity_notification(item, current_user).deliver_now if item.assignee.present? && current_user.present? && can_send_email?
  end

  # Need to have a host set before email can be sent
  def can_send_email?
    Setting.host.present?
  end

  def current_user
    # this deals with whodunnit inconsistencies, where in some cases it's set to a user's id and others the user object itself
    user_id_or_user = PaperTrail.request.whodunnit
    if user_id_or_user.is_a?(User)
      user_id_or_user
    elsif user_id_or_user.is_a?(String)
      User.find_by_id(user_id_or_user.to_i)
    end
  end

  ActiveSupport.run_load_hooks(:fat_free_crm_entity_observer, self)
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
fat_free_crm-0.21.0 app/models/observers/entity_observer.rb
fat_free_crm-0.20.1 app/models/observers/entity_observer.rb
fat_free_crm-0.20.0 app/models/observers/entity_observer.rb
fat_free_crm-0.19.2 app/models/observers/entity_observer.rb
fat_free_crm-0.19.0 app/models/observers/entity_observer.rb