Class: Decidim::AssetRouter::Storage
- Inherits:
-
Object
- Object
- Decidim::AssetRouter::Storage
- Defined in:
- lib/decidim/asset_router/storage.rb
Overview
Storage asset router provides global access to the asset routes for assets saved through ActiveStorage. This handles the different cases for routing to the remote routes when using an assets CDN or to local routes when using the local disk storage driver.
Note that when the assets are stored in a remote storage service, such as Amazon S3, Google Cloud Storage or Azure Storage, this generates the asset URL directly to the storage service itself bypassing the Rails server and saving CPU time from serving the asset redirect requests. This causes a significant performance improvement on pages that display a lot of images. It will also produce a less significant performance improvement when using the local disk storage because in this situation, the images are served using one request instead of two when served directly from the storage service rather than through the asset redirect URL.
When implementing changes to the logic, please keep the remote storage options and performance implications in mind because the specs for this utility do not cover the remote storage options because the extra configuration needed to test, the service itself needed for testing and the extra dependency overhead for adding these remote storage gems when they are not needed.
Instance Method Summary collapse
-
#initialize(asset) ⇒ Storage
constructor
Initializes the router.
-
#url ⇒ String
Generates the correct URL to the asset with the provided options.
Constructor Details
#initialize(asset) ⇒ Storage
Initializes the router.
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/decidim/asset_router/storage.rb', line 31 def initialize(asset) @asset = asset @blob = case asset when ActiveStorage::Blob asset else asset&.blob end end |
Instance Method Details
#url ⇒ String
Generates the correct URL to the asset with the provided options.
47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/decidim/asset_router/storage.rb', line 47 def url(**) case asset when ActiveStorage::Attached ensure_current_host(asset.record, **) blob_url(**) when ActiveStorage::Blob blob_url(**) else # ActiveStorage::VariantWithRecord, ActiveStorage::Variant ensure_current_host(nil, **) representation_url(**) end end |