Sha256: 03395547c5254bcc46c2ff97d4b68611668df6b52e6fa0ca1943f6968bbcee15

Contents?: true

Size: 1.2 KB

Versions: 1

Compression:

Stored size: 1.2 KB

Contents

module ActiveRecord
  module MySQL
    module Strict
      module Mixin
        extend ActiveSupport::Concern

        included do
          define_mysql_strict_validations
        end

        module ClassMethods
          def define_mysql_strict_validations
            except = @mysql_strict_options[:except] || []
            model_columns = self.columns.dup.reject { |c| except.include?(c.name.to_sym) }

            if only = @mysql_strict_options[:only]
              model_columns = model_columns.select { |c| only.include?(c.name.to_sym) }
            end

            model_columns.select { |c| c.type == :string }.each do |field|
              validates field.name, length: { in: 0..(field.limit || 255) }, allow_blank: true
            end

            model_columns.select { |c| c.type == :text }.each do |field|
              validates field.name, length: { in: 0..(field.limit || 65535) }, allow_blank: true
            end

            model_columns.select { |c| c.type == :integer }.each do |field|
              validates field.name, numericality: { greather_than_or_equal_to: -2147483647, less_than_or_equal_to: 2147483647 }, allow_blank: true
            end
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
activerecord_mysql_strict-0.1 lib/active_record/mysql/strict/mixin.rb