Class: Spree::Piwik::LineItem

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model
Defined in:
app/models/spree/piwik/line_item.rb

Constant Summary collapse

ATTRIBUTES =
[:sku, :name, :price, :quantity, :categories]

Class Method Summary collapse

Class Method Details

.from_product_ish(product_ish) ⇒ Object

Create a LineItem from a Spree::LinteItem-ish thing (line_items, products, variants)

All attributes in ATTRIBUTES will be mapped:

When we add a (private) method with the name map_product_ish_sku, this method will be called and get the product_ish to generate a value for that attribute.

When the product_ish has the same attribute name and no mapping-method provided the value, it will simply use the value from that attribute on product_ish



22
23
24
25
# File 'app/models/spree/piwik/line_item.rb', line 22

def self.from_product_ish(product_ish)
  values = ATTRIBUTES.map { |a| [a, product_ish_attr(a, product_ish)] }
  new(Hash[values])
end

.map_product_ish_categories(product_ish) ⇒ Object

Private



38
39
40
41
# File 'app/models/spree/piwik/line_item.rb', line 38

def self.map_product_ish_categories(product_ish)
  defaulted_taxons = product_ish.try(:taxons) || []
  defaulted_taxons.map(&:name).first(5)
end

.product_ish_attr(attr_name, product_ish) ⇒ Object

Private



28
29
30
31
32
33
34
35
# File 'app/models/spree/piwik/line_item.rb', line 28

def self.product_ish_attr(attr_name, product_ish)
  mapping_method = "map_product_ish_#{attr_name}".to_sym
  if respond_to?(mapping_method)
    send(mapping_method, product_ish)
  else
    product_ish.try(attr_name)
  end
end