Class: Shoppe::Product
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Shoppe::Product
- Defined in:
- app/models/shoppe/product.rb,
app/models/shoppe/product/variants.rb,
app/models/shoppe/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
-
.with_attributes(key, values) ⇒ Enumerable
Search for products which include the guven attributes and return an active record scope of these products.
Instance Method Summary collapse
-
#default_variant ⇒ Shoppe::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 ⇒ Shoppe::ProductCategory
The product’s category.
-
#stock ⇒ Fixnum
Return the total number of items currently in stock.
-
#tax_rate ⇒ Shoppe::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/shoppe/product/product_attributes.rb', line 9 def product_attributes_array @product_attributes_array end |
Class Method Details
.with_attributes(key, values) ⇒ Enumerable
102 103 104 105 |
# File 'app/models/shoppe/product.rb', line 102 def self.with_attributes(key, values) product_ids = Shoppe::ProductAttribute.searchable.where(:key => key, :value => values).pluck(:product_id).uniq where(:id => product_ids) end |
Instance Method Details
#default_variant ⇒ Shoppe::Product
Returns the default variant for the product or nil if none exists.
38 39 40 41 |
# File 'app/models/shoppe/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
61 62 63 |
# File 'app/models/shoppe/product.rb', line 61 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/shoppe/product/variants.rb', line 31 def has_variants? !variants.empty? end |
#in_stock? ⇒ Boolean
Is this product currently in stock?
84 85 86 |
# File 'app/models/shoppe/product.rb', line 84 def in_stock? self.default_variant ? self.default_variant.in_stock? : (stock_control? ? stock > 0 : true) end |
#orderable? ⇒ Boolean
Is this product orderable?
68 69 70 71 72 |
# File 'app/models/shoppe/product.rb', line 68 def orderable? return false unless self.active? return false if self.has_variants? true end |
#price ⇒ BigDecimal
The price for the product
77 78 79 |
# File 'app/models/shoppe/product.rb', line 77 def price self.default_variant ? self.default_variant.price : read_attribute(:price) end |
#product_category ⇒ Shoppe::ProductCategory
The product’s category
17 |
# File 'app/models/shoppe/product.rb', line 17 belongs_to :product_category, :class_name => 'Shoppe::ProductCategory' |
#stock ⇒ Fixnum
Return the total number of items currently in stock
91 92 93 |
# File 'app/models/shoppe/product.rb', line 91 def stock self.stock_level_adjustments.sum(:adjustment) end |
#tax_rate ⇒ Shoppe::TaxRate
The product’s tax rate
22 |
# File 'app/models/shoppe/product.rb', line 22 belongs_to :tax_rate, :class_name => "Shoppe::TaxRate" |
#variant? ⇒ Boolean
Is this product a variant of another?
46 47 48 |
# File 'app/models/shoppe/product/variants.rb', line 46 def variant? !self.parent_id.blank? end |