Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Loschcode/74df1062f0460df3c515221b7b48d3e7 to your computer and use it in GitHub Desktop.
Save Loschcode/74df1062f0460df3c515221b7b48d3e7 to your computer and use it in GitHub Desktop.
# HTML
<ul>
<% Category.roots.each do |category| %>
<li class="col-md-2">
<a href="#"><%= category.name %></a>
<% if !category.children.empty? %>
<ul>
<%= category.decorate.draw_tree %> <!-- TODO : refacto this, here we have very bad decorator methodology /!\ !-->
</ul>
<% end %>
</li>
<% end %>
</ul>
# DECORATOR
def draw_tree
=begin
children.each_with_object({}) do |child, memo|
memo[:name] = child.children.any? ? child.decorate.draw_tree : {}
end
=end
children.inject("") do |memo, child| # We should refacto this code and put the HTML in a view
memo << "<li class=\"dir\"><a href=\"#\">#{child.name}</a>"
memo << "<ul>#{child.decorate.draw_tree}</ul>" if not child.children.empty?
memo << "</li>"
end.html_safe
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment