Sha256: bc28285467c5e8fe662ffd9c0dfb16e8210c366275f94fd4a73cc8bb034b5afa

Contents?: true

Size: 1.59 KB

Versions: 5

Compression:

Stored size: 1.59 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
    #     value = map_enum_attribute(finder_class, attribute, value)
    #
    #     begin
    #       relation = build_relation(finder_class, table, attribute, value)
    #       # CPK
    #       # relation = relation.and(table[finder_class.primary_key.to_sym].not_eq(record.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_condition)
    #         end
    #         relation = relation.and(condition)
    #       end
    #       # End CPK
    #       relation = scope_relation(record, table, relation)
    #       relation = finder_class.unscoped.where(relation)
    #       relation = relation.merge(options[:conditions]) if options[:conditions]
    #     rescue RangeError
    #       relation = finder_class.none
    #     end
    #
    #     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
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
composite_primary_keys-9.0.4 lib/composite_primary_keys/validations/uniqueness.rb
composite_primary_keys-9.0.2 lib/composite_primary_keys/validations/uniqueness.rb
composite_primary_keys-9.0.1 lib/composite_primary_keys/validations/uniqueness.rb
composite_primary_keys-9.0.0 lib/composite_primary_keys/validations/uniqueness.rb
composite_primary_keys-9.0.0.beta1 lib/composite_primary_keys/validations/uniqueness.rb