Class: Product
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Product
- Defined in:
- app/models/product.rb
Overview
attributes : see permitted_attributes + inventory + deleted_on
Instance Method Summary collapse
-
#check_attributes ⇒ Object
if no url is set we generate one based on the name but product_items don’t have urls, so not for them.
- #check_parent_ean ⇒ Object
-
#delete ⇒ Object
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.
-
#deleted? ⇒ Boolean
has the product been deleted (marked as deleted).
-
#full_name ⇒ Object
full name, or display name, is the name for a product, and the concatination of the parents name and the name, for product items.
- #has_inventory? ⇒ Boolean
-
#line? ⇒ Boolean
this product represents a product line (ie is not sellable in itself).
- #new_product_item ⇒ Object
-
#product_item? ⇒ Boolean
this product is an item of a product line (so is sellable).
-
#sellable? ⇒ Boolean
only products and product items are sellable.
-
#type ⇒ Object
the type is one of: - product -product_line -product_item mostly used for translation.
- #update_line_inventory ⇒ Object
Instance Method Details
#check_attributes ⇒ Object
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_ean ⇒ Object
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 |
#delete ⇒ Object
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)
81 82 83 |
# File 'app/models/product.rb', line 81 def deleted? not deleted_on.blank? end |
#full_name ⇒ Object
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
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)
106 107 108 |
# File 'app/models/product.rb', line 106 def line? !product_item? and !products.empty? end |
#new_product_item ⇒ Object
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)
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
116 117 118 |
# File 'app/models/product.rb', line 116 def sellable? !line? end |
#type ⇒ Object
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_inventory ⇒ Object
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 |