Module: Brick::RouteSet

Defined in:
lib/brick.rb

Instance Method Summary collapse

Instance Method Details

#finalize!Object



463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
# File 'lib/brick.rb', line 463

def finalize!
  existing_controllers = routes.each_with_object({}) { |r, s| c = r.defaults[:controller]; s[c] = nil if c }
  ::Rails.application.routes.append do
    unless ::Brick.config.default_route_fallback.blank? || ::Rails.application.routes.named_routes.send(:routes)[:root]
      send(:root, "#{::Brick.config.default_route_fallback}#index")
    end
    # %%% TODO: If no auto-controllers then enumerate the controllers folder in order to build matching routes
    # If auto-controllers and auto-models are both enabled then this makes sense:
    ::Brick.relations.each do |rel_name, v|
      rel_name = rel_name.split('.').map(&:underscore)
      schema_names = rel_name[0..-2]
      schema_names.shift if ::Brick.apartment_multitenant && schema_names.first == Apartment.default_schema
      k = rel_name.last
      unless existing_controllers.key?(controller_name = k.pluralize)
        options = {}
        options[:only] = [:index, :show] if v.key?(:isView)
        if schema_names.present? # && !Object.const_defined('Apartment')
          send(:namespace, schema_names.first) do
            send(:resources, controller_name.to_sym, **options)
          end
        else
          send(:resources, controller_name.to_sym, **options)
        end
      end
      if ::Brick.config.add_status && instance_variable_get(:@set).named_routes.names.exclude?(:brick_status)
        get('/brick_status', to: 'brick_gem#status', as: 'brick_status')
      end
      if ::Brick.config.add_orphans && instance_variable_get(:@set).named_routes.names.exclude?(:brick_orphans)
        get('/brick_orphans', to: 'brick_gem#orphans', as: 'brick_orphans')
      end
    end
    send(:get, '/api-docs/v1/swagger.json', { to: 'brick_swagger#index' }) if Object.const_defined?('Rswag::Ui')
  end
  super
end