Class: Shoppe::Product

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/shoppe/product.rb,
app/models/shoppe/product/variants.rb,
app/models/shoppe/product/product_attributes.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#product_attributes_arrayObject

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

.ransackable_associations(auth_object = nil) ⇒ Object



98
99
100
# File 'app/models/shoppe/product.rb', line 98

def self.ransackable_associations(auth_object = nil)
  []
end

.ransackable_attributes(auth_object = nil) ⇒ Object



94
95
96
# File 'app/models/shoppe/product.rb', line 94

def self.ransackable_attributes(auth_object = nil)
  ["id", "name", "sku"] + _ransackers.keys
end

.with_attributes(key, values) ⇒ Enumerable

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'])

Returns:

  • (Enumerable)


109
110
111
112
# File 'app/models/shoppe/product.rb', line 109

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_variantShoppe::Product

Returns the default variant for the product or nil if none exists.

Returns:



26
27
28
29
# File 'app/models/shoppe/product/variants.rb', line 26

def default_variant
  return nil if self.parent
  @default_variant ||= self.variants.select { |v| v.default? }.first
end

#full_nameString

Return the name of the product

Returns:

  • (String)


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?

Returns:

  • (Boolean)


19
20
21
# File 'app/models/shoppe/product/variants.rb', line 19

def has_variants?
  !variants.empty?
end

#in_stock?Boolean

Is this product currently in stock?

Returns:

  • (Boolean)


83
84
85
# File 'app/models/shoppe/product.rb', line 83

def in_stock?
  self.default_variant ? self.default_variant.in_stock? : stock > 0
end

#orderable?Boolean

Is this product orderable?

Returns:

  • (Boolean)


68
69
70
71
# File 'app/models/shoppe/product.rb', line 68

def orderable?
  return false if self.has_variants?
  true
end

#priceBigDecimal

The price for the product

Returns:

  • (BigDecimal)


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

def price
  self.default_variant ? self.default_variant.price : read_attribute(:price)
end

#product_categoryShoppe::ProductCategory

The product’s category



17
# File 'app/models/shoppe/product.rb', line 17

belongs_to :product_category, :class_name => 'Shoppe::ProductCategory'

#stockFixnum

Return the total number of items currently in stock

Returns:

  • (Fixnum)


90
91
92
# File 'app/models/shoppe/product.rb', line 90

def stock
  @stock ||= self.stock_level_adjustments.sum(:adjustment)
end

#tax_rateShoppe::TaxRate

The product’s tax rate

Returns:



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?

Returns:

  • (Boolean)


34
35
36
# File 'app/models/shoppe/product/variants.rb', line 34

def variant?
  !self.parent_id.blank?
end