简体   繁体   中英

Autoloading rails controllers outside app/controllers folder

Is there a way how to properly load controllers outside of app/controllers folder? Background - the situation is that we have multiple clients and we didn't want to fork and create client-specific rails apps, therefore, we have only one app and we control business logic via services. Version of the app is determined via ENV variables.

We have defined that app/controllers folder will only be used for shared logic and we have created folders for each client inside app/clients/[CLIENT]/ that will be used for client-specific needs - services, concerns, configs, controllers, even specific routes will go here.

From this app/clients/[CLIENT]/controllers we would like to additionally load client-specific routes and controllers. An exemplary controller inside the client folder may look like this.

module Clients
  module ClientA
    module Controllers
     module Api
       module V1
        class RentalsController < ApplicationController; end
       end
     end
    end
  end
end

Is there a way how to properly load this? I tried doing the following in the application.rb

config.autoload_paths += %W(#{config.root}/app/clients/ENV['CLIENT']/**/**)

How do you even mount this controller then? I tried the following but didn't work.

resources :rentals, controller: 'clients/ClientA/controllers/api/v1/rentals' do

Use snake_case for the folders -- client_a instead of ClientA

And then:

config.autoload_paths <<
  Rails.root.join(
    'app',
    'clients',
    ENV['CLIENT'].underscore,
    'controllers',
    '**'
  )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM