Class: Shoulda::Matchers::ActiveModel::ValidateUniquenessOfMatcher

Inherits:
ValidationMatcher
  • Object
show all
Includes:
Helpers
Defined in:
lib/shoulda/matchers/active_model/validate_uniqueness_of_matcher.rb

Overview

:nodoc:

Instance Attribute Summary

Attributes inherited from ValidationMatcher

#failure_message_for_should

Instance Method Summary (collapse)

Methods included from Helpers

#default_error_message, #pretty_error_messages

Methods inherited from ValidationMatcher

#failure_message_for_should_not, #on, #strict

Constructor Details

- (ValidateUniquenessOfMatcher) initialize(attribute)

A new instance of ValidateUniquenessOfMatcher



38
39
40
41
# File 'lib/shoulda/matchers/active_model/validate_uniqueness_of_matcher.rb', line 38

def initialize(attribute)
  super(attribute)
  @options = {}
end

Instance Method Details

- (Object) allow_nil



58
59
60
61
# File 'lib/shoulda/matchers/active_model/validate_uniqueness_of_matcher.rb', line 58

def allow_nil
  @options[:allow_nil] = true
  self
end

- (Object) case_insensitive



53
54
55
56
# File 'lib/shoulda/matchers/active_model/validate_uniqueness_of_matcher.rb', line 53

def case_insensitive
  @options[:case_insensitive] = true
  self
end

- (Object) description



63
64
65
66
67
68
69
# File 'lib/shoulda/matchers/active_model/validate_uniqueness_of_matcher.rb', line 63

def description
  result = "require "
  result << "case sensitive " unless @options[:case_insensitive]
  result << "unique value for #{@attribute}"
  result << " scoped to #{@options[:scopes].join(', ')}" if @options[:scopes].present?
  result
end

- (Boolean) matches?(subject)

Returns:

  • (Boolean)


71
72
73
74
75
76
77
78
# File 'lib/shoulda/matchers/active_model/validate_uniqueness_of_matcher.rb', line 71

def matches?(subject)
  @subject = subject.class.new
  @expected_message ||= :taken
  set_scoped_attributes &&
    validate_everything_except_duplicate_nils? &&
    validate_after_scope_change? &&
    allows_nil?
end

- (Object) scoped_to(*scopes)



43
44
45
46
# File 'lib/shoulda/matchers/active_model/validate_uniqueness_of_matcher.rb', line 43

def scoped_to(*scopes)
  @options[:scopes] = [*scopes].flatten
  self
end

- (Object) with_message(message)



48
49
50
51
# File 'lib/shoulda/matchers/active_model/validate_uniqueness_of_matcher.rb', line 48

def with_message(message)
  @expected_message = message
  self
end