请帮我把这个Rails代码弄干。我必须遗漏一些非常明显的东西

时间:2010-10-20 12:27:02

标签: ruby-on-rails validation dry

这个rails验证代码非常丑陋,必须有更好的方法来实现它。

简短的故事是我的用户正在从测试结果中输入数据。每个特定测试有4个测量。通常用户输入所有5个测试(20个测量),但并不总是需要。我只需要检查一下,如果测试人员开始输入测试数据,他会在该测试中输入所有4个测量值。

(稍后我将制作验证课程,但我正在使用它来开始)

编辑:为了澄清,'测试'是对材料的测量(与软件无关)。测试人员将数据记录在纸上,然后将其输入此应用程序

def must_input_full_test

  #test Top Section
  if top_section_1 || top_section_2 || top_section_3 || top_section_4
    found_at_least_one = true
    #at least one is present; now check if any are empty
    if top_section_1.nil?
      errors.add :top_section_1, "Must fill in four values for a test"
    end
    if top_section_2.nil?
      errors.add :top_section_2, "Must fill in four values for a test"
    end
    if top_section_3.nil?
      errors.add :top_section_3, "Must fill in four values for a test"
    end
    if top_section_4.nil?
      errors.add :top_section_4, "Must fill in four values for a test"
    end
  end

  #test Bottom Section
  if bottom_section_1 || bottom_section_2 || bottom_section_3 || bottom_section_4
    found_at_least_one = true
    #at least one is present; now check if any are empty
    if bottom_section_1.nil?
      errors.add :bottom_section_1, "Must fill in four values for a test"
    end
    if bottom_section_2.nil?
      errors.add :bottom_section_2, "Must fill in four values for a test"
    end
    if bottom_section_3.nil?
      errors.add :bottom_section_3, "Must fill in four values for a test"
    end
    if bottom_section_4.nil?
      errors.add :bottom_section_4, "Must fill in four values for a test"
    end
  end

  #test Bottom
  if bottom_1 || bottom_2 || bottom_3 || bottom_4
    found_at_least_one = true
    #at least one is present; now check if any are empty
    if bottom_1.nil?
      errors.add :bottom_1, "Must fill in four values for a test"
    end
    if bottom_2.nil?
      errors.add :bottom_2, "Must fill in four values for a test"
    end
    if bottom_3.nil?
      errors.add :bottom_3, "Must fill in four values for a test"
    end
    if bottom_4.nil?
      errors.add :bottom_4, "Must fill in four values for a test"
    end
  end

  #test Middle Section
  if middle_1 || middle_2 || middle_3 || middle_4
    found_at_least_one = true
    #at least one is present; now check if any are empty
    if middle_1.nil?
      errors.add :middle_1, "Must fill in four values for a test"
    end
    if middle_2.nil?
      errors.add :middle_2, "Must fill in four values for a test"
    end
    if middle_3.nil?
      errors.add :middle_3, "Must fill in four values for a test"
    end
    if middle_4.nil?
      errors.add :middle_4, "Must fill in four values for a test"
    end
  end

  #test Top
  if top_1 || top_2 || top_3 || top_4
    found_at_least_one = true
    #at least one is present; now check if any are empty
    if top_1.nil?
      errors.add :top_1, "Must fill in four values for a test"
    end
    if top_2.nil?
      errors.add :top_2, "Must fill in four values for a test"
    end
    if top_3.nil?
      errors.add :top_3, "Must fill in four values for a test"
    end
    if top_4.nil?
      errors.add :top_4, "Must fill in four values for a test"
    end
  end

  if !found_at_least_one
    errors.add :middle_1, "Must fill in at least some test data"
  end
end

1 个答案:

答案 0 :(得分:1)

您可以更改正在测试的代码吗?最好有这样的东西(在JSON中):

{
  'top' : [section1, section2, section3, section4],
  'middle' : [section1, section2, section3, section4],
  'bottom' : [section1, section2, section3, section4]
}

因此,您将拥有密钥:top:middle:bottom(或您需要的任何内容)的哈希值,并且该哈希值的每个值都将是一个包含四个值的数组,这与您目前获得的_1_2_3_4相对应。

使用这样的结构,您可以轻松地迭代它。


编辑:由于无法更改模型,因此您需要弯曲测试以适应。使用我概述的技术,您可以创建与您的不同模型属性对应的字符串,然后使用read_attribute(attr_string)来获取属性值。如果您在执行此操作时遇到问题,我认为您需要提出一个新问题。