在Rails视图中重构丑陋的if / else语句

时间:2015-06-25 20:09:01

标签: ruby-on-rails

我在rails视图中有这个令人讨厌的if / else语句:

         <% if question.field_type == "text_area" %>
            <%= f.text_area :content, :class=>"form-control question-field", :data => {:question => question.id, :filter=> @filter}, :value=> question.answer(@assessment).try(:content) %>
          <% elsif question.field_type == "date" %>
            <%= f.date_select :content, { :order => [:year, :month, :day], :prompt => { :day => 'day', :month => 'month', :year=> "year" }, :end_year=> Date.today.year, :start_year => Date.today.year - 2 }, {:class => "question-field", :data => {:question => question.id, :filter=> @filter}, :value=> question.answer(@assessment).try(:content)} %>
          <% elsif question.field_type == "text_field" %>
            <%= f.text_field :content, :class=>"form-control question-field", :value=> question.answer(@assessment).try(:content), :data => {:question => question.id, :filter=> @filter} %>
         <% elsif question.field_type == "dropdown" %>
           <%= f.select :content, options_for_select(question.options), { :prompt => "Choose One..." }, :class=>"form-control question-field", :value=> question.answer(@assessment).try(:content), :data => {:question => question.id, :filter=> @filter} %>
          <% elsif question.field_type == "number" %>
           <%= f.select :content, options_for_select(1..10), {:include_blank=> true}, :class=>"form-control question-field", :value=> question.answer(@assessment).try(:content), :data => {:question => question.id, :filter=> @filter} %>
          <% elsif question.field_type == "percentage" %>
             <h2>100%</h2>
             <%= f.range_field :content, :value=> get_percentage(question), :class=> "question-field percentage", :data => {:question => question.id, :filter=> @filter}, :step => 25, :in => 0..100 %> 
          <% end %>

有没有一种好方法可以重构它以使其更好?这段代码遍布各个领域:

:class=>"form-control question-field", :value=> question.answer(@assessment).try(:content), :data => {:question => question.id, :filter=> @filter}

我是否重构为辅助方法或部分方法?

2 个答案:

答案 0 :(得分:1)

有时候模板很乱,你只能清理细节。重构为参数化部分将有所帮助。为了善良,请使用case。并考虑切换到HAML。它消除了很多视觉混乱。

<%= render 'question_field', f: f, type: question.field_type %>

然后在_question_field.erb

<%= case type %>
  <% when 'text_area' %>
    <% f.text_area :content, class: 'form-control question-field', %>
        <% data: { question: question.id, filter: @filter }, %>
        <% value: question.answer(@assessment).try(:content) %>
  <% when ... %>
<% end %>

注意常见的工业实践是选择最大线长并坚持下去:100和120很常见。此外,使用新的符号键表示法进行哈希处理。旧的勾箭太吵了。

在HAML中:

= case type
  - when 'text_area'
    - f.text_area :content, class: 'form-control question-field',
      data: { question: question.id, filter: @filter },
      value: question.answer(@assessment).try(:content)
  - when ...

答案 1 :(得分:0)

我会通过为每种可能性创建单独的部分来完全摆脱if和#then,然后你最终得到:

using namespace std;

或者使视图make helper方法更加清晰,只调用

<%= render question.field_type, locals: {question: question} %>

这个方法看起来有点像

<%= question_field(question) %>