Module: EffectiveCartsHelper
- Defined in:
- app/helpers/effective_carts_helper.rb
Instance Method Summary collapse
-
#current_cart(for_user = nil) ⇒ Object
TODO: Consider unique.
- #link_to_add_to_cart(purchasable, opts = {}) ⇒ Object
- #link_to_checkout(opts = {}) ⇒ Object
- #link_to_current_cart(opts = {}) ⇒ Object
- #link_to_empty_cart(opts = {}) ⇒ Object
- #link_to_remove_from_cart(cart_item, opts = {}) ⇒ Object
- #render_cart(cart = nil) ⇒ Object
- #render_purchasables(*purchasables) ⇒ Object
Instance Method Details
#current_cart(for_user = nil) ⇒ Object
TODO: Consider unique
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'app/helpers/effective_carts_helper.rb', line 3 def current_cart(for_user = nil) @cart ||= ( user = for_user || (current_user rescue nil) # rescue protects me against Devise not being installed if user.present? user_cart = Effective::Cart.where(user: user).first_or_create # Merge session cart into user cart. if session[:cart].present? session_cart = Effective::Cart.where(user: nil).where(id: session[:cart]).first if session_cart session_cart.cart_items.each { |i| user_cart.add(i.purchasable, quantity: i.quantity, unique: i.unique) } session_cart.destroy end session[:cart] = nil end user_cart elsif session[:cart].present? Effective::Cart.where(user_id: nil).where(id: session[:cart]).first_or_create else cart = Effective::Cart.create! session[:cart] = cart.id cart end ) end |
#link_to_add_to_cart(purchasable, opts = {}) ⇒ Object
47 48 49 50 51 52 53 54 55 56 |
# File 'app/helpers/effective_carts_helper.rb', line 47 def link_to_add_to_cart(purchasable, opts = {}) raise 'expecting an acts_as_purchasable object' unless purchasable.kind_of?(ActsAsPurchasable) = { label: 'Add to Cart', class: 'btn btn-primary', rel: :nofollow }.merge(opts) label = .delete(:label) [:class] = (([:class] || '') + ' btn-add-to-cart') link_to(label, effective_orders.add_to_cart_path(purchasable_type: purchasable.class.name, purchasable_id: purchasable.id.to_i), ) end |
#link_to_checkout(opts = {}) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'app/helpers/effective_carts_helper.rb', line 90 def link_to_checkout(opts = {}) = { label: 'Checkout', class: 'btn btn-primary', rel: :nofollow }.merge(opts) order = .delete(:order) label = .delete(:label) [:class] = (([:class] || '') + ' btn-checkout') if order.present? link_to(label, effective_orders.edit_order_path(order), ) else link_to(label, effective_orders.new_order_path, ) end end |
#link_to_current_cart(opts = {}) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'app/helpers/effective_carts_helper.rb', line 33 def link_to_current_cart(opts = {}) = { label: 'My Cart', id: 'current_cart', rel: :nofollow, class: 'btn btn-secondary' }.merge(opts) label = .delete(:label) [:class] = (([:class] || '') + ' btn-current-cart') link_to (current_cart.size == 0 ? label : "#{label} (#{current_cart.size})"), effective_orders.cart_path, end |
#link_to_empty_cart(opts = {}) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'app/helpers/effective_carts_helper.rb', line 75 def link_to_empty_cart(opts = {}) = { label: 'Empty Cart', class: 'btn btn-danger', rel: :nofollow, data: { confirm: 'This will clear your entire cart. Are you sure?' }, method: :delete }.merge(opts) label = .delete(:label) [:class] = (([:class] || '') + ' btn-empty-cart') link_to(label, effective_orders.cart_path, ) end |
#link_to_remove_from_cart(cart_item, opts = {}) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'app/helpers/effective_carts_helper.rb', line 58 def link_to_remove_from_cart(cart_item, opts = {}) raise 'expecting an Effective::CartItem object' unless cart_item.kind_of?(Effective::CartItem) = { label: 'Remove', class: 'btn btn-primary', rel: :nofollow, data: { confirm: 'Are you sure? This cannot be undone!' }, method: :delete }.merge(opts) label = .delete(:label) [:class] = (([:class] || '') + ' btn-remove-from-cart') link_to(label, effective_orders.remove_from_cart_path(cart_item), ) end |
#render_cart(cart = nil) ⇒ Object
104 105 106 107 |
# File 'app/helpers/effective_carts_helper.rb', line 104 def render_cart(cart = nil) cart ||= current_cart render(partial: 'effective/carts/cart', locals: { cart: cart }) end |