Sha256: 2fbfb57676414b5e748b7122480d4e0bb4d0c97eb92d9048f963f73de785a6e9

Contents?: true

Size: 1.57 KB

Versions: 3

Compression:

Stored size: 1.57 KB

Contents

module Mongoid
  module Matchers
    module Validations
      class ValidateUniquenessMatcher < HaveValidationMatcher
        def initialize(field)
          super(field, :uniqueness)
        end

        def case_insensitive
          @case_insensitive = true
          self
        end

        def scoped_to(*scope)
          @scope = [scope].flatten.map(&:to_s)
          self
        end

        def matches?(subject)
          return false unless @result = super(subject)

          check_scope if @scope
          check_case_sensivity if @case_insensitive

          @result
        end

        def description
          desc = []
          desc << " scoped to #{to_sentence(@scope)}" if @scope
          desc << " allowing case insensitive values" if @case_insensitive
          super << desc.to_sentence
        end

        private

        def check_scope
          actual_scope = [@validator.options[:scope]].flatten.map(&:to_s)
          message = " scoped to #{to_sentence(actual_scope)}"

          if actual_scope == @scope
            @positive_message << message
          else
            @negative_message << message
            @result = false
          end
        end

        def check_case_sensivity
          if @validator.options[:case_sensitive] == false
            @positive_message << " with case insensitive values"
          else
            @negative_message << " without case insensitive values"
            @result = false
          end
        end
      end

      def validate_uniqueness_of(field)
        ValidateUniquenessMatcher.new(field)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
mongoid-minitest-0.0.3.1 lib/matchers/validations/uniqueness.rb
mongoid-minitest-0.0.3 lib/matchers/validations/uniqueness.rb
mongoid-minitest-0.0.2 lib/matchers/validations/uniqueness_of.rb