Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h2>Extracting helpers methods into classes (solution n°2)</h2> <p>ok to address this specific way to clean up helpers 1st I dug up this old railscast : </p> <p><a href="http://railscasts.com/episodes/101-refactoring-out-helper-object" rel="nofollow">http://railscasts.com/episodes/101-refactoring-out-helper-object</a></p> <p>At the time it inspired me to create a little tabbing system (working in one of my apps in conjunction with a state machine) :</p> <pre><code>module WorkflowHelper # takes the block def workflow_for(model, opts={}, &amp;block) yield Workflow.new(model, opts[:match], self) return false end class Workflow def initialize(model, current_url, view) @view = view @current_url = current_url @model = model @links = [] end def link_to(text, url, opts = {}) @links &lt;&lt; url url = @model.new_record? ? "" : @view.url_for(url) @view.raw "&lt;li class='#{active_class(url)}'&gt;#{@view.link_to(text, url)}&lt;/li&gt;" end private def active_class(url) 'active' if @current_url.gsub(/(edit|new)/, "") == url.gsub(/(edit|new)/, "") || ( @model.new_record? &amp;&amp; @links.size == 1 ) end end #class Workflow end </code></pre> <p>And my views go like this :</p> <pre><code> -workflow_for @order, :match =&gt; request.path do |w| = w.link_to "✎ Create/Edit an Order", [:edit, :admin, @order] = w.link_to "√ Decide for Approval/Price", [:approve, :admin, @order] = w.link_to "✉ Notify User of Approval/Price", [:email, :admin, @order] = w.link_to "€ Create/Edit Order's Invoice", [:edit, :admin, @order, :invoice] </code></pre> <p>As you see it's a nice way to encapsulate the logic in a class and have only one method in the helper/view space</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload