Skip to content

Instantly share code, notes, and snippets.

@byped
Created June 30, 2010 17:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save byped/458951 to your computer and use it in GitHub Desktop.
Save byped/458951 to your computer and use it in GitHub Desktop.
#this method gets called on the before_save of transaction_item.
def apply_adjustments
applicable_adjustments = self.class.adjustments_to_apply[self.status.name.methodize.to_sym] || []
unless applicable_adjustments.empty?
pairing = if [:shipping_ready, :pending].include?(self.status.name.methodize.to_sym) && self.estimated_value
[:estimated_value, :estimated_total=]
elsif [:awaiting_confirmation, :accepted].include?(self.status.name.methodize.to_sym) &&
self.inspected_value
[:inspected_value, :payout_total=]
end
total_adjustment_value = 0
applicable_adjustments.each do |a|
total_adjustment_value += (a.apply_to(self) || 0)
end
unless pairing.nil?
total = ('%.2f' % (self.send(pairing[0]) + total_adjustment_value)).to_f
self.send(pairing[1], total)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment