Sha256: 07980f7ab59768a20cabbc283c89d1ad657f82bc49e0a8ed44bb21066ae47776

Contents?: true

Size: 1.05 KB

Versions: 6

Compression:

Stored size: 1.05 KB

Contents

module Piggybak
  class LineItem < ActiveRecord::Base
    belongs_to :order
    belongs_to :variant
  
    validates_presence_of :variant_id
    validates_presence_of :total
    validates_presence_of :quantity
    validates_numericality_of :quantity, :only_integer => true, :greater_than_or_equal_to => 0

    after_create :decrease_inventory
    after_destroy :increase_inventory
    after_update :update_inventory
        
    def admin_label
      "#{self.quantity} x #{self.variant.description}"
    end

    def decrease_inventory
      self.variant.update_inventory(-1 * self.quantity)
    end

    def increase_inventory
      self.variant.update_inventory(self.quantity)
    end

    def update_inventory
      if self.variant_id != self.variant_id_was
        old_variant = Variant.find(self.variant_id_was)
        old_variant.update_inventory(self.quantity_was)
        self.variant.update_inventory(-1*self.quantity)
      else
        quantity_diff = self.quantity_was - self.quantity
        self.variant.update_inventory(quantity_diff)
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
piggybak-0.4.0 app/models/piggybak/line_item.rb
piggybak-0.3.2 app/models/piggybak/line_item.rb
piggybak-0.3.1 app/models/piggybak/line_item.rb
piggybak-0.3.0 app/models/piggybak/line_item.rb
piggybak-0.2.1 app/models/piggybak/line_item.rb
piggybak-0.2.0 app/models/piggybak/line_item.rb