Class: Caboose::LineItem

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

Instance Method Summary collapse

Instance Method Details

#as_json(options = {}) ⇒ Object



86
87
88
89
90
91
# File 'app/models/caboose/line_item.rb', line 86

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

#check_nil_fieldsObject



63
64
65
# File 'app/models/caboose/line_item.rb', line 63

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

#copyObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'app/models/caboose/line_item.rb', line 100

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



51
52
53
# File 'app/models/caboose/line_item.rb', line 51

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



78
79
80
81
82
83
84
# File 'app/models/caboose/line_item.rb', line 78

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



71
72
73
74
75
76
# File 'app/models/caboose/line_item.rb', line 71

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



93
94
95
96
97
98
# File 'app/models/caboose/line_item.rb', line 93

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