Module: Bidi2pdfRails::Services::AssetHostManager

Defined in:
lib/bidi2pdf_rails/services/asset_host_manager.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.original_asset_hostObject

Returns the value of attribute original_asset_host.



10
11
12
# File 'lib/bidi2pdf_rails/services/asset_host_manager.rb', line 10

def original_asset_host
  @original_asset_host
end

Instance Attribute Details

#bidi2pdf_rails_current_asset_hostObject

Returns the value of attribute bidi2pdf_rails_current_asset_host.



7
8
9
# File 'lib/bidi2pdf_rails/services/asset_host_manager.rb', line 7

def bidi2pdf_rails_current_asset_host
  @bidi2pdf_rails_current_asset_host
end

Class Method Details

.override_asset_host!(config) ⇒ Object

this method has to be called within the initializer block or environment files to ensure that the asset host is overridden before any assets gems are loaded and copy the asset host from the config e.g. propshaft-rails, importmap-rails …



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/bidi2pdf_rails/services/asset_host_manager.rb', line 15

def override_asset_host!(config)
  self.original_asset_host = config.action_controller.asset_host

  Bidi2pdfRails.logger.warn "Overriding asset host #{original_asset_host}" if original_asset_host

  config.action_controller.asset_host = lambda do |source, request|
    thread_host = Thread.current.bidi2pdf_rails_current_asset_host
    if thread_host
      thread_host
    elsif original_asset_host.respond_to?(:call)
      original_asset_host.call(source, request)
    else
      original_asset_host
    end
  end
end

.reset_asset_hostObject



44
45
46
# File 'lib/bidi2pdf_rails/services/asset_host_manager.rb', line 44

def reset_asset_host
  Thread.current.bidi2pdf_rails_current_asset_host = nil
end

.set_current_asset_host(host_from_options, fallback_host_from_request) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/bidi2pdf_rails/services/asset_host_manager.rb', line 32

def set_current_asset_host(host_from_options, fallback_host_from_request)
  Thread.current.bidi2pdf_rails_current_asset_host = if host_from_options
                                                       host_from_options
                                                     elsif self.original_asset_host
                                                       self.original_asset_host
                                                     else
                                                       fallback_host_from_request
                                                     end

  Bidi2pdfRails.logger.info "Setting asset host to #{host_from_options || fallback_host_from_request} chosen #{Thread.current.bidi2pdf_rails_current_asset_host}"
end