How to make this ruby method less ugly (nesting) -


I have a helpful method that creates a navigation link for some controllers.

  def gen_associations (controllers) content_for: leftnav do sorted_controllers = controller Return string. Newway | Content | Content & lt; & Lt; Content_tag (: h3, "associations") & lt; & Lt; Content_tag (: ul ,: class = & gt; "nav") Sorted_controllers. C | Content_tag ("li", class => ("last" if c == sorted_controllers.last)) link_to (c.humanize, eval ("admin _ # {c} _url")) end end end end end  

I do not like this deeply nested structure, and additional & lt; & Lt; and end one of the rows.

How can I write it, is not it (not in lines) and long lines (& lt; 80 characters) without it being nested?

Make it out of the outside:

  df gen_associations (controller) Sorted_controllers = controllers Source list_items = sorted_controllers.collect do | C | Link_to (c.Humanize, eval ("admin _ # {c} _url")) content_tag ("li", class => ("last" if c == sorted_controllers.last)) end end list = Content_tag (: ul, list_items.join, class = & gt; "nav") for content_: leftnav do content_tag (: h3, "associations") & lt; & Lt; End of list end  

I will probably move content_for to visible or partially and simply gen_associations () back to List .

  ... <% content_for: leftnav do%> & Lt; H3 & gt; Associations & lt; / H3 & gt; & Lt;% = gen_associations (administrators)%> & gt; & Lt;% end% & gt; ...  

Comments

Popular posts from this blog

asp.net - Javascript/DOM Why is does my form not support submit()? -

sockets - Delphi: TTcpServer, connection reset when reading -

javascript - Classic ASP "ExecuteGlobal" statement acting differently on two servers -