1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Extract ActiveStorage::SetCurrent

Provide a handy concern for custom Active Storage controllers that can't inherit from ActiveStorage::BaseController.
This commit is contained in:
George Claghorn 2018-08-16 01:41:15 -04:00
parent 51bdbc2d01
commit e33c3cd8cc
3 changed files with 23 additions and 5 deletions

View file

@ -1,3 +1,8 @@
* Added the `ActiveStorage::SetCurrent` concern for custom Active Storage
controllers that can't inherit from `ActiveStorage::BaseController`.
*George Claghorn*
* Active Storage error classes like `ActiveStorage::IntegrityError` and * Active Storage error classes like `ActiveStorage::IntegrityError` and
`ActiveStorage::UnrepresentableError` now inherit from `ActiveStorage::Error` `ActiveStorage::UnrepresentableError` now inherit from `ActiveStorage::Error`
instead of `StandardError`. This permits rescuing `ActiveStorage::Error` to instead of `StandardError`. This permits rescuing `ActiveStorage::Error` to

View file

@ -1,10 +1,8 @@
# frozen_string_literal: true # frozen_string_literal: true
# The base controller for all ActiveStorage controllers. # The base class for all Active Storage controllers.
class ActiveStorage::BaseController < ActionController::Base class ActiveStorage::BaseController < ActionController::Base
protect_from_forgery with: :exception include ActiveStorage::SetCurrent
before_action do protect_from_forgery with: :exception
ActiveStorage::Current.host = request.base_url
end
end end

View file

@ -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