isolate_assets 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: dc92b30bb9f88b0d8f12eb3ed349f6a6f1f9decbe87a675b7200e70c8c9bd611
4
- data.tar.gz: 5aa6ea46f8f8b348f913b1656a6187c3e0875536edb066929f453d997abf7186
3
+ metadata.gz: b7a0b5e25e5067341e5e3c33c79159bbe1b9fe946efd2683a9c7a1dad13f8d9a
4
+ data.tar.gz: 9374de1fe6e919a09f742e8d0b483720f9f115e05423487ef88abd3680e1a02c
5
5
  SHA512:
6
- metadata.gz: 3bb014e542b5067a9b5543f0ae6c6a1c14e8578b1b6f8dbe2d717c304f4798ff11e7293baab555bcdcddba0162067ee172b6ccd6ddf5b92d25dfeb0bc18c2c94
7
- data.tar.gz: 96bedf91563d0e5cbea9ac08075e3b5611bdd61a8275fa366058420a7dacbeed2c04845f7c7cce7b424453a2e0638ba0879f403f168cdce2309d38ba67228e59
6
+ metadata.gz: 98df37ce2970ad2849dced942d34ba434f8e22f4a0590be3fb3b9e693fd557638793f4d88d201b963b2d976d2cefbe7c96c712b67f307c848d7fb89e0c92f325
7
+ data.tar.gz: '08b88c5fd5862e0ac159c714fef265168554a1d25fbafa60f1aef12cf6e28c34f9b13bd67e74a38f71bdc01458f960a64f904f5a4da802e57b0e1390c946f563'
data/README.md CHANGED
@@ -154,6 +154,27 @@ This exclusion relies on filtering `config.assets.paths` after engines register
154
154
  isolate_assets assets_subdir: "isolated_assets" # uses app/isolated_assets/
155
155
  ```
156
156
 
157
+ ## Non-isolated engines
158
+
159
+ `isolate_assets` assumes `isolate_namespace` and a mounted route set. For an engine that isn't isolated — one that draws its routes directly into the application router — use `IsolateAssets.register` instead, then draw the asset route yourself with whatever path and name you want:
160
+
161
+ ```ruby
162
+ # lib/my_engine.rb
163
+ module MyEngine
164
+ class Engine < ::Rails::Engine; end
165
+ Assets = IsolateAssets.register(namespace: self, engine: Engine, route_name: :my_engine_asset)
166
+ end
167
+ ```
168
+
169
+ ```ruby
170
+ # config/routes.rb (drawn into the application router)
171
+ Rails.application.routes.draw do
172
+ MyEngine::Assets.draw(self, "/my_engine/assets")
173
+ end
174
+ ```
175
+
176
+ This gives you the same namespaced helpers as the isolated path — `MyEngine.stylesheet_link_tag "app"`, `MyEngine.javascript_include_tag "app"`, etc. — fingerprinted against your chosen route.
177
+
157
178
  ## Requirements
158
179
 
159
180
  - Ruby 3.2+
@@ -2,7 +2,8 @@
2
2
 
3
3
  module IsolateAssets
4
4
  class Assets
5
- attr_reader :engine, :assets_subdir
5
+ attr_reader :engine, :assets_subdir, :route_name
6
+ attr_accessor :controller
6
7
 
7
8
  ASSET_DIRECTORIES = {
8
9
  "js" => "javascripts",
@@ -54,12 +55,20 @@ module IsolateAssets
54
55
  "ogv" => "video/ogg"
55
56
  }.freeze
56
57
 
57
- def initialize(engine:, assets_subdir: "assets")
58
+ def initialize(engine:, assets_subdir: "assets", route_name: :isolated_asset, url_helpers: nil)
58
59
  @engine = engine
59
60
  @assets_subdir = assets_subdir
61
+ @route_name = route_name
62
+ @url_helpers = url_helpers || -> { engine.routes.url_helpers }
60
63
  @fingerprints = {}
61
64
  end
62
65
 
66
+ # Draws the catch-all asset route into the given router (the application
67
+ # router for non-isolated engines, the engine's own for isolated ones).
68
+ def draw(mapper, path)
69
+ mapper.get "#{path}/*file", to: controller.action(:show), as: route_name
70
+ end
71
+
63
72
  def asset_path(source, type)
64
73
  directory = ASSET_DIRECTORIES[type.to_s]
65
74
  return nil unless directory
@@ -70,7 +79,11 @@ module IsolateAssets
70
79
 
71
80
  def asset_url(source, type)
72
81
  fingerprint_value = fingerprint(source, type)
73
- engine.routes.url_helpers.isolated_asset_path("#{source}.#{normalize_type(type)}", v: fingerprint_value)
82
+ @url_helpers.call.public_send(
83
+ "#{route_name}_path",
84
+ "#{source}.#{normalize_type(type)}",
85
+ v: fingerprint_value,
86
+ )
74
87
  end
75
88
 
76
89
  def fingerprint(source, type)
@@ -2,11 +2,6 @@
2
2
 
3
3
  module IsolateAssets
4
4
  module EngineExtension
5
- HELPER_METHODS = %i[
6
- stylesheet_link_tag javascript_include_tag javascript_importmap_tags
7
- asset_path image_path image_tag font_path audio_path audio_tag video_path video_tag
8
- ].freeze
9
-
10
5
  def isolate_assets(assets_subdir: "assets")
11
6
  engine_class = self
12
7
 
@@ -36,35 +31,10 @@ module IsolateAssets
36
31
 
37
32
  initializer "#{engine_name}.isolate_assets", before: :set_routes_reloader do
38
33
  assets = IsolateAssets::Assets.new(engine: engine_class, assets_subdir: assets_subdir)
39
-
40
- # Subclass the controller so that multiple engines using this gem get their own controller
41
- controller_class = Class.new(IsolateAssets::Controller)
42
- controller_class.isolated_assets = assets
43
-
44
- # Create helper module for inclusion in engine's ApplicationHelper
45
- helper_module = Module.new do
46
- define_method(:isolated_assets) { assets }
47
- include IsolateAssets::Helper
48
- end
34
+ controller_class = IsolateAssets.build_controller(assets)
49
35
 
50
36
  if engine_class.respond_to?(:railtie_namespace) && engine_class.railtie_namespace
51
- namespace = engine_class.railtie_namespace
52
-
53
- # Expose isolated_assets and helper module
54
- namespace.singleton_class.define_method(:isolated_assets) { assets }
55
- namespace.singleton_class.define_method(:isolated_assets_helper) { helper_module }
56
-
57
- # Define helper methods directly on namespace (e.g., Dummy.stylesheet_link_tag)
58
- helper_context = Class.new do
59
- include IsolateAssets::Helper
60
- define_method(:isolated_assets) { assets }
61
- end.new
62
-
63
- HELPER_METHODS.each do |method_name|
64
- namespace.singleton_class.define_method(method_name) do |*args, **kwargs, &block|
65
- helper_context.send(method_name, *args, **kwargs, &block)
66
- end
67
- end
37
+ IsolateAssets.expose_helpers(engine_class.railtie_namespace, assets)
68
38
  end
69
39
 
70
40
  engine_class.routes.prepend do
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module IsolateAssets
4
- VERSION = "0.3.0"
4
+ VERSION = "0.4.0"
5
5
  end
@@ -9,6 +9,55 @@ module IsolateAssets
9
9
  autoload :Controller, "isolate_assets/controller"
10
10
  autoload :Helper, "isolate_assets/helper"
11
11
  autoload :EngineExtension, "isolate_assets/engine_extension"
12
+
13
+ HELPER_METHODS = %i[
14
+ stylesheet_link_tag javascript_include_tag javascript_importmap_tags
15
+ asset_path image_path image_tag font_path audio_path audio_tag video_path video_tag
16
+ ].freeze
17
+
18
+ # Wires up an engine that draws its asset route into the application router
19
+ # rather than mounting an isolated route set. Returns the Assets handle; draw
20
+ # the route from the engine's routes file with `assets.draw(self, path)`.
21
+ def self.register(namespace:, engine:, route_name:, assets_subdir: "assets")
22
+ assets = Assets.new(
23
+ engine: engine,
24
+ assets_subdir: assets_subdir,
25
+ route_name: route_name,
26
+ url_helpers: -> { Rails.application.routes.url_helpers },
27
+ )
28
+ build_controller(assets)
29
+ expose_helpers(namespace, assets)
30
+ assets
31
+ end
32
+
33
+ def self.build_controller(assets)
34
+ controller = Class.new(Controller)
35
+ controller.isolated_assets = assets
36
+ assets.controller = controller
37
+ controller
38
+ end
39
+
40
+ # Defines isolated_assets + the asset tag helpers (stylesheet_link_tag, etc.)
41
+ # as singleton methods on the given module, e.g. Dummy.stylesheet_link_tag.
42
+ def self.expose_helpers(namespace, assets)
43
+ helper_module = Module.new do
44
+ define_method(:isolated_assets) { assets }
45
+ include Helper
46
+ end
47
+ namespace.singleton_class.define_method(:isolated_assets) { assets }
48
+ namespace.singleton_class.define_method(:isolated_assets_helper) { helper_module }
49
+
50
+ helper_context = Class.new do
51
+ include Helper
52
+ define_method(:isolated_assets) { assets }
53
+ end.new
54
+ HELPER_METHODS.each do |method_name|
55
+ namespace.singleton_class.define_method(method_name) do |*args, **kwargs, &block|
56
+ helper_context.send(method_name, *args, **kwargs, &block)
57
+ end
58
+ end
59
+ helper_module
60
+ end
12
61
  end
13
62
 
14
63
  # Extend Rails::Engine with isolate_assets
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: isolate_assets
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Micah Geisel
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2026-01-10 00:00:00.000000000 Z
11
+ date: 2026-06-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties