Module: RC::Numericality

Included in:
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
Defined in:
lib/rein/constraint/numericality.rb

Constant Summary

OPERATORS =
{
  :greater_than             => :>,
  :greater_than_or_equal_to => :>=,
  :equal_to                 => :=",
  :not_equal_to             => :!=",
  :less_than                => :<,
  :less_than_or_equal_to    => :<=
}.freeze

Instance Method Summary (collapse)

Instance Method Details

- (Object) add_numericality_constraint(table, attribute, options = {})



12
13
14
15
16
17
18
19
20
21
# File 'lib/rein/constraint/numericality.rb', line 12

def add_numericality_constraint(table, attribute, options = {})
  name = "#{table}_#{attribute}"

  conditions = OPERATORS.slice(*options.keys).map do |key, operator|
    value = options[key]
    [attribute, operator, value].join(" ")
  end.join(" AND ")

  execute("ALTER TABLE #{table} ADD CONSTRAINT #{name} CHECK (#{conditions})")
end