Skip to content

Commit 3cf65bc

Browse files
committed
Make Active Storage routes optional
Add configuration option to turn off drawing of Active Storage routes.
1 parent 3b36d75 commit 3cf65bc

File tree

6 files changed

+24
-1
lines changed

6 files changed

+24
-1
lines changed

activestorage/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
* Add `config.active_storage.draw_routes` to disable Active Storage routes.
2+
3+
*Gannon McGibbon*
4+
15
* Image analysis is skipped if ImageMagick returns an error.
26

37
`ActiveStorage::Analyzer::ImageAnalyzer#metadata` would previously raise a

activestorage/config/routes.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,4 @@
2929

3030
resolve("ActiveStorage::Blob") { |blob, options| route_for(:rails_blob, blob, options) }
3131
resolve("ActiveStorage::Attachment") { |attachment, options| route_for(:rails_blob, attachment.blob, options) }
32-
end
32+
end if ActiveStorage.draw_routes

activestorage/lib/active_storage.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ module ActiveStorage
6060
mattr_accessor :service_urls_expire_in, default: 5.minutes
6161

6262
mattr_accessor :routes_prefix, default: "/rails/active_storage"
63+
mattr_accessor :draw_routes, default: true
6364

6465
mattr_accessor :replace_on_assign_to_many, default: false
6566

activestorage/lib/active_storage/engine.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ class Engine < Rails::Engine # :nodoc:
7373
ActiveStorage.analyzers = app.config.active_storage.analyzers || []
7474
ActiveStorage.paths = app.config.active_storage.paths || {}
7575
ActiveStorage.routes_prefix = app.config.active_storage.routes_prefix || "/rails/active_storage"
76+
ActiveStorage.draw_routes = app.config.active_storage.draw_routes != false
7677

7778
ActiveStorage.variable_content_types = app.config.active_storage.variable_content_types || []
7879
ActiveStorage.content_types_to_serve_as_binary = app.config.active_storage.content_types_to_serve_as_binary || []

guides/source/configuring.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -885,6 +885,8 @@ text/javascript image/svg+xml application/postscript application/x-shockwave-fla
885885

886886
* `config.active_storage.replace_on_assign_to_many` determines whether assigning to a collection of attachments declared with `has_many_attached` replaces any existing attachments or appends to them. The default is `true`.
887887

888+
* `config.active_storage.draw_routes` can be used to toggle Active Storage route generation. The default is `true`.
889+
888890
### Results of `load_defaults`
889891

890892
#### With '5.0':

railties/test/application/configuration_test.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2593,6 +2593,21 @@ class MyLogger < ::Logger
25932593
MESSAGE
25942594
end
25952595

2596+
test "ActiveStorage.draw_routes can be configured via config.active_storage.draw_routes" do
2597+
app_file "config/environments/development.rb", <<-RUBY
2598+
Rails.application.configure do
2599+
config.active_storage.draw_routes = false
2600+
end
2601+
RUBY
2602+
2603+
output = rails("routes")
2604+
assert_not_includes(output, "rails_service_blob")
2605+
assert_not_includes(output, "rails_blob_representation")
2606+
assert_not_includes(output, "rails_disk_service")
2607+
assert_not_includes(output, "update_rails_disk_service")
2608+
assert_not_includes(output, "rails_direct_uploads")
2609+
end
2610+
25962611
test "hosts include .localhost in development" do
25972612
app "development"
25982613
assert_includes Rails.application.config.hosts, ".localhost"

0 commit comments

Comments
 (0)