Class: CountValidator

Inherits:
ActiveModel::Validations::LengthValidator
  • Object
show all
Defined in:
app/validators/count_validator.rb

Constant Summary collapse

MESSAGES =
{
  :wrong_length => :count_invalid,
  :too_short => :count_greater_than_or_equal_to,
  :too_long => :count_less_than_or_equal_to
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ CountValidator

Returns a new instance of CountValidator.



8
9
10
11
# File 'app/validators/count_validator.rb', line 8

def initialize(options)
  options = options.reverse_merge(MESSAGES)
  super
end

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



13
14
15
16
# File 'app/validators/count_validator.rb', line 13

def validate_each(record, attribute, value)
  existing_records = record.send(attribute).reject(&:marked_for_destruction?)
  super(record, attribute, existing_records)
end