Skip to content

Instantly share code, notes, and snippets.

@svs
Created December 30, 2012 15:11
Show Gist options
  • Save svs/4413218 to your computer and use it in GitHub Desktop.
Save svs/4413218 to your computer and use it in GitHub Desktop.
Before extracting workflow object
class Quotation
include DataMapper::Resource
# ...snip...
property :quoted_kms, Float
property :quoted_per_km, Float
property :quoted_price, Float
property :start_date, Date, :required => true
property :end_date, Date, :required => true
# ...snip...
def unconfirmed?
[:mildly_interested, :negotiating].include?(quality)
end
def expirable?
true if self.start_date <= Date.today
end
def sendable?
(approved? || sent?) && !intended_trip.user.email.blank? && !expirable?
end
def approvable?
set_end_date
(start_date && end_date) && !sent? && !unconfirmed? && !approved? && !expirable?
end
#... loads of small functions that check whats possible to do with the Quotation in its current state
workflow do
state :created do
event :approve, :transitions_to => :approved, :if => Proc.new{|t| t.approvable? }
event :reject, :transitions_to => :rejected, :if => Proc.new{|t| t.rejectable? }
event :hold, :transitions_to => :on_hold, :if => Proc.new{|t| t.on_holdable? }
event :expire, :transitions_to => :expired, :if => Proc.new{|t| t.expirable? }
event :callback, :transitions_to => :callback, :if => Proc.new{|t| t.callbackable? }
end
state :approved do
event :send_quotation, :transitions_to => :sent, :if => Proc.new{|t| t.sendable? }
event :reject, :transitions_to => :rejected, :if => Proc.new{|t| t.rejectable? }
event :hold, :transitions_to => :on_hold, :if => Proc.new{|t| t.on_holdable? }
event :pay, :transitions_to => :paid, :if => Proc.new{|t| t.payable? }
event :expire, :transitions_to => :expired, :if => Proc.new{|t| t.expirable? }
end
#... snip ...
end
# ..snip ..
end
def send_quotation(doer, params = {})
valid?
UserMailer.quotation(self).deliver
self.sent_at = DateTime.now
Transaction.all(:amount.lte => 0, :quotation => self).destroy
t = Transaction.create(:amount => -quoted_price, :date => Date.today, :user => self.intended_trip.user, :created_by => doer, :comment => "#{doer.email} sent quotation", :quotation => self)
end
def pay(doer, params = {})
t = Transaction.create(:amount => quoted_price, :date => Date.today, :user => self.intended_trip.user, :created_by => doer, :comment => "#{doer.email} marked as 'Paid'", :quotation => self)
end
# some more actions to be done on state transition
# loads of other methods to do with the Quotation proper
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment