daino3
daino3

Reputation: 4566

Blacklist a subfolder in rails eager load path

I need to blacklist routes in an api namespace from every environment except test. Reason being, our integration suite with some microservices using the API needs to wipe the API database in between test runs.

assuming I have the following app directory structure.

app
 \_controllers
    \_api
       \_v1
         \_test
            \_test_controller.rb
          base_controller.rb
          some_controller.rb

How would I go about blacklisting controllers/api/v1/test/ but still retaining app/controllers in the eager load path?

EDIT:

  # routes.rb
  namespace :api do
    namespace :v1 do
      resources :instruments, only: [:index, :show]
      resources :instrument_roots, only: [:index, :show]
      if Rails.env.test? # integration suite handlers
        namespace :test do
          resources :instruments, only: [:destroy]
          post '/reset' => 'devise/sessions#destroy'
        end
      end
    end
  end

I'm removing the routes with an environment check, but ideally I wouldn't load the controllers at all.

Upvotes: 0

Views: 253

Answers (1)

Jim Van Fleet
Jim Van Fleet

Reputation: 1118

Removing the subdirectories of app/controllers is unlikely to be possible, as the individual directory entries are not present on the load path in the first place.

You may get results from creating a RAILS_ROOT/test_api_support (which in turn would have an app/controllers directory), and moving these files there. You could then add that directory to your load path in only your spec_helper.rb, test_helper.rb, or config/environments/test.rb.

This approach should have the effect of removing them from your VM's memory in other environments.

Upvotes: 1

Related Questions