Class: Opportunity
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Opportunity
- Defined in:
- app/models/entities/opportunity.rb
Overview
Schema Information
Table name: opportunities
id :integer not null, primary key
user_id :integer
campaign_id :integer
assigned_to :integer
name :string(64) default(""), not null
access :string(8) default("Public")
source :string(32)
stage :string(32)
probability :integer
amount :decimal(12, 2)
discount :decimal(12, 2)
closes_on :date
deleted_at :datetime
created_at :datetime
updated_at :datetime
background_info :string(255)
Class Method Summary collapse
-
.create_for(model, account, params, users) ⇒ Object
Class methods.
- .outline ⇒ Object
-
.per_page ⇒ Object
Default values provided through class methods.
Instance Method Summary collapse
-
#attach!(attachment) ⇒ Object
Attach given attachment to the opportunity if it hasn’t been attached already.
-
#discard!(attachment) ⇒ Object
Discard given attachment from the opportunity.
-
#save_with_account_and_permissions(params) ⇒ Object
Backend handler for [Create New Opportunity] form (see opportunity/create).
-
#update_with_account_and_permissions(params) ⇒ Object
Backend handler for [Update Opportunity] form (see opportunity/update).
-
#weighted_amount ⇒ Object
—————————————————————————-.
Class Method Details
.create_for(model, account, params, users) ⇒ Object
Class methods.
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'app/models/entities/opportunity.rb', line 151 def self.create_for(model, account, params, users) opportunity = Opportunity.new(params) # Save the opportunity if its name was specified and account has no errors. if opportunity.name? && account.errors.empty? # Note: opportunity.account = account doesn't seem to work here. opportunity.account_opportunity = AccountOpportunity.new(:account => account, :opportunity => opportunity) unless account.id.blank? if opportunity.access != "Lead" || model.nil? opportunity.(users) else opportunity.(model) end end opportunity end |
.outline ⇒ Object
96 |
# File 'app/models/entities/opportunity.rb', line 96 def self.outline ; "long" ; end |
.per_page ⇒ Object
Default values provided through class methods.
95 |
# File 'app/models/entities/opportunity.rb', line 95 def self.per_page ; 20 ; end |
Instance Method Details
#attach!(attachment) ⇒ Object
Attach given attachment to the opportunity if it hasn’t been attached already.
133 134 135 136 137 |
# File 'app/models/entities/opportunity.rb', line 133 def attach!() unless self.send("#{.class.name.downcase}_ids").include?(.id) self.send(.class.name.tableize) << end end |
#discard!(attachment) ⇒ Object
Discard given attachment from the opportunity.
141 142 143 144 145 146 147 |
# File 'app/models/entities/opportunity.rb', line 141 def discard!() if .is_a?(Task) .update_attribute(:asset, nil) else # Contacts self.send(.class.name.tableize).delete() end end |
#save_with_account_and_permissions(params) ⇒ Object
Backend handler for [Create New Opportunity] form (see opportunity/create).
105 106 107 108 109 110 111 112 113 114 |
# File 'app/models/entities/opportunity.rb', line 105 def (params) # Quick sanitization, makes sure Account will not search for blank id. params[:account].delete(:id) if params[:account][:id].blank? account = Account.create_or_select_for(self, params[:account], params[:users]) self.account_opportunity = AccountOpportunity.new(:account => account, :opportunity => self) unless account.id.blank? self.account = account self.contacts << Contact.find(params[:contact]) unless params[:contact].blank? self.campaign = Campaign.find(params[:campaign]) unless params[:campaign].blank? self.(params[:users]) end |
#update_with_account_and_permissions(params) ⇒ Object
Backend handler for [Update Opportunity] form (see opportunity/update).
118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'app/models/entities/opportunity.rb', line 118 def (params) if params[:account] && (params[:account][:id] == "" || params[:account][:name] == "") self.account = nil # Opportunity is not associated with the account anymore. elsif params[:account] account = Account.create_or_select_for(self, params[:account], params[:users]) if self.account != account and account.id.present? self.account_opportunity = AccountOpportunity.new(:account => account, :opportunity => self) end end self.reload self.(params[:opportunity], params[:users]) end |
#weighted_amount ⇒ Object
99 100 101 |
# File 'app/models/entities/opportunity.rb', line 99 def weighted_amount ((amount || 0) - (discount || 0)) * (probability || 0) / 100.0 end |