Class: ActionDispatch::Routing::Mapper

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

Instance Method Summary collapse

Instance Method Details

#accountability_for(_tenants, options = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/accountability/rails/routes.rb', line 5

def accountability_for(_tenants, options = {})
  options[:controllers] ||= {}
  options[:routes] ||= {}

  options[:controllers].reverse_merge! default_accountability_controller_options
  options[:routes].reverse_merge! default_accountability_route_options

  options[:path_prefix] ||= options[:path] if options[:path_prefix].nil?

  define_accountability_routes(options)
end

#accountability_routes(options) ⇒ Object



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
# File 'lib/accountability/rails/routes.rb', line 24

def accountability_routes(options)
  scope path: options[:path_prefix], as: :accountability do
    resources :accounts, controller: options.dig(:controllers, :accounts), path: options.dig(:routes, :accounts) do
      resources :billing_configurations, controller: options.dig(:controllers, :billing_configurations), path: options.dig(:routes, :billing_configurations) do
        member { patch :designate_as_primary }
      end

      resources :payments, only: :create, controller: 'accountability/payments', path: 'payments'
      resources :statements, only: [], controller: 'accountability/statements', path: 'statements' do
        member { get :download_pdf }
      end
    end

    resources :products, controller: options.dig(:controllers, :products), path: options.dig(:routes, :products)

    resources :order_groups, controller: options.dig(:controllers, :order_groups), path: options.dig(:routes, :order_groups) do
      member do
        post :add_item
        put :checkout
      end
    end
  end

  unscoped_direct(:stripe_v3_javascript) { 'https://js.stripe.com/v3/' }
end

#default_accountability_controller_optionsObject



50
51
52
53
54
55
56
57
# File 'lib/accountability/rails/routes.rb', line 50

def default_accountability_controller_options
  {
    accounts: 'accountability/accounts',
    products: 'accountability/products',
    order_groups: 'accountability/order_groups',
    billing_configurations: 'accountability/billing_configurations'
  }
end

#default_accountability_route_optionsObject



59
60
61
62
63
64
65
66
# File 'lib/accountability/rails/routes.rb', line 59

def default_accountability_route_options
  {
    accounts: 'accounts',
    products: 'products',
    order_groups: 'orders',
    billing_configurations: 'billing_configurations'
  }
end

#define_accountability_routes(options) ⇒ Object



17
18
19
20
21
22
# File 'lib/accountability/rails/routes.rb', line 17

def define_accountability_routes(options)
  cartographer = -> { accountability_routes(options) }

  cartographer.call
  Accountability::Engine.routes.draw(&cartographer)
end

#unscoped_direct(name, options = {}, &block) ⇒ Object

Mimic the functionality of Mapper#direct so that we can add a “direct” route from the cartographer proc even though we are in a scope because of #isolate_namespace. github.com/rails/rails/blob/6-0-stable/actionpack/lib/action_dispatch/routing/mapper.rb#L2119-L2125



71
72
73
# File 'lib/accountability/rails/routes.rb', line 71

def unscoped_direct(name, options = {}, &block)
  @set.add_url_helper(name, options, &block)
end