Skip to content

Commit

Permalink
Remaining Opportunity functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
mattbeedle committed May 5, 2013
1 parent 5460063 commit f68d31b
Show file tree
Hide file tree
Showing 2 changed files with 440 additions and 1 deletion.
217 changes: 216 additions & 1 deletion lib/capsule_crm/opportunity.rb
Expand Up @@ -25,7 +25,7 @@ class Opportunity
validates :milestone_id, presence: { unless: :milestone }
validates :milestone, presence: { unless: :milestone_id }

# Public: Set the attributes of a person
# Public: Set the attributes of a opportunity
#
# attributes - The Hash of attributes (default: {}):
# :name - The String opportunity name
Expand Down Expand Up @@ -82,12 +82,227 @@ def self.all(options = {})
)
end

# Public: Find an opportunity by id
#
# id - The Integer ID
#
# Examples
#
# CapsuleCRM::Opportunity.find(id)
#
# Returns a CapsuleCRM::Opportunity
def self.find(id)
new CapsuleCRM::Connection.get("/api/opportunity/#{id}")['opportunity']
end

# Public: Create a new opportunity in capsulecrm
#
# attributes - The Hash of opportunity attributes (default: {}):
# :name - The String opportunity name
# :description - The String opportunity description
# :currency - The String currency code
# :value - The Float opportunity (financial) value
# :duration_basis - The String duration basis
# :duration - The Integer duration (for opportunities
# with a repeating (not FIXED) duratin basis
# :party_id - The Integer party id
# :milestone_id - The Integer milestone id
# :expected_close_date - The DateTime when the opportunity
# is expected to be closed
# :actual_close_date - The DateTime when the opportunity
# was actually closed
# :probability - The Float probability that this
# opportunity will be won
#
# Examples
#
# CapsuleCRM::opportunity.create(name: 'Test', milestone_id: 1)
#
# Returns a CapsuleCRM::opportunity
def self.create(attributes = {})
new(attributes).tap(&:save)
end

# Public: Create a new opportunity in capsulecrm and raise a
# CapsuleCRM::Errors::InvalidRecord error if not possible
#
# attributes - The Hash of opportunity attributes (default: {}):
# :name - The String opportunity name
# :description - The String opportunity description
# :currency - The String currency code
# :value - The Float opportunity (financial) value
# :duration_basis - The String duration basis
# :duration - The Integer duration (for opportunities
# with a repeating (not FIXED) duratin basis
# :party_id - The Integer party id
# :milestone_id - The Integer milestone id
# :expected_close_date - The DateTime when the opportunity
# is expected to be closed
# :actual_close_date - The DateTime when the opportunity
# was actually closed
# :probability - The Float probability that this
# opportunity will be won
#
# Examples
#
# CapsuleCRM::opportunity.create!(name: 'Test', milestone_id: 1)
#
# Returns a CapsuleCRM
def self.create!(attributes = {})
new(attributes).tap(&:save!)
end

# Public: If the opportunity already exists in capsule then update them,
# otherwise create a new opportunity
#
# Examples
#
# opportunity = CapsuleCRM::Opportunity.new(name: 'Test', milestone_id: 1)
# opportunity.save
#
# opportunity = CapsuleCRM::Opportunity.find(1)
# opportunity.name = 'Another Test'
# opportunity.save
#
# Returns a CapsuleCRM::opportunity
def save
if valid?
new_record? ? create_record : update_record
else
false
end
end

# Public: If the opportunity already exists in capsule then update them,
# otherwise create a new opportunity. If the opportunity is not valid then a
# CapsuleCRM::Errors::RecordInvalid exception is raised
#
# Examples
#
# opportunity = CapsuleCRM::Opportunity.new(name: 'Test, milestone_id: 1)
# opportunity.save
#
# opportunity = CapsuleCRM::Opportunity.find(1)
# opportunity.name = 'Another test'
# opportunity.save
#
# Returns a CapsuleCRM::opportunity
def save!
if valid?
new_record? ? create_record : update_record
else
raise CapsuleCRM::Errors::RecordInvalid.new(self)
end
end

# Public: Determine whether this CapsuleCRM::opportunity is a new record or not
#
# Returns a Boolean
def new_record?
!id
end

# Public: Determine whether or not this CapsuleCRM::opportunity has already been
# persisted to capsulecrm
#
# Returns a Boolean
def persisted?
!new_record?
end

# Public: Update the opportunity in capsule
#
# attributes - The Hash of opportunity attributes (default: {}):
# :name - The String opportunity name
# :description - The String opportunity description
# :currency - The String currency code
# :value - The Float opportunity (financial) value
# :duration_basis - The String duration basis
# :duration - The Integer duration (for opportunities
# with a repeating (not FIXED) duratin basis
# :party_id - The Integer party id
# :milestone_id - The Integer milestone id
# :expected_close_date - The DateTime when the opportunity
# is expected to be closed
# :actual_close_date - The DateTime when the opportunity
# was actually closed
# :probability - The Float probability that this
# opportunity will be won
# Examples
#
# opportunity = CapsuleCRM::Opportunity.find(1)
# opportunity.update_attributes name: 'A New Name'
#
# Returns a CapsuleCRM::opportunity
def update_attributes(attributes = {})
self.attributes = attributes
save
end

# Public: Update the opportunity in capsule. If the person is not valid then a
# CapsuleCRM::Errors::RecordInvalid exception will be raised
#
# attributes - The Hash of opportunity attributes (default: {}):
# :name - The String opportunity name
# :description - The String opportunity description
# :currency - The String currency code
# :value - The Float opportunity (financial) value
# :duration_basis - The String duration basis
# :duration - The Integer duration (for opportunities
# with a repeating (not FIXED) duratin basis
# :party_id - The Integer party id
# :milestone_id - The Integer milestone id
# :expected_close_date - The DateTime when the opportunity
# is expected to be closed
# :actual_close_date - The DateTime when the opportunity
# was actually closed
# :probability - The Float probability that this
# opportunity will be won
#
# Examples
#
# opportunity = CapsuleCRM::Opportunity.find(1)
# opportunity.update_attributes! name: 'A New Name'
# => CapsuleCRM::Opportunity
#
# opportunity.update_attributes! name: nil
# => CapsuleCRM::Errors::RecordInvalid
#
# Returns a CapsuleCRM::opportunity
def update_attributes!(attributes = {})
self.attributes = attributes
save!
end

# Public: Build a hash of attributes and camelize the keys for capsule
#
# Examples
#
# opportunity.to_capsule_json
#
# Returns a Hash
def to_capsule_json
{
opportunity: CapsuleCRM::HashHelper.camelize_keys(
attributes.dup.delete_if { |key, value| value.blank? }
)
}.stringify_keys
end

private

def create_record
self.attributes = CapsuleCRM::Connection.post(
'/api/opportunity', to_capsule_json
)
self
end

def update_record
CapsuleCRM::Connection.put("/api/opportunity/#{id}", attributes)
self
end

def self.init_collection(collection)
CapsuleCRM::ResultsProxy.new(collection.map { |item| new item })
end
Expand Down

0 comments on commit f68d31b

Please sign in to comment.