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

#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



65
66
67
68
69
70
71
72
73
74
75
# File 'app/models/product.rb', line 65

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



56
57
58
59
60
61
# File 'app/models/product.rb', line 56

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

#deleteObject

deleting sets the deleted_on flag to today Products can not be deleted because of the risk of invalidating old orders to actually remove a product from the db, use the console



88
89
90
91
92
93
# File 'app/models/product.rb', line 88

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

#deleted?Boolean

has the product been deleted (marked as deleted)

Returns:

  • (Boolean)


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

def deleted?
  not deleted_on.blank?
end

#full_nameObject

full name, or display name, is the name for a product, and the concatination of the parents name and the name, for product items



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

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

#has_inventory?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'app/models/product.rb', line 77

def has_inventory?
  self.inventory > 0
end

#line?Boolean

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

Returns:

  • (Boolean)


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

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

#new_product_itemObject



130
131
132
133
# File 'app/models/product.rb', line 130

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)


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

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)


116
117
118
# File 'app/models/product.rb', line 116

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



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

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

#update_line_inventoryObject



48
49
50
51
52
53
54
# File 'app/models/product.rb', line 48

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