Class: ViewComponentLiquid::FileSystem

Inherits:
Object
  • Object
show all
Defined in:
lib/view_component_liquid/file_system.rb

Instance Method Summary collapse

Constructor Details

#initialize(view) ⇒ FileSystem

Returns a new instance of FileSystem.



6
7
8
# File 'lib/view_component_liquid/file_system.rb', line 6

def initialize(view)
  @view = view
end

Instance Method Details

#read_template_file(template_path) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/view_component_liquid/file_system.rb', line 10

def read_template_file(template_path)
  controller_path = view.controller_path

  template_name = File.basename(template_path)
  template_folders = unless template_path.include?("/")
                       [
                         "",
                         controller_path,
                         "application",
                         "shared"
                       ]
                     else
                      [File.dirname(template_path)]
                     end

  # Search Rails' configured view paths first
  result = view.view_paths.find_all(
    template_name,
    template_folders,
    true,
    lookup_details
  )

  if result.present?
    LiquidComponent.parse(result.first.source).content
  else
    # Time to look through autoload paths for component folders
    components_folders = Zeitwerk::Loader.all_dirs.select {|item| item.ends_with?("_components")}

    template = nil

    components_folders.each do |components_folder|
      tmpl = components_folder + "/#{template_path}.liquid"
      if File.exist?(tmpl)
        template = LiquidComponent.parse(File.read(tmpl)).content
        break
      end
    end

    template.presence || raise(ActionView::MissingTemplate.new(template_folders + components_folders, template_name, template_folders, false, "liquid views"))
  end
end