class ViewComponent::Base

def sidecar_files(extensions)

Parameters:
  • extensions (Array) -- Extensions of which to return matching sidecar files.
def sidecar_files(extensions)
  return [] unless source_location
  extensions = extensions.join(",")
  # view files in a directory named like the component
  directory = File.dirname(source_location)
  filename = File.basename(source_location, ".rb")
  component_name = name.demodulize.underscore
  # Add support for nested components defined in the same file.
  #
  # for example
  #
  # class MyComponent < ViewComponent::Base
  #   class MyOtherComponent < ViewComponent::Base
  #   end
  # end
  #
  # Without this, `MyOtherComponent` will not look for `my_component/my_other_component.html.erb`
  nested_component_files =
    if name.include?("::") && component_name != filename
      Dir["#{directory}/#{filename}/#{component_name}.*{#{extensions}}"]
    else
      []
    end
  # view files in the same directory as the component
  sidecar_files = Dir["#{directory}/#{component_name}.*{#{extensions}}"]
  sidecar_directory_files = Dir["#{directory}/#{component_name}/#{filename}.*{#{extensions}}"]
  (sidecar_files - [source_location] + sidecar_directory_files + nested_component_files).uniq
end