Class: Shoppe::Product
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Shoppe::Product
- Defined in:
- app/models/shoppe/product.rb,
app/models/shoppe/product/product_attributes.rb
Instance Attribute Summary collapse
-
#product_attributes_array ⇒ Object
Attribute for providing the hash.
Class Method Summary collapse
-
.ransackable_associations(auth_object = nil) ⇒ Object
Specify which associations can be searched.
-
.ransackable_attributes(auth_object = nil) ⇒ Object
Specify which attributes can be searched.
-
.with_attributes(key, values) ⇒ Object
Search for products which include the guven attributes and return an active record scope of these products.
Instance Method Summary collapse
-
#in_stock? ⇒ Boolean
Is this product currently in stock?.
-
#update_stock_level(purchased = 1) ⇒ Object
Remove the provided number of units from the current stock level of this product.
Instance Attribute Details
#product_attributes_array ⇒ Object
Attribute for providing the hash
7 8 9 |
# File 'app/models/shoppe/product/product_attributes.rb', line 7 def product_attributes_array @product_attributes_array end |
Class Method Details
.ransackable_associations(auth_object = nil) ⇒ Object
Specify which associations can be searched
57 58 59 |
# File 'app/models/shoppe/product.rb', line 57 def self.ransackable_associations(auth_object = nil) [] end |
.ransackable_attributes(auth_object = nil) ⇒ Object
Specify which attributes can be searched
52 53 54 |
# File 'app/models/shoppe/product.rb', line 52 def self.ransackable_attributes(auth_object = nil) ["id", "title", "sku"] + _ransackers.keys end |
.with_attributes(key, values) ⇒ Object
Search for products which include the guven attributes and return an active record scope of these products. Chainable with other scopes and with_attributes methods. For example:
Shoppe::Product.active.with_attribute('Manufacturer', 'Apple').with_attribute('Model', ['Macbook', 'iPhone'])
66 67 68 69 |
# File 'app/models/shoppe/product.rb', line 66 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
#in_stock? ⇒ Boolean
Is this product currently in stock?
39 40 41 |
# File 'app/models/shoppe/product.rb', line 39 def in_stock? !stock_control? || stock > 0 end |
#update_stock_level(purchased = 1) ⇒ Object
Remove the provided number of units from the current stock level of this product
44 45 46 47 48 49 |
# File 'app/models/shoppe/product.rb', line 44 def update_stock_level(purchased = 1) if self.stock_control? self.stock -= purchased self.save! end end |