aboutsummaryrefslogtreecommitdiffstats
path: root/activestorage/app/controllers/concerns/active_storage/set_current.rb
diff options
context:
space:
mode:
authorGeorge Claghorn <george.claghorn@gmail.com>2018-08-16 01:41:15 -0400
committerGeorge Claghorn <george.claghorn@gmail.com>2018-08-16 01:41:15 -0400
commite33c3cd8ccbecaca6c6af0438956431b02cb3fb2 (patch)
tree3c4285fda6f56507976227abd339395ab1e616ea /activestorage/app/controllers/concerns/active_storage/set_current.rb
parent51bdbc2d0198c666cd62d1404c18da477e5cbfbc (diff)
downloadrails-e33c3cd8ccbecaca6c6af0438956431b02cb3fb2.tar.gz
rails-e33c3cd8ccbecaca6c6af0438956431b02cb3fb2.tar.bz2
rails-e33c3cd8ccbecaca6c6af0438956431b02cb3fb2.zip
Extract ActiveStorage::SetCurrent
Provide a handy concern for custom Active Storage controllers that can't inherit from ActiveStorage::BaseController.
Diffstat (limited to 'activestorage/app/controllers/concerns/active_storage/set_current.rb')
-rw-r--r--activestorage/app/controllers/concerns/active_storage/set_current.rb15
1 files changed, 15 insertions, 0 deletions
diff --git a/activestorage/app/controllers/concerns/active_storage/set_current.rb b/activestorage/app/controllers/concerns/active_storage/set_current.rb
new file mode 100644
index 0000000000..597afe7064
--- /dev/null
+++ b/activestorage/app/controllers/concerns/active_storage/set_current.rb
@@ -0,0 +1,15 @@
+# frozen_string_literal: true
+
+# Sets the <tt>ActiveStorage::Current.host</tt> attribute, which the disk service uses to generate URLs.
+# Include this concern in custom controllers that call ActiveStorage::Blob#service_url,
+# ActiveStorage::Variant#service_url, or ActiveStorage::Preview#service_url so the disk service can
+# generate URLs using the same host, protocol, and base path as the current request.
+module ActiveStorage::SetCurrent
+ extend ActiveSupport::Concern
+
+ included do
+ before_action do
+ ActiveStorage::Current.host = request.base_url
+ end
+ end
+end