Sha256: 37ce6ba7df1cf6a37d67024e0a5268e4014bf4c129a8f8e47b5c4597c456abe9

Contents?: true

Size: 1.36 KB

Versions: 19

Compression:

Stored size: 1.36 KB

Contents

module ActiveRecord
  module Validations
    class UniquenessValidator
      def validate_each(record, attribute, value)
        finder_class = find_finder_class_for(record)
        table = finder_class.arel_table

        coder = record.class.serialized_attributes[attribute.to_s]

        if value && coder
          value = coder.dump value
        end

        relation = build_relation(finder_class, table, attribute, value)
        # CPK
        # relation = relation.and(table[finder_class.primary_key.to_sym].not_eq(record.send(:id))) if record.persisted?
        if record.persisted?
          not_eq_conditions = Array(finder_class.primary_key).zip(Array(record.send(:id))).map do |name, value|
            table[name.to_sym].not_eq(value)
          end

          condition = not_eq_conditions.shift
          not_eq_conditions.each do |not_eq_condition|
            condition = condition.or(not_eq_conditions)
          end
          relation = relation.and(condition)
        end

        Array.wrap(options[:scope]).each do |scope_item|
          scope_value = record.send(scope_item)
          relation = relation.and(table[scope_item].eq(scope_value))
        end

        if finder_class.unscoped.where(relation).exists?
          record.errors.add(attribute, :taken, options.except(:case_sensitive, :scope).merge(:value => value))
        end
      end
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
composite_primary_keys-5.0.6 lib/composite_primary_keys/validations/uniqueness.rb
composite_primary_keys-5.0.5 lib/composite_primary_keys/validations/uniqueness.rb
composite_primary_keys-5.0.4 lib/composite_primary_keys/validations/uniqueness.rb
composite_primary_keys-5.0.2 lib/composite_primary_keys/validations/uniqueness.rb
composite_primary_keys-5.0.1 lib/composite_primary_keys/validations/uniqueness.rb
composite_primary_keys-5.0.0 lib/composite_primary_keys/validations/uniqueness.rb
composite_primary_keys-5.0.0.rc1 lib/composite_primary_keys/validations/uniqueness.rb
composite_primary_keys-4.1.2 lib/composite_primary_keys/validations/uniqueness.rb
composite_primary_keys-4.1.1 lib/composite_primary_keys/validations/uniqueness.rb
composite_primary_keys-4.0.0 lib/composite_primary_keys/validations/uniqueness.rb
composite_primary_keys-4.0.0.beta9 lib/composite_primary_keys/validations/uniqueness.rb
composite_primary_keys-4.0.0.beta8 lib/composite_primary_keys/validations/uniqueness.rb
composite_primary_keys-4.0.0.beta7 lib/composite_primary_keys/validations/uniqueness.rb
composite_primary_keys-4.0.0.beta6 lib/composite_primary_keys/validations/uniqueness.rb
composite_primary_keys-4.0.0.beta5 lib/composite_primary_keys/validations/uniqueness.rb
composite_primary_keys-4.0.0.beta4 lib/composite_primary_keys/validations/uniqueness.rb
composite_primary_keys-4.0.0.beta3 lib/composite_primary_keys/validations/uniqueness.rb
composite_primary_keys-4.0.0.beta2 lib/composite_primary_keys/validations/uniqueness.rb
composite_primary_keys-4.0.0.beta1 lib/composite_primary_keys/validations/uniqueness.rb