Class: PowerShop::Middleware::ShoppingCart

Inherits:
Object
  • Object
show all
Defined in:
lib/power_shop/middleware/shopping_cart.rb

Overview

Public: class find shopping cart from session for existing user or create new shopping cart in session

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ ShoppingCart

Returns a new instance of ShoppingCart.



6
7
8
# File 'lib/power_shop/middleware/shopping_cart.rb', line 6

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/power_shop/middleware/shopping_cart.rb', line 10

def call(env)
  unless env['PATH_INFO'] =~ %r{^/assets/}
    env['shopping_cart'] ||= get_or_crete_cart(env['rack.session'])
  end

  @app.call(env)
end

#get_or_crete_cart(session) ⇒ Object



18
19
20
21
22
23
# File 'lib/power_shop/middleware/shopping_cart.rb', line 18

def get_or_crete_cart(session)
  cart = ::ShoppingCart.find_or_create_by_id(session[:shopping_cart_id])
  session[:shopping_cart_id] = cart.id

  cart
end