Class: Mongoid::Matchers::Validations::ValidateUniquenessOfMatcher

Inherits:
HaveValidationMatcher show all
Defined in:
lib/matchers/validations/uniqueness_of.rb

Instance Method Summary collapse

Methods inherited from HaveValidationMatcher

#failure_message_for_should, #failure_message_for_should_not

Constructor Details

#initialize(field) ⇒ ValidateUniquenessOfMatcher

Returns a new instance of ValidateUniquenessOfMatcher.



5
6
7
# File 'lib/matchers/validations/uniqueness_of.rb', line 5

def initialize(field)
  super(field, :uniqueness)
end

Instance Method Details

#allow_blank?(allow_blank) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/matchers/validations/uniqueness_of.rb', line 15

def allow_blank?(allow_blank)
  @allow_blank = allow_blank
end

#descriptionObject



39
40
41
42
43
44
# File 'lib/matchers/validations/uniqueness_of.rb', line 39

def description
  options_desc = []
  options_desc << " scoped to #{@scope.inspect}" if @scope
  options_desc << " allowing blank values" if @allow_blank          
  super << options_desc.to_sentence
end

#matches?(actual) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/matchers/validations/uniqueness_of.rb', line 19

def matches?(actual)
  return false unless result = super(actual)
  
  if [@validator.options[:scope]].flatten == @scope
    @positive_result_message = @positive_result_message << "scope to #{@validator.options[:scope]}"
  else
    @negative_result_message = @negative_result_message << "scope to #{@validator.options[:scope]}"
    result = false
  end if @scope
  
  if @validator.options[:allow_blank] == @allow_blank
    @positive_result_message = @positive_result_message << " with blank values allowed"
  else
    @negative_result_message = @negative_result_message << " with no blank values allowed"
    result = false
  end if @allow_blank          
  
  result
end

#scoped_to(*scope) ⇒ Object Also known as: scoped_on



9
10
11
12
# File 'lib/matchers/validations/uniqueness_of.rb', line 9

def scoped_to(*scope)
  @scope = [scope].flatten
  self
end