Sha256: 3c5fe20166bbae10b9ebdfcb417977ea992f8dc1c54f2a5c4898d5c6fe48099c

Contents?: true

Size: 725 Bytes

Versions: 1

Compression:

Stored size: 725 Bytes

Contents

module Rein
  module Constraint
    # This module contains methods for defining presence constraints.
    module Presence
      include ActiveRecord::ConnectionAdapters::Quoting

      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

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rein-2.0.0 lib/rein/constraint/presence.rb