Class: ActivityObserver

Inherits:
ActiveRecord::Observer
  • Object
show all
Defined in:
app/models/observers/activity_observer.rb

Overview

Fat Free CRM Copyright © 2008-2011 by Michael Dvorkin

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License along with this program. If not, see <www.gnu.org/licenses/>.


Constant Summary collapse

@@tasks =
{}
@@leads =
{}
@@opportunities =
{}

Instance Method Summary collapse

Instance Method Details

#after_create(subject) ⇒ Object



25
26
27
28
29
30
# File 'app/models/observers/activity_observer.rb', line 25

def after_create(subject)
  log_activity(subject, :created)
  if subject.is_a?(Opportunity) && subject.campaign && subject.stage == "won"
    update_campaign_revenue(subject.campaign, (subject.amount || 0) - (subject.discount || 0))
  end
end

#after_update(subject) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/models/observers/activity_observer.rb', line 42

def after_update(subject)
  if subject.is_a?(Task)
    original = @@tasks.delete(subject.id)
    if original
      return log_activity(subject, :completed)   if subject.completed_at && original.completed_at.nil?
      return log_activity(subject, :reassigned)  if subject.assigned_to != original.assigned_to
      return log_activity(subject, :rescheduled) if subject.bucket != original.bucket
    end
  elsif subject.is_a?(Lead)
    original = @@leads.delete(subject.id)
    if original && original.status != "rejected" && subject.status == "rejected"
      return log_activity(subject, :rejected)
    end
  elsif subject.is_a?(Opportunity)
    original = @@opportunities.delete(subject.id)
    if original
      if original.stage != "won" && subject.stage == "won"    # :other to :won -- add to total campaign revenue.
        update_campaign_revenue(subject.campaign, (subject.amount || 0) - (subject.discount || 0))
        return log_activity(subject, :won)
      elsif original.stage == "won" && subject.stage != "won" # :won to :other -- substract from total campaign revenue.
        update_campaign_revenue(original.campaign, -((original.amount || 0) - (original.discount || 0)))
      end
    end
  end
  log_activity(subject, :updated)
end

#before_destroy(subject) ⇒ Object



69
70
71
# File 'app/models/observers/activity_observer.rb', line 69

def before_destroy(subject)
  log_activity(subject, :deleted)
end

#before_update(subject) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'app/models/observers/activity_observer.rb', line 32

def before_update(subject)
  if subject.is_a?(Task)
    @@tasks[subject.id] = Task.find(subject.id).freeze
  elsif subject.is_a?(Lead)
    @@leads[subject.id] = Lead.find(subject.id).freeze
  elsif subject.is_a?(Opportunity)
    @@opportunities[subject.id] = Opportunity.find(subject.id).freeze
  end
end