Class: Piggybak::Cart
- Inherits:
-
Object
- Object
- Piggybak::Cart
- Defined in:
- app/models/piggybak/cart.rb
Instance Attribute Summary collapse
-
#errors ⇒ Object
Returns the value of attribute errors.
-
#extra_data ⇒ Object
Returns the value of attribute extra_data.
-
#sellables ⇒ Object
(also: #items)
Returns the value of attribute sellables.
-
#total ⇒ Object
(also: #subtotal)
Returns the value of attribute total.
Class Method Summary collapse
- .add(cookie, params) ⇒ Object
- .remove(cookie, sellable_id) ⇒ Object
- .to_hash(cookie) ⇒ Object
- .to_string(cart) ⇒ Object
- .update(cookie, params) ⇒ Object
Instance Method Summary collapse
- #empty? ⇒ Boolean
-
#initialize(cookie = '') ⇒ Cart
constructor
A new instance of Cart.
- #set_extra_data(form_params) ⇒ Object
- #to_cookie ⇒ Object
- #update_quantities ⇒ Object
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(='') self.sellables = [] self.errors = [] ||= '' .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
#errors ⇒ Object
Returns the value of attribute errors.
5 6 7 |
# File 'app/models/piggybak/cart.rb', line 5 def errors @errors end |
#extra_data ⇒ Object
Returns the value of attribute extra_data.
6 7 8 |
# File 'app/models/piggybak/cart.rb', line 6 def extra_data @extra_data end |
#sellables ⇒ Object 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 |
#total ⇒ Object 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(, params) cart = to_hash() 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(, sellable_id) cart = to_hash() 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() ||= '' .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) = '' cart.each do |k, v| += "#{k.to_s}:#{v.to_s};" if v.to_i > 0 end end |
.update(cookie, params) ⇒ Object
54 55 56 57 58 |
# File 'app/models/piggybak/cart.rb', line 54 def self.update(, params) cart = to_hash() cart.each { |k, v| cart[k] = params[:quantity][k].to_i } to_string(cart) end |
Instance Method Details
#empty? ⇒ 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 |
#to_cookie ⇒ Object
60 61 62 63 64 65 66 |
# File 'app/models/piggybak/cart.rb', line 60 def = '' self.sellables.each do |item| += "#{item[:sellable].id.to_s}:#{item[:quantity].to_s};" if item[:quantity].to_i > 0 end end |
#update_quantities ⇒ Object
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 |