Class: Blacklight::Component::EngineCompiler

Inherits:
ViewComponent::Compiler
  • Object
show all
Defined in:
lib/blacklight/component.rb

Instance Method Summary collapse

Instance Method Details

#templatesObject

ViewComponent::Compiler locates and caches templates from sidecar files to the component source file. While this is sensible in a Rails application, it prevents component templates defined in an Engine from being overridden by an installing application without subclassing the component, which may also require modifying any partials rendering the component. This subclass of compiler overrides the template location algorithm to take the sidecar file names from the Engine, but look to see if a file of the same name existing in the installing application (ie, under Rails.root). If the latter exists, this compiler will cache that template instead of the engine-defined file; if not, the compiler will fall back to the engine-defined file.



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

def templates
  @templates ||= begin
    extensions = ActionView::Template.template_handler_extensions

    component_class.sidecar_files(extensions).each_with_object([]) do |path, memo|
      pieces = File.basename(path).split(".")
      app_path = Rails.root.join(path.slice(path.index(component_class.view_component_path)..-1).to_s).to_s

      memo << {
        path: File.exist?(app_path) ? app_path : path,
        variant: pieces.second.split("+").second&.to_sym,
        handler: pieces.last
      }
    end
  end
end