Class: Caboose::LineItem

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/caboose/line_item.rb

Constant Summary collapse

STATUS_PENDING =
'pending'
STATUS_BACKORDERED =
'backordered'
STATUS_CANCELED =
'canceled'
STATUS_PROCESSED =
'processed'

Instance Method Summary collapse

Instance Method Details

#as_json(options = {}) ⇒ Object



83
84
85
86
87
88
# File 'app/models/caboose/line_item.rb', line 83

def as_json(options={})
  self.attributes.merge({        
    :variant => self.variant,
    :title   => self.title
  })
end

#check_nil_fieldsObject



60
61
62
# File 'app/models/caboose/line_item.rb', line 60

def check_nil_fields      
  self.subtotal = 0.00 if self.subtotal.nil?        
end

#copyObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'app/models/caboose/line_item.rb', line 97

def copy
  LineItem.new(      
    :variant_id      => self.variant_id      ,
    :quantity        => self.quantity        ,
    :unit_price      => self.unit_price      ,
    :subtotal        => self.subtotal        ,
    :notes           => self.notes           ,
    :invoice_id        => self.invoice_id        ,
    :status          => self.status          ,        
    :custom1         => self.custom1         ,
    :custom2         => self.custom2         ,
    :custom3         => self.custom3
  )
end

#quantity_in_stockObject

validates :quantity, :numericality => { :greater_than_or_equal_to => 0 }

validate :quantity_in_stock



48
49
50
# File 'app/models/caboose/line_item.rb', line 48

def quantity_in_stock
  errors.add(:base, "There #{self.variant.quantity_in_stock > 1 ? 'are' : 'is'} only #{self.variant.quantity_in_stock} left in stock.") if self.variant.quantity_in_stock - self.quantity < 0
end

#titleObject



75
76
77
78
79
80
81
# File 'app/models/caboose/line_item.rb', line 75

def title
  if self.variant.product.variants.count > 1
    "#{self.variant.product.title} - #{self.variant.title}"
  else
    self.variant.product.title
  end
end

#update_subtotalObject

Methods



68
69
70
71
72
73
# File 'app/models/caboose/line_item.rb', line 68

def update_subtotal
  if self.unit_price.nil?
    self.unit_price = self.variant.on_sale? ? self.variant.sale_price : self.variant.price        
  end      
  self.subtotal = self.unit_price * self.quantity
end

#verify_unit_priceObject



90
91
92
93
94
95
# File 'app/models/caboose/line_item.rb', line 90

def verify_unit_price      
  if self.unit_price.nil?
    self.unit_price = self.variant.clearance && self.variant.clearance_price ? self.variant.clearance_price : (self.variant.on_sale? ? self.variant.sale_price : self.variant.price)
    self.save
  end      
end