Sha256: 742d36842784e893faa288528d458009eea827d586b45d0c138b3680da7f9698

Contents?: true

Size: 1.86 KB

Versions: 16

Compression:

Stored size: 1.86 KB

Contents

module CabooseStore
  class LineItem < ActiveRecord::Base
    self.table_name = 'store_line_items'
    
    belongs_to :variant
    belongs_to :order, :dependent => :destroy
    belongs_to :parent, :class_name => 'LineItem', :foreign_key => 'parent_id'
    has_many :children, :class_name => 'LineItem', :foreign_key => 'parent_id'
    
    attr_accessible :id,
      :variant_id,
      :quantity,
      :price,
      :notes,
      :order_id,
      :status,
      :tracking_number,
      :custom1,
      :custom2,
      :custom3
    
    #
    # Scopes
    #
    
    scope :pending, where('status = ?', 'pending')
    scope :fulfilled, where('status = ?', 'shipped')
    scope :unfulfilled, where('status != ?', 'shipped')
    #
    # Validations
    #
    
    validates :status, :inclusion => {
      :in      => ['pending', 'shipped'],
      :message => "%{value} is not a valid status. Must be either 'pending' or 'shipped'"
    }
    
    validates :quantity, :numericality => { :greater_than_or_equal_to => 0 }
    
    validate :quantity_in_stock
    def quantity_in_stock
      errors.add(:base, "There #{self.variant.quantity > 1 ? 'are' : 'is'} only #{self.variant.quantity} left in stock.") if self.variant.quantity - self.quantity < 0
    end
    
    #
    # Callbacks
    #
    
    before_save :update_price
    after_save { self.order.calculate }
    
    #
    # Methods
    #
    
    def update_price
      self.price = self.variant.price * self.quantity
    end
    
    def title
      if self.variant.product.variants.count > 1
        "#{self.variant.product.title} - #{self.variant.title}"
      else
        self.variant.product.title
      end
    end
    
    def as_json(options={})
      self.attributes.merge({
        :variant => self.variant,
        :title => self.title,
        :product => { :images => self.variant.product.product_images }
      })
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
caboose-store-0.0.26 app/models/caboose_store/line_item.rb
caboose-store-0.0.25 app/models/caboose_store/line_item.rb
caboose-store-0.0.24 app/models/caboose_store/line_item.rb
caboose-store-0.0.23 app/models/caboose_store/line_item.rb
caboose-store-0.0.22 app/models/caboose_store/line_item.rb
caboose-store-0.0.21 app/models/caboose_store/line_item.rb
caboose-store-0.0.20 app/models/caboose_store/line_item.rb
caboose-store-0.0.19 app/models/caboose_store/line_item.rb
caboose-store-0.0.18 app/models/caboose_store/line_item.rb
caboose-store-0.0.17 app/models/caboose_store/line_item.rb
caboose-store-0.0.16 app/models/caboose_store/line_item.rb
caboose-store-0.0.15 app/models/caboose_store/line_item.rb
caboose-store-0.0.14 app/models/caboose_store/line_item.rb
caboose-store-0.0.13 app/models/caboose_store/line_item.rb
caboose-store-0.0.12 app/models/caboose_store/line_item.rb
caboose-store-0.0.11 app/models/caboose_store/line_item.rb