Class: PresenceAtLeastOneValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/presence_at_least_one_validator.rb

Instance Method Summary collapse

Instance Method Details

#all_specified_attributes_are_blank?(record) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/presence_at_least_one_validator.rb', line 26

def all_specified_attributes_are_blank?(record)
  attributes.all? { |attribute| record.send(attribute).blank? }
end

#validate(record) ⇒ Object

Validates that AT LEAST ONE of the specified attributes are not blank (as defined by Object#blank?).

Examples

class Product < ActiveRecord::Base
   validates :name, :price, :presence_at_least_one => true
end

>> product = Product.new
>> product.valid?
=> false
>> [product.errors[:name], :product.errors[:price]]
=> ['presence_at_least_one translate message error', 'presence_at_least_one translate message error']

>> product.name = 'Donate a Macbook to Tomas!'
>> product.valid?
=> true


20
21
22
23
24
# File 'lib/presence_at_least_one_validator.rb', line 20

def validate(record)
  if all_specified_attributes_are_blank?(record)
    attributes.each { |attribute| record.errors.add(attribute, :presence_at_least_one) }
  end
end