Module: RogerStyleGuide::Helpers::ComponentHelper

Defined in:
lib/roger_style_guide/helpers/component_helper.rb

Overview

Compent helper and friends

Instance Method Summary collapse

Instance Method Details

#component(path, locals = {}, &block) ⇒ Object

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.



12
13
14
15
16
17
18
19
20
21
# 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) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# 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