Class: IsolateAssets::Assets
- Inherits:
-
Object
- Object
- IsolateAssets::Assets
- Defined in:
- lib/isolate_assets/assets.rb
Constant Summary collapse
- ASSET_DIRECTORIES =
{ "js" => "javascripts", "javascript" => "javascripts", "css" => "stylesheets", "stylesheet" => "stylesheets", "png" => "images", "jpg" => "images", "jpeg" => "images", "gif" => "images", "svg" => "images", "webp" => "images", "ico" => "images", "woff" => "fonts", "woff2" => "fonts", "ttf" => "fonts", "otf" => "fonts", "eot" => "fonts", "mp3" => "audio", "ogg" => "audio", "wav" => "audio", "mp4" => "video", "webm" => "video", "ogv" => "video" }.freeze
- CONTENT_TYPES =
{ "js" => "application/javascript", "javascript" => "application/javascript", "css" => "text/css", "stylesheet" => "text/css", "png" => "image/png", "jpg" => "image/jpeg", "jpeg" => "image/jpeg", "gif" => "image/gif", "svg" => "image/svg+xml", "webp" => "image/webp", "ico" => "image/x-icon", "woff" => "font/woff", "woff2" => "font/woff2", "ttf" => "font/ttf", "otf" => "font/otf", "eot" => "application/vnd.ms-fontobject", "mp3" => "audio/mpeg", "ogg" => "audio/ogg", "wav" => "audio/wav", "mp4" => "video/mp4", "webm" => "video/webm", "ogv" => "video/ogg" }.freeze
Instance Attribute Summary collapse
-
#assets_subdir ⇒ Object
readonly
Returns the value of attribute assets_subdir.
-
#controller ⇒ Object
Returns the value of attribute controller.
-
#engine ⇒ Object
readonly
Returns the value of attribute engine.
-
#route_name ⇒ Object
readonly
Returns the value of attribute route_name.
Instance Method Summary collapse
- #asset_path(source, type) ⇒ Object
- #asset_url(source, type) ⇒ Object
- #content_type(type) ⇒ Object
-
#draw(mapper, path) ⇒ Object
Draws the catch-all asset route into the given router (the application router for non-isolated engines, the engine’s own for isolated ones).
- #fingerprint(source, type) ⇒ Object
-
#initialize(engine:, assets_subdir: "assets", route_name: :isolated_asset, url_helpers: nil) ⇒ Assets
constructor
A new instance of Assets.
- #javascript_files ⇒ Object
Constructor Details
#initialize(engine:, assets_subdir: "assets", route_name: :isolated_asset, url_helpers: nil) ⇒ Assets
Returns a new instance of Assets.
58 59 60 61 62 63 64 |
# File 'lib/isolate_assets/assets.rb', line 58 def initialize(engine:, assets_subdir: "assets", route_name: :isolated_asset, url_helpers: nil) @engine = engine @assets_subdir = assets_subdir @route_name = route_name @url_helpers = url_helpers || -> { engine.routes.url_helpers } @fingerprints = {} end |
Instance Attribute Details
#assets_subdir ⇒ Object (readonly)
Returns the value of attribute assets_subdir.
5 6 7 |
# File 'lib/isolate_assets/assets.rb', line 5 def assets_subdir @assets_subdir end |
#controller ⇒ Object
Returns the value of attribute controller.
6 7 8 |
# File 'lib/isolate_assets/assets.rb', line 6 def controller @controller end |
#engine ⇒ Object (readonly)
Returns the value of attribute engine.
5 6 7 |
# File 'lib/isolate_assets/assets.rb', line 5 def engine @engine end |
#route_name ⇒ Object (readonly)
Returns the value of attribute route_name.
5 6 7 |
# File 'lib/isolate_assets/assets.rb', line 5 def route_name @route_name end |
Instance Method Details
#asset_path(source, type) ⇒ Object
72 73 74 75 76 77 78 |
# File 'lib/isolate_assets/assets.rb', line 72 def asset_path(source, type) directory = ASSET_DIRECTORIES[type.to_s] return nil unless directory normalized = normalize_type(type) engine.root.join("app/#{assets_subdir}/#{directory}", "#{source}.#{normalized}") end |
#asset_url(source, type) ⇒ Object
80 81 82 83 84 85 86 87 |
# File 'lib/isolate_assets/assets.rb', line 80 def asset_url(source, type) fingerprint_value = fingerprint(source, type) @url_helpers.call.public_send( "#{route_name}_path", "#{source}.#{normalize_type(type)}", v: fingerprint_value, ) end |
#content_type(type) ⇒ Object
99 100 101 |
# File 'lib/isolate_assets/assets.rb', line 99 def content_type(type) CONTENT_TYPES[type.to_s] || "application/octet-stream" end |
#draw(mapper, path) ⇒ Object
Draws the catch-all asset route into the given router (the application router for non-isolated engines, the engine’s own for isolated ones).
68 69 70 |
# File 'lib/isolate_assets/assets.rb', line 68 def draw(mapper, path) mapper.get "#{path}/*file", to: controller.action(:show), as: route_name end |
#fingerprint(source, type) ⇒ Object
89 90 91 92 93 94 95 96 97 |
# File 'lib/isolate_assets/assets.rb', line 89 def fingerprint(source, type) cache_key = "#{source}.#{type}" if ::Rails.env.production? @fingerprints[cache_key] ||= calculate_fingerprint(source, type) else calculate_fingerprint(source, type) end end |
#javascript_files ⇒ Object
103 104 105 |
# File 'lib/isolate_assets/assets.rb', line 103 def javascript_files engine.root.glob("app/#{assets_subdir}/javascripts/**/*.js") end |