Skip to content

Ways to reduce boilerplate? #408

Closed Answered by MatheusRich
mockdeep asked this question in General
Discussion options

You must be logged in to vote

I've created this helper. I don't know if its 100% bullet proof, tho. Any thoughts @jaredcwhite?

module ComponentsHelper
  # Shorthand for rendering view_components
  #
  # Usage:
  #
  #   <%= component :section, title: 'Greeting', classes: 'my--1' do %>
  #     <p class="Txt--center">Hello world</p>
  #   <% end %>
  def component(name, *args, &block)
    component_class = "#{name.to_s.camelcase}::Component".constantize

    render_component(component_class.new(*args), &block)
  end
  ruby2_keywords :component
end
<%= component :section, title: 'Greeting', classes: 'my--1' do %>

should equal

<%= render_component Section::Component.new(title: 'Greeting', classes: 'my--1') do %>

Replies: 2 comments 5 replies

Comment options

You must be logged in to vote
1 reply
@mockdeep
Comment options

Comment options

You must be logged in to vote
4 replies
@adrienpoly
Comment options

@adrienpoly
Comment options

@MatheusRich
Comment options

@adrienpoly
Comment options

Answer selected by joelhawksley
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
4 participants