Module: Rein::Constraint::Presence

Includes:
ActiveRecord::ConnectionAdapters::Quoting
Included in:
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
Defined in:
lib/rein/constraint/presence.rb

Overview

This module contains methods for defining presence constraints.

Instance Method Summary collapse

Instance Method Details

#add_presence_constraint(table, attribute, options = {}) ⇒ Object



7
8
9
10
11
12
13
14
# File 'lib/rein/constraint/presence.rb', line 7

def add_presence_constraint(table, attribute, options = {})
  name = "#{table}_#{attribute}"
  conditions = "#{attribute} !~ '^\\s*$'"
  if options[:if].present?
    conditions = "NOT (#{options[:if]}) OR (#{conditions})"
  end
  execute("ALTER TABLE #{table} ADD CONSTRAINT #{name} CHECK (#{conditions})")
end

#remove_presence_constraint(table, attribute) ⇒ Object



16
17
18
19
# File 'lib/rein/constraint/presence.rb', line 16

def remove_presence_constraint(table, attribute)
  name = "#{table}_#{attribute}"
  execute("ALTER TABLE #{table} DROP CONSTRAINT #{name}")
end