Class: Piggybak::Cart

Inherits:
Object
  • Object
show all
Defined in:
app/models/piggybak/cart.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cookie = '') ⇒ Cart

Returns a new instance of Cart.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'app/models/piggybak/cart.rb', line 10

def initialize(cookie='')
  self.sellables = []
  self.errors = []
  cookie ||= ''
  cookie.split(';').each do |item|
    item_sellable = Piggybak::Sellable.where(id: item.split(':')[0]).first
    if item_sellable.present?
      self.sellables << { :sellable => item_sellable, :quantity => (item.split(':')[1]).to_i }
    end
  end
  self.total = self.sellables.sum { |item| item[:quantity]*item[:sellable].price }

  self.extra_data = {}
end

Instance Attribute Details

#errorsObject

Returns the value of attribute errors.



5
6
7
# File 'app/models/piggybak/cart.rb', line 5

def errors
  @errors
end

#extra_dataObject

Returns the value of attribute extra_data.



6
7
8
# File 'app/models/piggybak/cart.rb', line 6

def extra_data
  @extra_data
end

#sellablesObject Also known as: items

Returns the value of attribute sellables.



3
4
5
# File 'app/models/piggybak/cart.rb', line 3

def sellables
  @sellables
end

#totalObject Also known as: subtotal

Returns the value of attribute total.



4
5
6
# File 'app/models/piggybak/cart.rb', line 4

def total
  @total
end

Class Method Details

.add(cookie, params) ⇒ Object



41
42
43
44
45
46
# File 'app/models/piggybak/cart.rb', line 41

def self.add(cookie, params)
  cart = to_hash(cookie)
  cart["#{params[:sellable_id]}"] ||= 0
  cart["#{params[:sellable_id]}"] += params[:quantity].to_i
  to_string(cart)
end

.remove(cookie, sellable_id) ⇒ Object



48
49
50
51
52
# File 'app/models/piggybak/cart.rb', line 48

def self.remove(cookie, sellable_id)
  cart = to_hash(cookie)
  cart[sellable_id] = 0
  to_string(cart)
end

.to_hash(cookie) ⇒ Object



25
26
27
28
29
30
31
# File 'app/models/piggybak/cart.rb', line 25

def self.to_hash(cookie)
  cookie ||= ''
  cookie.split(';').inject({}) do |hash, item|
    hash[item.split(':')[0]] = (item.split(':')[1]).to_i
    hash
  end
end

.to_string(cart) ⇒ Object



33
34
35
36
37
38
39
# File 'app/models/piggybak/cart.rb', line 33

def self.to_string(cart)
  cookie = ''
  cart.each do |k, v|
    cookie += "#{k.to_s}:#{v.to_s};" if v.to_i > 0
  end
  cookie
end

.update(cookie, params) ⇒ Object



54
55
56
57
58
# File 'app/models/piggybak/cart.rb', line 54

def self.update(cookie, params)
  cart = to_hash(cookie)
  cart.each { |k, v| cart[k] = params[:quantity][k].to_i }
  to_string(cart)
end

Instance Method Details

#empty?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'app/models/piggybak/cart.rb', line 94

def empty?
  self.sellables.inject(0) { |nitems, item| nitems + item[:quantity] } == 0      
end

#set_extra_data(form_params) ⇒ Object



88
89
90
91
92
# File 'app/models/piggybak/cart.rb', line 88

def set_extra_data(form_params)
  form_params.each do |k, v|
    self.extra_data[k.to_sym] = v if ![:controller, :action].include?(k)
  end
end


60
61
62
63
64
65
66
# File 'app/models/piggybak/cart.rb', line 60

def to_cookie
  cookie = ''
  self.sellables.each do |item|
    cookie += "#{item[:sellable].id.to_s}:#{item[:quantity].to_s};" if item[:quantity].to_i > 0
  end
  cookie
end

#update_quantitiesObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'app/models/piggybak/cart.rb', line 68

def update_quantities
  self.errors = []
  new_sellables = []
  self.sellables.each do |item|
    if !item[:sellable].active
      self.errors << ["Sorry, #{item[:sellable].description} is no longer for sale"]
    elsif item[:sellable].unlimited_inventory || item[:sellable].quantity >= item[:quantity]
      new_sellables << item
    elsif item[:sellable].quantity == 0
      self.errors << ["Sorry, #{item[:sellable].description} is no longer available"]
    else
      self.errors << ["Sorry, only #{item[:sellable].quantity} available for #{item[:sellable].description}"]
      item[:quantity] = item[:sellable].quantity
      new_sellables << item if item[:quantity] > 0
    end
  end
  self.sellables = new_sellables
  self.total = self.sellables.sum { |item| item[:quantity]*item[:sellable].price }
end