Class: Tienda::Product
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Tienda::Product
- Defined in:
- app/models/tienda/product.rb,
app/models/tienda/product/variants.rb,
app/models/tienda/product/product_attributes.rb
Instance Attribute Summary collapse
-
#product_attributes_array ⇒ Object
Used for setting an array of product attributes which will be updated.
Class Method Summary collapse
-
.import(file) ⇒ Object
Imports products from a spreadsheet file Example:.
- .open_spreadsheet(file) ⇒ Object
-
.with_attributes(key, values) ⇒ Enumerable
Search for products which include the given attributes and return an active record scope of these products.
Instance Method Summary collapse
-
#categories ⇒ Array
Return all product categories.
-
#default_variant ⇒ Tienda::Product
Returns the default variant for the product or nil if none exists.
-
#full_name ⇒ String
Return the name of the product.
-
#has_variants? ⇒ Boolean
Does this product have any variants?.
-
#in_stock? ⇒ Boolean
Is this product currently in stock?.
-
#orderable? ⇒ Boolean
Is this product orderable?.
-
#price ⇒ BigDecimal
The price for the product.
-
#product_category ⇒ Tienda::ProductCategory
The product’s category.
-
#tax_rate ⇒ Tienda::TaxRate
The product’s tax rate.
-
#variant? ⇒ Boolean
Is this product a variant of another?.
Instance Attribute Details
#product_attributes_array ⇒ Object
Used for setting an array of product attributes which will be updated. Usually received from a web browser.
9 10 11 |
# File 'app/models/tienda/product/product_attributes.rb', line 9 def product_attributes_array @product_attributes_array end |
Class Method Details
.import(file) ⇒ Object
Imports products from a spreadsheet file Example:
Tienda:Product.import("path/to/file.csv")
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'app/models/tienda/product.rb', line 131 def self.import(file) spreadsheet = open_spreadsheet(file) spreadsheet.default_sheet = spreadsheet.sheets.first header = spreadsheet.row(1) (2..spreadsheet.last_row).each do |i| row = Hash[[header, spreadsheet.row(i)].transpose] # Don't import products where the name is blank unless row["name"].nil? if product = find_by(name: row["name"]) # Dont import products with the same name but update quantities if they're not the same qty = row["qty"].to_i product.stock_level_adjustments.create!(description: I18n.t('tienda.import'), adjustment: qty) if qty > 0 && qty != product.stock else product = new(row) product.product_category_id = Tienda::ProductCategory.find_or_create_by(name: row['category_name']).id product.save! # Create quantities qty = row["qty"].to_i product.stock_level_adjustments.create!(description: I18n.t('tienda.import'), adjustment: qty) if qty > 0 end end end end |
.open_spreadsheet(file) ⇒ Object
158 159 160 161 162 163 164 165 |
# File 'app/models/tienda/product.rb', line 158 def self.open_spreadsheet(file) case File.extname(file.original_filename) when ".csv" then Roo::CSV.new(file.path) when ".xls" then Roo::Excel.new(file.path) when ".xlsx" then Roo::Excelx.new(file.path) else raise I18n.t('tienda.imports.errors.unknown_format', filename: File.original_filename) end end |
.with_attributes(key, values) ⇒ Enumerable
122 123 124 125 |
# File 'app/models/tienda/product.rb', line 122 def self.with_attributes(key, values) product_ids = Tienda::ProductAttribute.searchable.where(key: key, value: values).pluck(:product_id).uniq where(id: product_ids) end |
Instance Method Details
#categories ⇒ Array
Return all product categories
104 105 106 107 108 109 110 111 112 113 |
# File 'app/models/tienda/product.rb', line 104 def categories parent_category_id = product_category.id cats = [] while parent_category_id != nil c = Tienda::ProductCategory.find(parent_category_id) cats << c parent_category_id = c.parent_id end cats end |
#default_variant ⇒ Tienda::Product
Returns the default variant for the product or nil if none exists.
38 39 40 41 |
# File 'app/models/tienda/product/variants.rb', line 38 def default_variant return nil if self.parent @default_variant ||= self.variants.select { |v| v.default? }.first end |
#full_name ⇒ String
Return the name of the product
74 75 76 |
# File 'app/models/tienda/product.rb', line 74 def full_name self.parent ? "#{self.parent.name} (#{name})" : name end |
#has_variants? ⇒ Boolean
Does this product have any variants?
31 32 33 |
# File 'app/models/tienda/product/variants.rb', line 31 def has_variants? !variants.empty? end |
#in_stock? ⇒ Boolean
Is this product currently in stock?
97 98 99 |
# File 'app/models/tienda/product.rb', line 97 def in_stock? self.default_variant ? self.default_variant.in_stock? : (stock_control? ? stock_count > 0 : true) end |
#orderable? ⇒ Boolean
Is this product orderable?
81 82 83 84 85 |
# File 'app/models/tienda/product.rb', line 81 def orderable? return false unless self.active? return false if self.has_variants? true end |
#price ⇒ BigDecimal
The price for the product
90 91 92 |
# File 'app/models/tienda/product.rb', line 90 def price self.default_variant ? self.default_variant.price : read_attribute(:price) end |
#product_category ⇒ Tienda::ProductCategory
The product’s category
17 |
# File 'app/models/tienda/product.rb', line 17 belongs_to :product_category |
#tax_rate ⇒ Tienda::TaxRate
The product’s tax rate
22 |
# File 'app/models/tienda/product.rb', line 22 belongs_to :tax_rate |
#variant? ⇒ Boolean
Is this product a variant of another?
46 47 48 |
# File 'app/models/tienda/product/variants.rb', line 46 def variant? !self.parent_id.blank? end |