Class: OpportunityObserver
- Inherits:
-
ActiveRecord::Observer
- Object
- ActiveRecord::Observer
- OpportunityObserver
- Defined in:
- app/models/observers/opportunity_observer.rb
Overview
Copyright © 2008-2013 Michael Dvorkin and contributors.
Fat Free CRM is freely distributable under the terms of MIT license. See MIT-LICENSE file or www.opensource.org/licenses/mit-license.php
Constant Summary collapse
- @@opportunities =
{}
Instance Method Summary collapse
Instance Method Details
#after_create(item) ⇒ Object
13 14 15 |
# File 'app/models/observers/opportunity_observer.rb', line 13 def after_create(item) update_campaign_revenue(item.campaign, item.amount.to_f - item.discount.to_f) if item.campaign && item.stage == "won" end |
#after_update(item) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'app/models/observers/opportunity_observer.rb', line 21 def after_update(item) original = @@opportunities.delete(item.id) if original if original.stage != "won" && item.stage == "won" # :other to :won -- add to total campaign revenue. update_campaign_revenue(item.campaign, item.amount.to_f - item.discount.to_f) item.update_attribute(:probability, 100) # Set probability to 100% if won log_activity(item, :won) elsif original.stage == "won" && item.stage != "won" # :won to :other -- substract from total campaign revenue. update_campaign_revenue(original.campaign, -(original.amount.to_f - original.discount.to_f)) elsif original.stage != "lost" && item.stage == "lost" item.update_attribute(:probability, 0) # Set probability to 0% if lost end end end |
#before_update(item) ⇒ Object
17 18 19 |
# File 'app/models/observers/opportunity_observer.rb', line 17 def before_update(item) @@opportunities[item.id] = Opportunity.find(item.id).freeze end |