Class: Stall::Routes

Inherits:
Object
  • Object
show all
Defined in:
lib/stall/routes.rb

Defined Under Namespace

Classes: CuratedProductListExistsConstraint, ManufacturerExistsConstraint, ProductCategoryExistsConstraint, ProductExistsConstraint

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(router) ⇒ Routes

Returns a new instance of Routes.



5
6
7
# File 'lib/stall/routes.rb', line 5

def initialize(router)
  @router = router
end

Instance Attribute Details

#routerObject (readonly)

Returns the value of attribute router.



3
4
5
# File 'lib/stall/routes.rb', line 3

def router
  @router
end

Instance Method Details

#controller_for(key) ⇒ Object



97
98
99
# File 'lib/stall/routes.rb', line 97

def controller_for(key)
  Stall.config.controllers[key] || "stall/#{key}"
end

#draw(mount_location, options = {}) ⇒ Object

This method is called with the options passed to the mount_stall routes helper.

You can pass it one of the following options to disable parts of the routing :

- { users: false }  =>  Disables built in devise user authentication
- { products: false } => Disables all products related routing
- { checkout: false } => Disables all checkout and cart related routing


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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/stall/routes.rb', line 19

def draw(mount_location, options = {})
  routes = self

  users = options.fetch(:users, true)
  products = options.fetch(:products, true)
  checkout = options.fetch(:checkout, true)

  router.instance_eval do
    if users
      devise_for :users, Stall.config.devise_for_user_config

      devise_scope :user do
        get '/users/omniauth/:provider/redirect' => 'stall/omniauth_callbacks#redirect', as: :user_omniauth_redirect
      end
    end

    scope mount_location do
      if products
        resources :products, only: [:index], as: :products, controller: routes.controller_for(:products)

        constraints ProductExistsConstraint.new do
          resources :products, path: '/', only: [:show], controller: routes.controller_for(:products)
        end

        constraints ProductCategoryExistsConstraint.new do
          resources :product_categories, path: '/', only: [:show], controller: routes.controller_for(:product_categories)
        end

        constraints CuratedProductListExistsConstraint.new do
          resources :curated_product_lists, path: '/', only: [:show], controller: routes.controller_for(:curated_product_lists) do
            resources :products, only: [:show], path: '/', controller: routes.controller_for(:products)
          end
        end

        constraints ManufacturerExistsConstraint.new do
          resources :manufacturers, path: '/', only: [:show], controller: routes.controller_for(:manufacturers) do
            resources :products, only: [:show], path: '/', controller: routes.controller_for(:products)
          end
        end
      end

      if checkout
        resources :carts, controller: routes.controller_for(:carts) do
          resources :line_items, only: [:create], controller: routes.controller_for(:cart_line_items)
          resource :credit, only: [:update, :destroy], controller: routes.controller_for(:cart_credits)
        end

        resources :wish_lists, only: [:show], controller: routes.controller_for(:wish_lists) do
          resources :line_items, only: [:create, :destroy], controller: routes.controller_for(:wish_list_line_items)
        end

        get 'checkout/:cart_key' => "#{routes.controller_for(:checkouts)}#show", as: :checkout

        scope 'checkout', as: :checkout do
          scope '(:cart_key)' do
            resource :step, only: [:show, :update], controller: routes.controller_for(:'checkout/steps') do
              post '/', action: :update, as: :update
              get  '/process', action: :update, as: :process
              # Allow external URLs process steps, allowing some payment
              # gateways to return the user through a POST request
              post '/process', action: :foreign_update
              get 'change/:step', action: :change, as: :change
            end
          end
        end

        scope '/:gateway' do
          resource :payment, only: [], controller: routes.controller_for(:payments) do
            member do
              match 'notify', action: 'notify', via: [:get, :post]
            end
          end
        end
      end
    end
  end
end