Sha256: b6d1780a70e51d661f8d855efa504ffcfd499e141e80e857509e9ca6b5f2f02e
Contents?: true
Size: 1.38 KB
Versions: 14
Compression:
Stored size: 1.38 KB
Contents
module Soulless module Validations class UniquenessValidator < ActiveModel::EachValidator def initialize(options) raise 'ActiveRecord is not defined. The Soulless uniqueness validator cannot be used when ActiveRecord is not present.' unless Object.const_defined?('ActiveRecord') @model = options[:model] @attribute = options[:attribute] options.merge!(class: @model) if ActiveModel::VERSION::STRING >= '4.1.0' @validator = ActiveRecord::Validations::UniquenessValidator.new(options) super(options) end def validate_each(record, attribute, value) if !@model raise ArgumentError, 'Missing required argument "model"' else record_orig, attribute_orig = record, attribute attribute = @attribute if @attribute record = @model.new(attribute => value) @validator.setup(@model) if ActiveModel::VERSION::STRING < '4.1' @validator.validate_each(record, attribute, value) if record.errors.any? record_orig.errors.add(attribute, :taken, options.except(:case_sensitive, :scope).merge(value: value)) end end end end module ClassMethods def validates_uniqueness_of(*attr_name) validates_with(UniquenessValidator, _merge_attributes(attr_name)) end end end end
Version data entries
14 entries across 14 versions & 1 rubygems