Sha256: 6097eeec32a12fadbed106613e16dd244381e422e8acaaf409f3b6b1440ccaa5
Contents?: true
Size: 1.57 KB
Versions: 1
Compression:
Stored size: 1.57 KB
Contents
module ParanoiaUniquenessValidator module Validations class UniquenessWithoutDeletedValidator < ActiveRecord::Validations::UniquenessValidator def validate_each(record, attribute, value) finder_class = find_finder_class_for(record) table = finder_class.arel_table value = map_enum_attribute(finder_class, attribute, value) relation = build_relation(finder_class, table, attribute, value) if record.persisted? if finder_class.primary_key relation = relation.where.not(finder_class.primary_key => record.id_was || record.id) else raise UnknownPrimaryKey.new(finder_class, "Can not validate uniqueness for persisted record without primary key.") end end relation = scope_relation(record, table, relation) relation = relation.merge(options[:conditions]) if options[:conditions] if defined?('Paranoia') && Paranoia.respond_to?(:default_sentinel_value) sentinel_value = Paranoia.default_sentinel_value else sentinel_value = nil end relation = relation.where(deleted_at: sentinel_value) if relation.exists? error_options = options.except(:case_sensitive, :scope, :conditions) error_options[:value] = value record.errors.add(attribute, :taken, error_options) end end end module ClassMethods def validates_uniqueness_without_deleted_of(*attr_name) validates_with UniquenessWithoutDeletedValidator, _merge_attributes(attr_names) end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
paranoia_uniqueness_validator-2.0.0 | lib/paranoia_uniqueness_validator/validations/uniqueness_without_deleted.rb |