Class: Effective::Cart
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Effective::Cart
- Defined in:
- app/models/effective/cart.rb
Instance Method Summary collapse
-
#add(item, quantity: 1, unique: true) ⇒ Object
cart.add(@product, unique: -> (a, b) { a.kind_of?(Product) && b.kind_of?(Product) && a.category == b.category }) cart.add(@product, unique: :category) cart.add(@product, unique: false) # Add as many as you want.
- #blank? ⇒ Boolean
- #clear! ⇒ Object
- #find(item) ⇒ Object
- #includes?(item) ⇒ Boolean
- #present? ⇒ Boolean
- #purchasables ⇒ Object
- #remove(item) ⇒ Object
- #size ⇒ Object
- #subtotal ⇒ Object
Instance Method Details
#add(item, quantity: 1, unique: true) ⇒ Object
cart.add(@product, unique: -> (a, b) { a.kind_of?(Product) && b.kind_of?(Product) && a.category == b.category }) cart.add(@product, unique: :category) cart.add(@product, unique: false) # Add as many as you want
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'app/models/effective/cart.rb', line 20 def add(item, quantity: 1, unique: true) raise 'expecting an acts_as_purchasable object' unless item.kind_of?(ActsAsPurchasable) existing = ( if unique.kind_of?(Proc) cart_items.find { |cart_item| instance_exec(item, cart_item.purchasable, &unique) } elsif unique.kind_of?(Symbol) || (unique.kind_of?(String) && unique != 'true') raise "expected item to respond to unique #{unique}" unless item.respond_to?(unique) cart_items.find { |cart_item| cart_item.purchasable.respond_to?(unique) && item.send(unique) == cart_item.purchasable.send(unique) } elsif unique.present? find(item) end ) if existing if unique || (existing.unique.present?) existing.assign_attributes(purchasable: item, quantity: quantity, unique: existing.unique) else existing.quantity = existing.quantity + quantity end end if item.quantity_enabled? && (existing ? existing.quantity : quantity) > item.quantity_remaining raise EffectiveOrders::SoldOutException, "#{item.title} is sold out" end existing ||= cart_items.build(purchasable: item, quantity: quantity, unique: (unique.to_s if unique.kind_of?(Symbol) || unique.kind_of?(String) || unique == true)) save! end |
#blank? ⇒ Boolean
80 81 82 |
# File 'app/models/effective/cart.rb', line 80 def blank? size <= 0 end |
#clear! ⇒ Object
50 51 52 53 |
# File 'app/models/effective/cart.rb', line 50 def clear! cart_items.each { |cart_item| cart_item.mark_for_destruction } save! end |
#find(item) ⇒ Object
64 65 66 |
# File 'app/models/effective/cart.rb', line 64 def find(item) cart_items.find { |cart_item| cart_item == item || cart_item.purchasable == item } end |
#includes?(item) ⇒ Boolean
60 61 62 |
# File 'app/models/effective/cart.rb', line 60 def includes?(item) find(item).present? end |
#present? ⇒ Boolean
76 77 78 |
# File 'app/models/effective/cart.rb', line 76 def present? size > 0 end |
#purchasables ⇒ Object
68 69 70 |
# File 'app/models/effective/cart.rb', line 68 def purchasables cart_items.map { |cart_item| cart_item.purchasable } end |
#remove(item) ⇒ Object
55 56 57 58 |
# File 'app/models/effective/cart.rb', line 55 def remove(item) find(item).try(:mark_for_destruction) save! end |
#size ⇒ Object
72 73 74 |
# File 'app/models/effective/cart.rb', line 72 def size cart_items_count || cart_items.length end |
#subtotal ⇒ Object
84 85 86 |
# File 'app/models/effective/cart.rb', line 84 def subtotal cart_items.map { |ci| ci.subtotal }.sum end |