module Rein::Constraint::Presence

This module contains methods for defining presence constraints.

Public Instance Methods

add_presence_constraint(*args) click to toggle source
# File lib/rein/constraint/presence.rb, line 9
def add_presence_constraint(*args)
  reversible do |dir|
    dir.up { _add_presence_constraint(*args) }
    dir.down { _remove_presence_constraint(*args) }
  end
end
remove_presence_constraint(*args) click to toggle source
# File lib/rein/constraint/presence.rb, line 16
def remove_presence_constraint(*args)
  reversible do |dir|
    dir.up { _remove_presence_constraint(*args) }
    dir.down { _add_presence_constraint(*args) }
  end
end

Private Instance Methods

_add_presence_constraint(table, attribute, options = {}) click to toggle source
# File lib/rein/constraint/presence.rb, line 25
def _add_presence_constraint(table, attribute, options = {})
  name = Util.constraint_name(table, attribute, 'presence', options)
  table = Util.wrap_identifier(table)
  attribute = Util.wrap_identifier(attribute)
  conditions = Util.conditions_with_if(
    "(#{attribute} IS NOT NULL) AND (#{attribute} !~ '^\\s*$')",
    options
  )
  sql = "ALTER TABLE #{table} ADD CONSTRAINT #{name} CHECK (#{conditions})"
  execute(Util.add_not_valid_suffix_if_required(sql, options))
end
_remove_presence_constraint(table, attribute, options = {}) click to toggle source
# File lib/rein/constraint/presence.rb, line 37
def _remove_presence_constraint(table, attribute, options = {})
  name = Util.constraint_name(table, attribute, 'presence', options)
  table = Util.wrap_identifier(table)
  execute("ALTER TABLE #{table} DROP CONSTRAINT #{name}")
end