Class: ActiveModel::Validations::AtLeastOnePresentValidator

Inherits:
ActiveModel::Validator
  • Object
show all
Defined in:
lib/activemodel-validators/at_least_one_present_validator.rb

Instance Method Summary collapse

Instance Method Details

#validate(record) ⇒ Object



4
5
6
7
8
9
10
11
# File 'lib/activemodel-validators/at_least_one_present_validator.rb', line 4

def validate(record)
  attr_names = options[:at_least_one_of]
  #puts %(attr_names=#{attr_names.inspect})
  unless attr_names.map {|attr_name| record[attr_name]}.any?(&:present?)
    record.errors.add :base, options[:message] || :at_least_one_field_must_be_present,
      attr_names_list: attr_names.to_sentence(two_words_connector: ' or ', last_word_connector: ', or ')
  end
end