Class: Product

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

Overview

attributes : see permitted_attributes + inventory + deleted_on

Instance Method Summary collapse

Instance Method Details

#adjust_costObject



74
75
76
77
78
# File 'app/models/product.rb', line 74

def adjust_cost
  if self.cost == 0.0
    self.cost = self.price / 2
  end
end

#check_attributesObject

if no url is set we generate one based on the name but product_items don’t have urls, so not for them



62
63
64
65
66
67
68
69
70
71
72
# File 'app/models/product.rb', line 62

def check_attributes
  if product_item?
    self.link = ""
  else
    self.link = self.name.gsub(" " , "_").downcase if self.link.blank? && self.name != nil
  end
  if line?
    self.ean = "" unless self.ean.blank?
    self.scode = "" unless self.scode.blank?
  end
end

#check_parent_eanObject



53
54
55
56
57
58
# File 'app/models/product.rb', line 53

def check_parent_ean
  parent = self.product
  return unless parent
  parent.check_attributes
  parent.save! if parent.changed? 
end

#deleteObject



84
85
86
87
88
89
# File 'app/models/product.rb', line 84

def delete
  self.deleted_on = Date.today
  self.link = ""
  products.each {|p| p.delete}
  self
end

#deleted?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'app/models/product.rb', line 80

def deleted?
  not deleted_on.blank?
end

#full_nameObject



114
115
116
117
118
119
120
# File 'app/models/product.rb', line 114

def full_name
  if product_item?
    product.name + " : " + self.name
  else
    self.name
  end
end

#line?Boolean

this product represents a product line (ie is not sellable in itself)

Returns:

  • (Boolean)


102
103
104
# File 'app/models/product.rb', line 102

def line?
  !product_item? and !products.empty?
end

#new_product_itemObject



122
123
124
125
# File 'app/models/product.rb', line 122

def new_product_item
  Product.new :tax => self.tax , :weight => self.weight , :cost => self.cost ,  :product_id => self.id , 
      :supplier_id => self.supplier_id , :category_id => self.category_id , :price => self.price
end

#product_item?Boolean

this product is an item of a product line (so is sellable)

Returns:

  • (Boolean)


106
107
108
# File 'app/models/product.rb', line 106

def product_item?
  self.product_id != nil
end

#sellable?Boolean

only products and product items are sellable. in other words if it’s not a line

Returns:

  • (Boolean)


110
111
112
# File 'app/models/product.rb', line 110

def sellable?
  !line?
end

#typeObject

the type is one of:

  • product

-product_line -product_item mostly used for translation. Function below let you test for each of the possibilities



96
97
98
99
# File 'app/models/product.rb', line 96

def type
  return :product_item if product_item?
  products.empty? ? :product : :product_line
end

#update_line_inventoryObject



45
46
47
48
49
50
51
# File 'app/models/product.rb', line 45

def update_line_inventory
  parent = self.product
  return unless parent
  inv = parent.inventory
  parent.inventory = parent.products.sum(:inventory) 
  parent.save! if inv != parent.inventory
end