serradura / rails-components

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Component-Based Rails App

Test the below examples in app/blogs/index.html.erb.

#1 PORO helper components

<tbody>
  <%= safe_join @blogs.map(&method(:blogs_table_row)) %>
</tbody>
<tbody>
  <% @blogs.each do |blog| %>
    <%= blogs_table_row(blog) %>
  <% end %>
</tbody>
<tbody>
  <% @blogs.each_with_index do |blog, index| %>
    <%=
      blogs_table_row blog, -> do
        content_tag(:strong, "Ahoy #{'!' * (index+1)}")
      end
    %>
  <% end %>
</tbody>

#2 Rails partial components

<tbody>
  <% @blogs.each do |blog| %>
    <%= component(:row, blog: blog) %>
  <% end %>
</tbody>
<tbody>
  <% @blogs.each_with_index do |blog, index| %>
    <%= component(:row, blog: blog) do %>
      <strong>Ahoy<%= '!' * (index+1) %></strong>
    <% end %>
  <% end %>
</tbody>

Extras:

Test the following snippets in rails console:

tags = Components::Tags.new(helper)

tags.br
#=> "<br />"

tags.br style: 'margin-top: 10px'
#=> "<br style=\"margin-top: 10px\" />"

tags.ul
#=> "<ul></ul>"

tags.ul [1,2,3].map &tags.method(:li)
#=> "<ul><li>1</li><li>2</li><li>3</li></ul>"

Related resources:

About


Languages

Language:Ruby 75.5%Language:HTML 16.9%Language:CSS 5.1%Language:JavaScript 2.0%Language:CoffeeScript 0.5%