Sha256: fa90dd754ca6b545aa9da5e6761b54ba50f0d752b339d28d6b1d2118469be664

Contents?: true

Size: 1.42 KB

Versions: 2

Compression:

Stored size: 1.42 KB

Contents

# Compent helper and friends
module RogerStyleGuide::Helpers::ComponentHelper
  # 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.
  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

  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
end

Roger::Renderer.helper RogerStyleGuide::Helpers::ComponentHelper

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
roger_style_guide-1.0.1 lib/roger_style_guide/helpers/component_helper.rb
roger_style_guide-1.0.0 lib/roger_style_guide/helpers/component_helper.rb