自定义验证检查

时间:2016-05-16 06:54:28

标签: ruby-on-rails ruby-on-rails-4 rails-activerecord

我有以下假期模型。我要做的是根据休假类型,休假数量和休假分配来核查休假数量。看起来我的'check_leave'没有因为某种原因而没有任何错误的验证。感谢任何帮助!

class Leave < ActiveRecord::Base
  belongs_to :staff
  validates :staff, :leave_type, :start_date, :end_date, :number_of_days, :approved_by, presence: true
  enum leave_type: {Medical: 0, Annual: 1, Urgent: 3, "Birth Leave": 4}
  validate :check_leave, if: -> {self.number_of_days.present?}

protected

  def check_leave
    if self.leave_type == 0
      if ( self.number_of_days = self.number_of_days + LeaveAllocation.last.medical_leave_counter ) > LeaveAllocation.last.medical_leave
        self.errors.add(:number_of_days, "Days exceeded the limit")
      end
    elsif self.leave_type == 1
      if ( self.number_of_days = self.number_of_days + LeaveAllocation.last.annual_leave_counter ) > LeaveAllocation.last.annual_leave
        self.errors.add(:number_of_days, "Days exceeded the limit")
      end
    end
  end
end

2 个答案:

答案 0 :(得分:0)

试试这个,因为leave_type是一个哈希,你需要提供正确的密钥来获取值......

更改

  if self.leave_type == 0
  ...
  elsif self.leave_type == 1
  ...
  end

  if self.leave_type[:Medical] == 0
  ...
  elsif self.leave_type[:Annual] == 1
  ...
  end

答案 1 :(得分:0)

在我看来,你只需要在你的条件中删除self.number_of_days的赋值(两者都有)。如果我理解你在寻找什么,那就应该这样做:

def check_leave
  if self.leave_type == 0
    if (self.number_of_days + LeaveAllocation.last.medical_leave_counter) > LeaveAllocation.last.medical_leave
      errors.add(:number_of_days, "Days exceeded the limit")
    end
  elsif self.leave_type == 1
    if (self.number_of_days + LeaveAllocation.last.annual_leave_counter) > LeaveAllocation.last.annual_leave
      errors.add(:number_of_days, "Days exceeded the limit")
    end
  end
end

我还从self.删除了errors.add,因为没有理由拥有它。您可以删除此方法中self.的所有用法,但如果存在我不知道的冲突,我会留给您。

我创建了一个简短的测试程序来测试我创建的一些代表性数据的条件。测试程序在这里:

test_data = [
  { leave_type: 0, number_of_days:  2, leave_counter: 2, leave_limit: 13, result: true },
  { leave_type: 0, number_of_days: 12, leave_counter: 2, leave_limit: 13, result: false },
  { leave_type: 0, number_of_days: 12, leave_counter: 2, leave_limit: 14, result: true },
  { leave_type: 1, number_of_days:  5, leave_counter: 1, leave_limit:  9, result: true },
  { leave_type: 1, number_of_days: 12, leave_counter: 2, leave_limit: 13, result: false },
  { leave_type: 1, number_of_days: 12, leave_counter: 2, leave_limit: 14, result: true },
]

def check_leave(leave_type, number_of_days, leave_counter, leave_limit)
  if leave_type == 0
    if (number_of_days + leave_counter) > leave_limit
      return false
    end
  elsif leave_type == 1
    if (number_of_days + leave_counter) > leave_limit
      return false
    end
  end

  return true
end

test_data.each_with_index do |test, i|
  puts "#{check_leave(test[:leave_type], test[:number_of_days], test[:leave_counter], test[:leave_limit]) == test[:result] ? 'Pass' : 'Fail'}: #{i}"
end

结果如下:

Pass: 0
Pass: 1
Pass: 2
Pass: 3
Pass: 4
Pass: 5

这是6个特定的测试条件,它们确认了2个leave_types中的每一个的以下3个条件:

  • (number_of_days + leave_counter)&lt; leave_limit(希望验证通过)
  • (number_of_days + leave_counter)&gt; leave_limit(期望验证失败)
  • (number_of_days + leave_counter)== leave_limit(希望验证通过)

这表示检查正常。由于测试数据不好,几乎可以肯定无法在预期时进行验证。此验证中使用的4个字段中的任何一个都可能不好:

  • leave_type
  • NUMBER_OF_DAYS
  • leave_counter
  • leave_limit

唯一剩下的步骤是确认应用程序测试中使用的数据,以确认报告不起作用的验证。至少需要一组测试数据(4个必填字段的值)才能继续下去。