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
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 49 |
# File 'app/models/effective/cart.rb', line 21 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.purchasable_name} is sold out" end existing ||= cart_items.build(purchasable: item, quantity: quantity, unique: (unique.to_s unless unique.kind_of?(Proc))) save! end |
#blank? ⇒ Boolean
81 82 83 |
# File 'app/models/effective/cart.rb', line 81 def blank? size <= 0 end |
#clear! ⇒ Object
51 52 53 54 |
# File 'app/models/effective/cart.rb', line 51 def clear! cart_items.each { |cart_item| cart_item.mark_for_destruction } cart_items.present? ? save! : true end |
#find(item) ⇒ Object
65 66 67 |
# File 'app/models/effective/cart.rb', line 65 def find(item) cart_items.find { |cart_item| cart_item == item || cart_item.purchasable == item } end |
#includes?(item) ⇒ Boolean
61 62 63 |
# File 'app/models/effective/cart.rb', line 61 def includes?(item) find(item).present? end |
#present? ⇒ Boolean
77 78 79 |
# File 'app/models/effective/cart.rb', line 77 def present? size > 0 end |
#purchasables ⇒ Object
69 70 71 |
# File 'app/models/effective/cart.rb', line 69 def purchasables cart_items.map { |cart_item| cart_item.purchasable } end |
#remove(item) ⇒ Object
56 57 58 59 |
# File 'app/models/effective/cart.rb', line 56 def remove(item) find(item).try(:mark_for_destruction) save! end |
#size ⇒ Object
73 74 75 |
# File 'app/models/effective/cart.rb', line 73 def size cart_items_count || cart_items.length end |
#subtotal ⇒ Object
85 86 87 |
# File 'app/models/effective/cart.rb', line 85 def subtotal cart_items.map { |ci| ci.subtotal }.sum end |