Skip to content

Commit 7937092

Browse files
authored
Merge pull request rails#49723 from mylesboone/activestorage_touch_config
Add config option to not touch records
2 parents 08473e3 + 13d66b1 commit 7937092

File tree

6 files changed

+20
-4
lines changed

6 files changed

+20
-4
lines changed

activestorage/app/models/active_storage/attachment.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class ActiveStorage::Attachment < ActiveStorage::Record
2424
# :method:
2525
#
2626
# Returns the associated record.
27-
belongs_to :record, polymorphic: true, touch: true
27+
belongs_to :record, polymorphic: true, touch: ActiveStorage.touch_attachment_records
2828

2929
##
3030
# :method:

activestorage/app/models/active_storage/blob.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class ActiveStorage::Blob < ActiveStorage::Record
4747
self.service_name ||= self.class.service&.name
4848
end
4949

50-
after_update :touch_attachment_records
50+
after_update :touch_attachments
5151

5252
after_update_commit :update_service_metadata, if: -> { content_type_previously_changed? || metadata_previously_changed? }
5353

@@ -376,8 +376,14 @@ def service_metadata
376376
end
377377
end
378378

379-
def touch_attachment_records
380-
attachments.includes(:record).each do |attachment|
379+
def touch_attachments
380+
attachments.then do |relation|
381+
if ActiveStorage.touch_attachment_records
382+
relation.includes(:record)
383+
else
384+
relation
385+
end
386+
end.each do |attachment|
381387
attachment.touch
382388
end
383389
end

activestorage/lib/active_storage.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@ module ActiveStorage
354354
mattr_accessor :unsupported_image_processing_arguments
355355

356356
mattr_accessor :service_urls_expire_in, default: 5.minutes
357+
mattr_accessor :touch_attachment_records, default: true
357358
mattr_accessor :urls_expire_in
358359

359360
mattr_accessor :routes_prefix, default: "/rails/active_storage"

activestorage/lib/active_storage/engine.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ class Engine < Rails::Engine # :nodoc:
110110
ActiveStorage.variable_content_types = app.config.active_storage.variable_content_types || []
111111
ActiveStorage.web_image_content_types = app.config.active_storage.web_image_content_types || []
112112
ActiveStorage.content_types_to_serve_as_binary = app.config.active_storage.content_types_to_serve_as_binary || []
113+
ActiveStorage.touch_attachment_records = app.config.active_storage.touch_attachment_records != false
113114
ActiveStorage.service_urls_expire_in = app.config.active_storage.service_urls_expire_in || 5.minutes
114115
ActiveStorage.urls_expire_in = app.config.active_storage.urls_expire_in
115116
ActiveStorage.content_types_allowed_inline = app.config.active_storage.content_types_allowed_inline || []

activestorage/test/engine_test.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,8 @@ class ActiveStorage::EngineTest < ActiveSupport::TestCase
2525
test "image/bmp is a default content type" do
2626
assert_includes ActiveStorage.variable_content_types, "image/bmp"
2727
end
28+
29+
test "true is the default touch_attachment_records value" do
30+
assert_equal true, ActiveStorage.touch_attachment_records
31+
end
2832
end

guides/source/configuring.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2848,6 +2848,10 @@ The default is 5 minutes.
28482848

28492849
Determines the default expiry of URLs in the Rails application generated by Active Storage. The default is nil.
28502850

2851+
#### `config.active_storage.touch_attachment_records`
2852+
2853+
Directs ActiveStorage::Attachments to touch its corresponding record when updated. The default is true.
2854+
28512855
#### `config.active_storage.routes_prefix`
28522856

28532857
Can be used to set the route prefix for the routes served by Active Storage. Accepts a string that will be prepended to the generated routes.

0 commit comments

Comments
 (0)