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
19 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 |
# File 'app/models/effective/cart.rb', line 19 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
79 80 81 |
# File 'app/models/effective/cart.rb', line 79 def blank? size <= 0 end |
#clear! ⇒ Object
49 50 51 52 |
# File 'app/models/effective/cart.rb', line 49 def clear! cart_items.each { |cart_item| cart_item.mark_for_destruction } cart_items.present? ? save! : true end |
#find(item) ⇒ Object
63 64 65 |
# File 'app/models/effective/cart.rb', line 63 def find(item) cart_items.find { |cart_item| cart_item == item || cart_item.purchasable == item } end |
#includes?(item) ⇒ Boolean
59 60 61 |
# File 'app/models/effective/cart.rb', line 59 def includes?(item) find(item).present? end |
#present? ⇒ Boolean
75 76 77 |
# File 'app/models/effective/cart.rb', line 75 def present? size > 0 end |
#purchasables ⇒ Object
67 68 69 |
# File 'app/models/effective/cart.rb', line 67 def purchasables cart_items.map { |cart_item| cart_item.purchasable } end |
#remove(item) ⇒ Object
54 55 56 57 |
# File 'app/models/effective/cart.rb', line 54 def remove(item) find(item).try(:mark_for_destruction) save! end |
#size ⇒ Object
71 72 73 |
# File 'app/models/effective/cart.rb', line 71 def size cart_items_count || cart_items.length end |
#subtotal ⇒ Object
83 84 85 |
# File 'app/models/effective/cart.rb', line 83 def subtotal cart_items.map { |ci| ci.subtotal }.sum end |