Sha256: b93320b45ca1c8bbf657b70dffb05cdc80f8908fa297c794e0d6d54842dad452

Contents?: true

Size: 753 Bytes

Versions: 1

Compression:

Stored size: 753 Bytes

Contents

require "active_record/connection_adapters/abstract/quoting"

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

      def add_inclusion_constraint(table, attribute, options = {})
        name = "#{table}_#{attribute}"
        values = options[:in].map { |value| quote(value) }.join(", ")
        conditions = "#{attribute} IN (#{values})"
        execute("ALTER TABLE #{table} ADD CONSTRAINT #{name} CHECK (#{conditions})")
      end

      def remove_inclusion_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/inclusion.rb