14

I have a brand new Rails 6 app and without anything in the config/routes.rb, the output of bin/rails routes has a massive list of very long urls for ActiveStorage, Action Mailbox, and conductor.

This is making bin/rails routes completely useless as a form of documentation, especially since the options for bin/rails routes don't allow filtering out things.

I don't want to omit these parts of Rails as I may need them. But I would prefer these routes a) not exist if I'm not using them and b) not show up in bin/rails routes.

Does anyone know how to make this work?

davetron5000
  • 24,123
  • 11
  • 70
  • 98
  • Have you tried disabling them like outlined [here](https://stackoverflow.com/a/50307393)?. [Here's the latest railties being required](https://github.com/rails/rails/blob/v6.0.2.1/railties/lib/rails/all.rb). If you don't want to disable them, but still not show them in rails routes, maybe create a bash script that uses `rails routes` with grep to exclude them (https://stackoverflow.com/a/3548465) – AbM Jan 04 '20 at 19:26
  • Yeah, I don't want to remove them from my app. It's just weird that if I haven't configured e.g. sendgrid, I get these live sendgrid-related URLs. I could make a `bin/routes` and `grep -v`, but I'm wondering if there is a Rails way to do this before I embark down that road. – davetron5000 Jan 04 '20 at 23:21

2 Answers2

15

As of Rails 6.0.2.1, this is the way to do it:

In config/application.rb, remove the line require "rails/all" and replace it with this:

# See https://github.com/rails/rails/blob/v6.0.2.1/railties/lib/rails/all.rb for the list
# of what is being included here
require "rails"

# This list is here as documentation only - it's not used
omitted = %w(
  active_storage/engine
  action_cable/engine
  action_mailbox/engine
  action_text/engine
)

# Only the frameworks in Rails that do not pollute our routes
%w(
  active_record/railtie
  action_controller/railtie
  action_view/railtie
  action_mailer/railtie
  active_job/railtie
  rails/test_unit/railtie
  sprockets/railtie
).each do |railtie|
  begin
    require railtie
  rescue LoadError
  end
end

Note that if you leave actiontext in, you still get a few active storage routes included. Not sure why. This configuration basically means you cannot use active storage, action text, or action mailbox. Bringing those back in will bring back many routes you will never need.

Also note this solution has a carrying cost because with each Rails version upgrade, you must examine rails/all.rb to make sure no new frameworks were added that you might care about (or removed that you no longer should be requiring).

Note: You will also need to remove references to active storage from the files in config/environments/*.rb

davetron5000
  • 24,123
  • 11
  • 70
  • 98
  • I don't think it will affect it at all. – davetron5000 Sep 24 '20 at 14:09
  • 1
    ruby's module, class will not be freed by GC. If we avoid require 'active_storage etc.' therefore ruby use less memory from OS right ? – 孙悟空 Sep 25 '20 at 10:14
  • I mean, maybe, but the answer here isn't something you should do if you are concerned about memory usage. If you are concerned about memory usage, you should profile, look for hot spots and address them. – davetron5000 Sep 25 '20 at 14:28
  • @da Profile a running application can be challenging. – Matt Feb 13 '21 at 20:30
  • I believe for this case it's best to omit only `action_cable/engine`, `action_mailbox/engine` and `action_text/engine`. do `require "active_storage/engine"` – m_antis Feb 27 '21 at 16:31
  • I tried removing action_mailbox as explained here but when running "bundle exec derailed bundle:mem development" I still see "action_mailbox/engine: 33.582 MiB" in the top. – sparkle Jul 05 '22 at 13:00
2

You can have the ActionMailbox routes omitted by commenting out the specific require line in your application.rb.

Specifically if you comment the require "action_mailbox/engine" line you'll no longer see any of the /rails/action_mailbox or /rails/conductor/action_mailbox routes.

You'll need to restart the app for the changes to take affect.