Sha256: fc28c97508a57dd11803146c8829ba2bf5d51f4116a48d9b9ff37d8fd87ca6de

Contents?: true

Size: 945 Bytes

Versions: 5

Compression:

Stored size: 945 Bytes

Contents

# ActiveModel 必须包含type或class_name_type(plan_type)属性
class DeadlineValidator < ActiveModel::EachValidator
  def validate_each(record, attribute, value)
    type = get_type(record)
    return unless type

    names = []
    case type.to_sym
    when :daily
      names = [:hour, :minute]
    when :weekly, :monthly
      names = [:hour, :minute, :day]
    when :quarterly, :yearly
      names = [:hour, :minute, :day, :month]
    end

    validate_deadline_of(record, attribute, value, names)
  end

  private
  def get_type(record)
    return record.type if record.respond_to?(:type)

    record.send("#{record.class.name.demodulize.underscore}_type")
  end

  def validate_deadline_of(record, attribute, value, names)
    names = [names] unless names.is_a? Array

    names.each do |name|
      name = "deadline_#{name}".to_sym
      record.errors[attribute] << ("#{name} is required") if value[name].blank?
    end
  end

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
task-manager-0.1.7 lib/task-manager/deadline_validator.rb
task-manager-0.1.6 lib/task-manager/deadline_validator.rb
task-manager-0.1.5 lib/task-manager/deadline_validator.rb
task-manager-0.1.4 lib/task-manager/deadline_validator.rb
task-manager-0.1.3 lib/task-manager/deadline_validator.rb