Module: EffectiveCartsHelper
- Included in:
- Effective::CartsController, Effective::OrdersController, Effective::SubscriptionsController
- Defined in:
- app/helpers/effective_carts_helper.rb
Instance Method Summary collapse
- #current_cart(for_user = nil) ⇒ Object
- #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
2 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 |
# File 'app/helpers/effective_carts_helper.rb', line 2 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? Effective::Cart.where(:user_id => user.id).first_or_create.tap do |user_cart| if session[:cart].present? session_cart = Effective::Cart.where('user_id IS NULL').where(:id => session[:cart]).first if session_cart.present? session_cart.cart_items.update_all(:cart_id => user_cart.id) session_cart.destroy user_cart.reload end session[:cart] = nil end end elsif session[:cart].present? Effective::Cart.where('user_id IS NULL').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
37 38 39 40 41 42 43 44 |
# File 'app/helpers/effective_carts_helper.rb', line 37 def link_to_add_to_cart(purchasable, opts = {}) raise ArgumentError.new('expecting an acts_as_purchasable object') unless purchasable.respond_to?(:is_effectively_purchasable?) = {:class => 'btn', :rel => :nofollow, 'data-disable-with' => 'Adding...'}.merge(opts) [:class] = (([:class] || '') + ' btn-add-to-cart') link_to (.delete(:label) || 'Add to Cart'), effective_orders.add_to_cart_path(:purchasable_type => purchasable.class.name, :purchasable_id => purchasable.id.to_i), end |
#link_to_checkout(opts = {}) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 |
# File 'app/helpers/effective_carts_helper.rb', line 79 def link_to_checkout(opts = {}) = { :class => 'btn', :rel => :nofollow, :disable_with => 'Proceeding...' }.merge(opts) [:class] = (([:class] || '') + ' btn-checkout') link_to (.delete(:label) || 'Proceed to Checkout'), effective_orders.new_order_path, end |
#link_to_current_cart(opts = {}) ⇒ Object
30 31 32 33 34 35 |
# File 'app/helpers/effective_carts_helper.rb', line 30 def link_to_current_cart(opts = {}) = {:id => 'current_cart', :rel => :nofollow}.merge(opts) label = .delete(:label) || 'Cart' link_to (current_cart.size == 0 ? label : "#{label} (#{current_cart.size})"), effective_orders.cart_path, end |
#link_to_empty_cart(opts = {}) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'app/helpers/effective_carts_helper.rb', line 63 def link_to_empty_cart(opts = {}) = { :rel => :nofollow, :class => 'btn', :data => { :confirm => 'This will clear your entire cart. Are you sure? This cannot be undone!', :disable_with => 'Emptying...' }, :method => :delete }.merge(opts) [:class] = (([:class] || '') + ' btn-empty-cart btn-danger') link_to (.delete(:label) || 'Empty Cart'), effective_orders.cart_path, end |
#link_to_remove_from_cart(cart_item, opts = {}) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'app/helpers/effective_carts_helper.rb', line 46 def link_to_remove_from_cart(cart_item, opts = {}) raise ArgumentError.new('expecting an Effective::CartItem object') unless cart_item.kind_of?(Effective::CartItem) = { :rel => :nofollow, :data => { :confirm => 'Are you sure? This cannot be undone!', :disable_with => 'Removing...' }, :method => :delete }.merge(opts) [:class] = (([:class] || '') + ' btn-remove-from-cart') link_to (.delete(:label) || 'Remove'), effective_orders.remove_from_cart_path(cart_item), end |
#render_cart(cart = nil) ⇒ Object
91 92 93 94 |
# File 'app/helpers/effective_carts_helper.rb', line 91 def render_cart(cart = nil) cart ||= current_cart render(:partial => 'effective/carts/cart', :locals => {:cart => cart}) end |
#render_purchasables(*purchasables) ⇒ Object
96 97 98 |
# File 'app/helpers/effective_carts_helper.rb', line 96 def render_purchasables(*purchasables) render(:partial => 'effective/orders/order_items', :locals => {:order => Effective::Order.new(purchasables)}) end |