Class: Decidim::AssetRouter::Pipeline

Inherits:
Object
  • Object
show all
Defined in:
lib/decidim/asset_router/pipeline.rb

Overview

The pipeline asset router provides global access to the asset routers for assets that are precompiled through the Rails assets pipeline (or webpack) and stored locally or in a remote CDN. This handles the configuration options for the asset routes so that they do not have to be always manually created.

Instance Method Summary collapse

Constructor Details

#initialize(asset, model: nil) ⇒ Pipeline

Initializes the router.

Parameters:

  • asset (String)

    The asset to route to

  • model (ActiveRecord::Base, nil) (defaults to: nil)

    The model that provides the organizational context. When nil, the host will be included in the URL when it is available through other configurations. Otherwise, the host will not be part of the URL.



18
19
20
21
# File 'lib/decidim/asset_router/pipeline.rb', line 18

def initialize(asset, model: nil)
  @asset = asset
  @model = model
end

Instance Method Details

#urlString

Generates the correct URL to the asset with the provided options.

Parameters:

  • options (Hash)

    The options for the URL that are the normal route options Rails route helpers accept

Returns:

  • (String)

    The full URL to the asset or when host cannot be resolved, the asset path.



29
30
31
32
33
34
# File 'lib/decidim/asset_router/pipeline.rb', line 29

def url(**)
  path = ActionController::Base.helpers.asset_pack_path(asset, **)
  return path if path.match?(%r{\A(https?:)?//})

  "#{asset_host}#{path}"
end