module RogerStyleGuide::Helpers::ComponentHelper

Compent helper and friends

Public Instance Methods

component(path, locals = {}, &block) click to toggle source

Ease use of components by wrapping the partial helper

This allows us to call component(“map”) which will render RogerStyleGuide.components_path/map/_map.html.erb

Calling component(“map/map”) will still work

Also simplifies passing of locals. It's just the second parameter of the component helper.

# File lib/roger_style_guide/helpers/component_helper.rb, line 12
def component(path, locals = {}, &block)
  partial_path = component_template_paths(path).find do |template_path|
    renderer.send(:find_partial, template_path)
  end

  renderer.send(:template_not_found!, :component, path) unless partial_path

  # Render the partial
  partial(partial_path, locals: locals, &block)
end
component_template_paths(path) click to toggle source
# File lib/roger_style_guide/helpers/component_helper.rb, line 23
def component_template_paths(path)
  name = File.basename(path)
  local_name = name.sub(/\A_?/, "_")
  extension = File.extname(name)[1..-1]
  name_without_extension = extension ? name.sub(/\.#{Regexp.escape(extension)}\Z/, "") : name

  dirs = RogerStyleGuide.components_paths.inject([]) do |dirs, components_path|
    dir = File.join(
      components_path,
      path.slice(0, path.size - name.size).to_s.strip
    )

    dirs += [
      # component_path/name/_name.xyz
      File.join(dir, name_without_extension, local_name),
      # component_path/name
      File.join(dir, name)
    ]
  end
end