Method: Webhookdb::Postgres::Validations#validates_all_or_none

Defined in:
lib/webhookdb/postgres/validations.rb

#validates_all_or_none(*cols, predicate: :nil?) ⇒ Object

Ensures that either all of the passed columns are nil or none of them are.



15
16
17
18
19
20
21
22
# File 'lib/webhookdb/postgres/validations.rb', line 15

def validates_all_or_none(*cols, predicate: :nil?)
  truthy_cols = cols.find_all { |col| !self[col].send(predicate) }

  return if truthy_cols.empty? || truthy_cols.length == cols.length

  msg = "the columns #{cols[1..].join(', ')} must all be set or all be #{predicate.to_s.delete_suffix('?')}"
  self.errors.add(cols.first, msg)
end