Sha256: 73991c0740f57120368a9491f2488b409369c196c35344a09a301c79cff00ac8

Contents?: true

Size: 984 Bytes

Versions: 6

Compression:

Stored size: 984 Bytes

Contents

class Adjustment < ActiveRecord::Base
  acts_as_list :scope => :order
  
  belongs_to :order
  belongs_to :adjustment_source, :polymorphic => true

  validates_presence_of :amount
  validates_presence_of :description
  validates_numericality_of :amount

  before_save do |record|
    new_amount = record.calculate_adjustment
    record.amount = new_amount if new_amount
    record.secondary_type ||= record.type
  end

  def calculate_adjustment
    if adjustment_source
      calc = adjustment_source.calculator || adjustment_source.default_calculator
      raise(RuntimeError, "#{self.class.name}##{id} doesn't have a calculator") unless calc
      calc.compute(adjustment_source)
    elsif read_attribute(:amount)
      read_attribute(:amount)
    else
      nil
    end
  end

  def amount
    read_attribute(:amount) || self.calculate_adjustment
  end
  
  def update_amount
    new_amount = calculate_adjustment
    update_attribute(:amount, new_amount) if new_amount
  end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
spree-enriquez-0.9.4 app/models/adjustment.rb
spree-0.9.4 app/models/adjustment.rb
spree-0.9.3 app/models/adjustment.rb
spree-0.9.2 app/models/adjustment.rb
spree-0.9.1 app/models/adjustment.rb
spree-0.9.0 app/models/adjustment.rb