在 Rails 上 Ruby 中重构繁重的 if 语句的有效方法是什么?

What is an efficient way to refactor a heavy if statement in Ruby on Rails?

重构繁重的 if 语句的有效方法是什么?

鼓励所有建议...

<% if @user.plan_id == 1 %>
      <span class="label label-info">Plan1</span>
  <% elsif @user.plan_id == 2 %>
      <span class="label label-warning">Plan2</span>
  <% elsif @user.plan_id == 3 %>
      <span class="label label-info">Plan3</span>
  <% elsif @user.plan_id == 4 %>
      <span class="label label-warning">Plan4</span>
  <% elsif @user.plan_id == 5 %>
      <span class="label label-info">Plan5</span>
  <% elsif @user.plan_id == 6 %>
      <span class="label label-warning">Plan6</span>
  <% elsif @user.plan_id == 7 %>
      <span class="label label-info">Plan7</span>   
  <% elsif @user.plan_id == 8 %>
      <span class="label label-warning">Plan8</span>
<% end %>

您可以使用case

<% case @user.plan_id %>
<% when 1 %>
    <span class="label label-info">CONTRIBUTOR</span>
<% when 2 %>
    <span class="label label-warning">ELITE CONTRIBUTOR</span>
<% when 3 %>
     <span class="label label-info">TECHNICIAN</span>
<% when 4 %>
     <span class="label label-warning">ELITE TECHNICIAN</span>
<% when 5 %>
     <span class="label label-info">CENTER</span>
<% when 6 %>
     <span class="label label-warning">ELITE CENTER</span>
<% when 7 %>
     <span class="label label-info">AFFAIR</span>   
<% when 8 %>
      <span class="label label-warning">ELITE AFFAIR</span>
<% end %>

plan_id 是否引用了 Plan 模型?如果没有,我建议创建一个,带有 label 属性和 elite 标志。那么,您的陈述将是:

<span class="label label-<%= @user.plan.elite ? 'warning' : 'info' %>}">
  <%= @user.plan.label %>
</span>

定义辅助方法:

def plan_hash
  {
    1 => { :klass => "label-info",    :description => "Plan1" },
    2 => { :klass => "label-warning", :description => "Plan2" },
    3 => { :klass => "label-info",    :description => "Plan3" },
    4 => { :klass => "label-warning", :description => "Plan4" },
    5 => { :klass => "label-info",    :description => "Plan5" },
    6 => { :klass => "label-warning", :description => "Plan6" },
    7 => { :klass => "label-info",    :description => "Plan7" },
    8 => { :klass => "label-warning", :description => "Plan8" }
  }
end

那么,在您看来:

<span class="label <%= plan_hash[@user.plan_id][:klass] %>"><%= plan_hash[@user.plan_id][:description] %></span>

这是一个辅助模块的工作,像这样:

module UserPlanHelper
  PLANS = {
    1 => [ "info", "Plan1" ],
    2 => [ "warning", "Plan2" ],
    3 => [ "info", "Plan3" ],
    4 => [ "warning", "Plan4" ],
    5 => [ "info", "Plan5" ],
    6 => [ "warning", "Plan6" ],
    7 => [ "info", "Plan7" ],
    8 => [ "warning", "Plan8" ]
  }

  def plan_details(plan_id)
    yield(PLANS[plan_id])
  end
end

如果在您的控制器中使用,这会将您的代码简化为:

<%= plan_details(@user.plan_id) do |label_class, label| %>
  <span class="label label-<%= label_class %>"><%= label %></span>
<%- end %>
<span class="label label-<%= @user.add_warning? ? 'warning' : 'info' %>">
  Plan<%= @user.plan_id.to_s %>
</span>

在您的用户模型中使用此方法:

def add_warning?
  self.plan_id.in?([2, 4, 6, 8])
end