Module: IsolateAssets

Defined in:
lib/isolate_assets.rb,
lib/isolate_assets/assets.rb,
lib/isolate_assets/helper.rb,
lib/isolate_assets/version.rb,
lib/isolate_assets/controller.rb,
lib/isolate_assets/engine_extension.rb

Defined Under Namespace

Modules: EngineExtension, Helper Classes: Assets, Controller

Constant Summary collapse

HELPER_METHODS =
%i[
  stylesheet_link_tag javascript_include_tag javascript_importmap_tags
  asset_path image_path image_tag font_path audio_path audio_tag video_path video_tag
].freeze
VERSION =
"0.4.0"

Class Method Summary collapse

Class Method Details

.build_controller(assets) ⇒ Object



33
34
35
36
37
38
# File 'lib/isolate_assets.rb', line 33

def self.build_controller(assets)
  controller = Class.new(Controller)
  controller.isolated_assets = assets
  assets.controller = controller
  controller
end

.expose_helpers(namespace, assets) ⇒ Object

Defines isolated_assets + the asset tag helpers (stylesheet_link_tag, etc.) as singleton methods on the given module, e.g. Dummy.stylesheet_link_tag.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/isolate_assets.rb', line 42

def self.expose_helpers(namespace, assets)
  helper_module = Module.new do
    define_method(:isolated_assets) { assets }
    include Helper
  end
  namespace.singleton_class.define_method(:isolated_assets) { assets }
  namespace.singleton_class.define_method(:isolated_assets_helper) { helper_module }

  helper_context = Class.new do
    include Helper
    define_method(:isolated_assets) { assets }
  end.new
  HELPER_METHODS.each do |method_name|
    namespace.singleton_class.define_method(method_name) do |*args, **kwargs, &block|
      helper_context.send(method_name, *args, **kwargs, &block)
    end
  end
  helper_module
end

.register(namespace:, engine:, route_name:, assets_subdir: "assets") ⇒ Object

Wires up an engine that draws its asset route into the application router rather than mounting an isolated route set. Returns the Assets handle; draw the route from the engine’s routes file with ‘assets.draw(self, path)`.



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/isolate_assets.rb', line 21

def self.register(namespace:, engine:, route_name:, assets_subdir: "assets")
  assets = Assets.new(
    engine: engine,
    assets_subdir: assets_subdir,
    route_name: route_name,
    url_helpers: -> { Rails.application.routes.url_helpers },
  )
  build_controller(assets)
  expose_helpers(namespace, assets)
  assets
end