Sha256: 8b2ca46d380b5ae9cafc989c88c1ebea3291f09f2802692219dc9d5851223af0

Contents?: true

Size: 1.64 KB

Versions: 5

Compression:

Stored size: 1.64 KB

Contents

require 'active_model/validator'

module ROM
  module Model
    module Validator
      # Uniqueness validation
      #
      # @api public
      class UniquenessValidator < ActiveModel::EachValidator
        # Relation validator class
        #
        # @api private
        attr_reader :klass

        # error message
        #
        # @return [String, Symbol]
        #
        # @api private
        attr_reader :message

        # @api private
        def initialize(options)
          super
          @klass = options.fetch(:class)
          @message = options.fetch(:message) { :taken }
        end

        # Hook called by ActiveModel internally
        #
        # @api private
        def validate_each(validator, name, value)
          validator.errors.add(name, message) unless unique?(name, value)
        end

        private

        # Get relation object from the rom env
        #
        # @api private
        def relation
          rom.relations[relation_name]
        end

        # Relation name defined on the validator class
        #
        # @api private
        def relation_name
          klass.relation
        end

        # Shortcut to access global rom env
        #
        # @return [ROM::Env]
        #
        # @api private
        def rom
          ROM.env
        end

        # Ask relation if a given attribute value is unique
        #
        # This uses `Relation#unique?` interface that not all adapters can
        # implement.
        #
        # @return [TrueClass,FalseClass]
        #
        # @api private
        def unique?(name, value)
          relation.unique?(name => value)
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rom-rails-0.3.3 lib/rom/rails/model/validator/uniqueness_validator.rb
rom-rails-0.3.2 lib/rom/rails/model/validator/uniqueness_validator.rb
rom-rails-0.3.1 lib/rom/rails/model/validator/uniqueness_validator.rb
rom-rails-0.3.0 lib/rom/rails/model/validator/uniqueness_validator.rb
rom-rails-0.3.0.rc1 lib/rom/rails/model/validator/uniqueness_validator.rb