我应该向Helper或Model或......添加字符串条件方法吗?

时间:2016-10-11 04:32:15

标签: ruby-on-rails

这样做的最佳方式是什么?

我在视图<%= badges(challenge) %><%= challenge.badges %>中有这个,具体取决于我是否将badges放在帮助器或模型中。

我被告知要把它放在帮手里。我把它放进去就像这样:

module ChallengesHelper
  def badges(challenge)
    if challenge.name == "Read 20 Min"
      ActionController::Base.helpers.image_tag("read.png", class: "gold-star")
    elsif challenge.name == "Exercise 20 Min"
      ActionController::Base.helpers.image_tag("exercise.png", class: "gold-star")
    elsif challenge.name == "Meditate 10 Min"
      ActionController::Base.helpers.image_tag("meditate.png", class: "gold-star")
    elsif challenge.name == "Stretch 5 Min"
      ActionController::Base.helpers.image_tag("stretch.png", class: "gold-star")
    elsif challenge.name == "Write 500 Words"
      ActionController::Base.helpers.image_tag("write.png", class: "gold-star")
    elsif challenge.name == "Walk 5,000 Steps"
      ActionController::Base.helpers.image_tag("walk.png", class: "gold-star")
    elsif challenge.name == "Eat Fruit & Veg"
      ActionController::Base.helpers.image_tag("fruit-and-vegetable.png", class: "gold-star")
    elsif challenge.name == "Plan Day"
      ActionController::Base.helpers.image_tag("plan.png", class: "gold-star")
    elsif challenge.name == "After Waking, Guzzle Water"
      ActionController::Base.helpers.image_tag("water.png", class: "gold-star")
    elsif challenge.name == "Track Food Consumption"
      ActionController::Base.helpers.image_tag("track-food.png", class: "gold-star")
    elsif challenge.name == "Random Act of Kindness"
      ActionController::Base.helpers.image_tag("random-kindness.png", class: "gold-star")
    elsif challenge.name == "Write 3 Gratitudes"
      ActionController::Base.helpers.image_tag("gratitude.png", class: "gold-star")
    elsif challenge.name == "Juice Fast"
      ActionController::Base.helpers.image_tag("juice.png", class: "gold-star")
    elsif challenge.name == "Not Smoke"
      ActionController::Base.helpers.image_tag("not-smoke.png", class: "gold-star")
    elsif challenge.name == "Not Drink Alcohol"
      ActionController::Base.helpers.image_tag("not-drink.png", class: "gold-star")
    # GOAL CHALLENGES
    elsif challenge.name == "Live Abroad"
      ActionController::Base.helpers.image_tag("stretch.png", class: "gold-star")
    elsif challenge.name == "Long Road Trip"
      ActionController::Base.helpers.image_tag("stretch.png", class: "gold-star")
    elsif challenge.name == "Tour Capital Building"
      ActionController::Base.helpers.image_tag("stretch.png", class: "gold-star")
    elsif challenge.name == "Karaoke"
      ActionController::Base.helpers.image_tag("stretch.png", class: "gold-star")
    elsif challenge.name == "See New York Skyline"
      ActionController::Base.helpers.image_tag("stretch.png", class: "gold-star")
    elsif challenge.name == "Run 5K"
      ActionController::Base.helpers.image_tag("stretch.png", class: "gold-star")
    elsif challenge.name == "Write Memoir"
      ActionController::Base.helpers.image_tag("stretch.png", class: "gold-star")
    elsif challenge.name == "Lose 10 Pounds"
      ActionController::Base.helpers.image_tag("stretch.png", class: "gold-star")
    elsif challenge.name == "Join Club"
      ActionController::Base.helpers.image_tag("stretch.png", class: "gold-star")
    elsif challenge.name == "Skydive"
      ActionController::Base.helpers.image_tag("stretch.png", class: "gold-star")
    elsif challenge.name == "Start a Blog"
      ActionController::Base.helpers.image_tag("stretch.png", class: "gold-star")
    elsif challenge.name == "Donate $100 to Charity"
      ActionController::Base.helpers.image_tag("stretch.png", class: "gold-star")
    elsif challenge.name == "Create Independent Income Stream"
      ActionController::Base.helpers.image_tag("stretch.png", class: "gold-star")
    elsif challenge.name == "Paint a Picture"
      ActionController::Base.helpers.image_tag("stretch.png", class: "gold-star")
    elsif challenge.name == "Give a Public Speech"
      ActionController::Base.helpers.image_tag("stretch.png", class: "gold-star")
    else
      ActionController::Base.helpers.image_tag("gold-star-maze.png", class: "gold-star")
    end
  end
end

我只是担心,因为它似乎比帮助模型更“罗嗦”。

  def badges
    if name == "Read 20 Min"
      ActionController::Base.helpers.image_tag("read.png", class: "gold-star")
    elsif name == "Exercise 20 Min"
      ActionController::Base.helpers.image_tag("exercise.png", class: "gold-star")
    elsif name == "Meditate 10 Min"
      ActionController::Base.helpers.image_tag("meditate.png", class: "gold-star")
    elsif name == "Stretch 5 Min"
      ActionController::Base.helpers.image_tag("stretch.png", class: "gold-star")
    elsif name == "Write 500 Words"
      ActionController::Base.helpers.image_tag("write.png", class: "gold-star")
    elsif name == "Walk 5,000 Steps"
      ActionController::Base.helpers.image_tag("walk.png", class: "gold-star")
    elsif name == "Eat Fruit & Veg"
      ActionController::Base.helpers.image_tag("fruit-and-vegetable.png", class: "gold-star")
    elsif name == "Plan Day"
      ActionController::Base.helpers.image_tag("plan.png", class: "gold-star")
    elsif name == "After Waking, Guzzle Water"
      ActionController::Base.helpers.image_tag("water.png", class: "gold-star")
    elsif name == "Track Food Consumption"
      ActionController::Base.helpers.image_tag("track-food.png", class: "gold-star")
    elsif name == "Random Act of Kindness"
      ActionController::Base.helpers.image_tag("random-kindness.png", class: "gold-star")
    elsif name == "Write 3 Gratitudes"
      ActionController::Base.helpers.image_tag("gratitude.png", class: "gold-star")
    elsif name == "Juice Fast"
      ActionController::Base.helpers.image_tag("juice.png", class: "gold-star")
    elsif name == "Not Smoke"
      ActionController::Base.helpers.image_tag("not-smoke.png", class: "gold-star")
    elsif name == "Not Drink Alcohol"
      ActionController::Base.helpers.image_tag("not-drink.png", class: "gold-star")
    # GOAL CHALLENGES
    elsif name == "Live Abroad"
      ActionController::Base.helpers.image_tag("stretch.png", class: "gold-star")
    elsif name == "Long Road Trip"
      ActionController::Base.helpers.image_tag("stretch.png", class: "gold-star")
    elsif name == "Tour Capital Building"
      ActionController::Base.helpers.image_tag("stretch.png", class: "gold-star")
    elsif name == "Karaoke"
      ActionController::Base.helpers.image_tag("stretch.png", class: "gold-star")
    elsif name == "See New York Skyline"
      ActionController::Base.helpers.image_tag("stretch.png", class: "gold-star")
    elsif name == "Run 5K"
      ActionController::Base.helpers.image_tag("stretch.png", class: "gold-star")
    elsif name == "Write Memoir"
      ActionController::Base.helpers.image_tag("stretch.png", class: "gold-star")
    elsif name == "Lose 10 Pounds"
      ActionController::Base.helpers.image_tag("stretch.png", class: "gold-star")
    elsif name == "Join Club"
      ActionController::Base.helpers.image_tag("stretch.png", class: "gold-star")
    elsif name == "Skydive"
      ActionController::Base.helpers.image_tag("stretch.png", class: "gold-star")
    elsif name == "Start a Blog"
      ActionController::Base.helpers.image_tag("stretch.png", class: "gold-star")
    elsif name == "Donate $100 to Charity"
      ActionController::Base.helpers.image_tag("stretch.png", class: "gold-star")
    elsif name == "Create Independent Income Stream"
      ActionController::Base.helpers.image_tag("stretch.png", class: "gold-star")
    elsif name == "Paint a Picture"
      ActionController::Base.helpers.image_tag("stretch.png", class: "gold-star")
    elsif name == "Give a Public Speech"
      ActionController::Base.helpers.image_tag("stretch.png", class: "gold-star")
    else
      ActionController::Base.helpers.image_tag("gold-star-maze.png", class: "gold-star")
    end
  end

为什么一个地方比另一个好?对于类似这样的事情来说,最好的做法是什么呢?

4 个答案:

答案 0 :(得分:4)

最佳方式只是删除这个可怕的方法并将映射直接存储在模型中(因为它看起来很静态)。像这样:

Challenge.create(name: 'Read 20 Min', badge: 'read.png')

辅助方法变得更加简单

def badge_tag(challenge)
  ActionController::Base.helpers.image_tag(challenge.badge, class: "gold-star")
end

那就是它。

可以将此方法放在模型中,但你不应该,因为它是纯粹的表示逻辑,与模型无关。我,我在这里使用presenter而不是帮助者。

答案 1 :(得分:1)

在我看来,在Helper中有这样的东西要好得多。模型中的代码较少,您应该只拥有模型本身所需的逻辑。视图不是模型本身的逻辑,所以这就是为什么帮助者在那里。

第二,更好地使用case when而不是if elsif. 你可以尝试使用draper gem。 Draper

答案 2 :(得分:1)

您可以采取一些措施使其更加结构化。

  1. 首先,正如其他人所说的那样,代码正在生成视图代码并且不属于模型,所以将它放在帮助器中会更有意义。

  2. DRY。

  3. 而不是

    if name == "Read 20 Min"
                  ActionController::Base.helpers.image_tag("read.png", class: "gold-star")
        .
        .
        .
    

    应该只设置要修改的属性并将调用放在一行......

    if name == "Read 20 Min"
          image = "read.png"
    else
    .
    .
    end
    
    ActionController::Base.helpers.image_tag(image, class: "gold-star")
    

    这将更易于维护,例如您可能决定更改类,只需要修改一行。

    1. 考虑将图像名称添加到挑战模型中,然后根本不需要对描述进行测试。

答案 3 :(得分:1)

将此代码保留在助手中的另一种方法是使用哈希声明常量,并使用它来避免switchif s:

module ChallengesHelper
  CHALLENGE_IMAGES = {
    'Read 20 Min' => 'read.png',
    'Exercise 20 Min' => 'exercise.png',
    'Meditate 10 Min' => 'meditate.png',
    'Stretch 5 Min' => 'stretch.png',
    'Write 500 Words' => 'write.png',
    'Walk 5,000 Steps' => 'walk.png',
    'Eat Fruit & Veg' =>  'fruit-and-vegetable.png',
    'Plan Day' => 'plan.png',
    'After Waking, Guzzle Water' => 'water.png',
    'Track Food Consumption' => 'track-food.png',
    'Random Act of Kindness' => 'random-kindness.png',
    'Write 3 Gratitudes' => 'gratitude.png',
    'Juice Fast' => 'juice.png',
    'Not Smoke' => 'not-smoke.png',
    'Not Drink Alcohol' =>  'not-drink.png',
    'Live Abroad' => 'stretch.png',
    'Long Road Trip' => 'stretch.png',
    'Tour Capital Building' => 'stretch.png',
    'Karaoke' => 'stretch.png',
    'See New York Skyline' => 'stretch.png',
    'Run 5K' => 'stretch.png',
    'Write Memoir' => 'stretch.png',
    'Lose 10 Pounds' => 'stretch.png',
    'Join Club' => 'stretch.png',
    'Skydive' => 'stretch.png',
    'Start a Blog' => 'stretch.png',
    'Donate $100 to Charity' => 'stretch.png',
    'Create Independent Income Stream' => 'stretch.png',
    'Paint a Picture' => 'stretch.png',
    'Give a Public Speech' => 'stretch.png'
  }.freeze

  def badges(challenge)
    ActionController::Base.helpers.image_tag(CHALLENGE_IMAGES[challenge] || 'gold-star-maze.png', class: "gold-star")
  end
end