Class: CartItem

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
PropMixin
Defined in:
app/models/cart_item.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from PropMixin

#deserialized_properties, #getProp, #setProp, #set_props

Class Method Details

.attributesObject

matches #to_a


59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/models/cart_item.rb', line 59

def self.attributes
  [
    'description',
    'details',
    'vendor',
    'url',
    'notes',
    'part_number',
    'green',
    'features',
    'socio',
    'quantity',
    'unit price',
    'price for quantity'
  ]
end

.from_params(params) ⇒ Object


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/models/cart_item.rb', line 76

def self.from_params(params)
  cart = self.new(
    :vendor => params.fetch(:vendor, nil),
    :description => params.fetch(:description, nil),
    :url => params.fetch(:url, nil),
    :notes => params.fetch(:notes, nil),
    :quantity => params.fetch(:qty , 0),
    :details => params.fetch(:details, nil),
    :part_number => params.fetch(:partNumber , nil),
    :price => params.fetch(:price, nil).gsub(/[\$\,]/,"").to_f
  )

  traits_params = params[:traits] || []
  cart.initialize_traits(traits_params)

  props = params[:properties]
  unless props.blank?
    cart.set_props(props)
  end

  cart
end

Instance Method Details

#build_traits(name, value) ⇒ Object


54
55
56
# File 'app/models/cart_item.rb', line 54

def build_traits(name, value)
  cart_item_traits.build(name: name, value: value)
end

#featuresObject


12
13
14
# File 'app/models/cart_item.rb', line 12

def features
  cart_item_traits.select{ |trait| trait.name.include?("feature") }.map(&:value)
end

#green?Boolean


8
9
10
# File 'app/models/cart_item.rb', line 8

def green?
  cart_item_traits.map(&:name).include?('green')
end

#initialize_traits(traits_params) ⇒ Object


42
43
44
45
46
47
48
49
50
51
52
# File 'app/models/cart_item.rb', line 42

def initialize_traits(traits_params)
  traits_params.each do |trait|
    if trait[1].kind_of?(Array)
      trait[1].each do |individual|
        build_traits(trait[0], individual) if individual.present?
      end
    else
      build_traits(trait[0], trait_value(trait))
    end
  end
end

#socioObject


16
17
18
# File 'app/models/cart_item.rb', line 16

def socio
  cart_item_traits.select{ |trait| trait.name.include?("socio") }.map(&:value)
end

#subtotalObject


20
21
22
# File 'app/models/cart_item.rb', line 20

def subtotal
  price * quantity
end

#to_aObject

matches .attributes


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/models/cart_item.rb', line 25

def to_a
  [
    self.description,
    self.details,
    self.vendor,
    self.url,
    self.notes,
    self.part_number,
    self.green?,
    self.features,
    self.socio,
    self.quantity,
    self.price,
    self.quantity * self.price
  ]
end

#trait_value(trait) ⇒ Object


99
100
101
# File 'app/models/cart_item.rb', line 99

def trait_value trait
  trait[1].presence || "true"
end