module RailsComponents

robots.thoughtbot.com/mygem-configure-block

meant to be mixed into a hash

Constants

COMPONENT_RESERVED_WORDS

Attributes

configuration[RW]

Public Class Methods

configure() { |configuration| ... } click to toggle source
# File lib/rails_components/configuration.rb, line 11
def self.configure
  yield(configuration) if block_given?
end

Public Instance Methods

component(component_template, text_or_locals_with_block = nil, locals = nil, &block) click to toggle source
# File lib/rails_components.rb, line 7
def component(component_template, text_or_locals_with_block = nil, locals = nil, &block)
  if RailsComponents.configuration.template_directory
    component_template = [RailsComponents.configuration.template_directory, component_template].join('/')
  end

  if block_given?
    render({ layout: component_template, locals: component_locals(text_or_locals_with_block) }, &block)
  elsif text_or_locals_with_block.is_a?(Hash) && locals.nil?
    render(layout: component_template, locals: component_locals(text_or_locals_with_block)) {}
  else
    render(layout: component_template, locals: component_locals(locals)) { text_or_locals_with_block }
  end
end
component_content_tag(props, name, content_or_options_with_block = nil, options = nil, escape = true, &block) click to toggle source

references:

# File lib/rails_components.rb, line 23
def component_content_tag(props, name, content_or_options_with_block = nil, options = nil, escape = true, &block)
  if block_given?
    content_or_options_with_block = props.merge_html(content_or_options_with_block) if content_or_options_with_block.is_a? Hash
    content_tag(name, content_or_options_with_block, options, escape, &block)
  else
    options = props.merge_html(options)
    content_tag(name, content_or_options_with_block, options, escape, &block)
  end
end

Private Instance Methods

component_locals(locals) click to toggle source

because rails does some weird stuff to make it easy to access the locals in templates, this strips out any problem-causing key names (e.g. “class”), but gives you a new local, “prop” that has access to all the locals that were passed in (including the stripped out ones)

references:

# File lib/rails_components.rb, line 42
def component_locals(locals)
  locals ||= {}
  locals.extend(HtmlHelpers)
  locals.except(*COMPONENT_RESERVED_WORDS).merge(props: locals)
end