Sha256: 097f38d6642a6c26137399ed9974b31b6a9ec3d771b5ad37609c9e1a9c8cf4e0

Contents?: true

Size: 1.28 KB

Versions: 4

Compression:

Stored size: 1.28 KB

Contents

module Remarkable::Mongoid
  module Matchers
    def validate_uniqueness_of(attr)
      ValidateUniquenessOfMatcher.new(attr)
    end
    
    class ValidateUniquenessOfMatcher
      attr_accessor :attr, :validation_type, :message, :scope

      def initialize(attr)
        self.attr = attr.to_sym
      end
      
      def with_message(message)
        self.message = message
        self
      end
      
      def scoped_to(scope)
        self.scope = scope.to_sym
        self
      end

      def matches?(subject)
        @subject    = subject
        validations = @subject._validators[attr]
        if validations
          validation = validations.detect { |k| k.class == ::Mongoid::Validations::UniquenessValidator }
          validation && check_option(validation, :message) && check_option(validation, :scope)
        else
          false
        end
      end

      def description
        "validates that :#{attr} is unique"
      end

      def failure_message_for_should
        "\nUniqueness validation failure\nExpected: '#{attr}' to be unique"
      end
      
      private
      
      def check_option(validation, option)
        if self.send(option)
          validation.options[option] == self.send(option)
        else
          true
        end
      end
    end
    
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
remarkable_mongoid-0.5.0 lib/remarkable/mongoid/validate_uniqueness_of.rb
remarkable_mongoid-0.4.0 lib/remarkable/mongoid/validate_uniqueness_of.rb
remarkable_mongoid-0.3.0 lib/remarkable/mongoid/validate_uniqueness_of.rb
remarkable_mongoid-0.2.0 lib/remarkable/mongoid/validate_uniqueness_of.rb