Class: Shoulda::ActiveRecord::Matchers::ValidateUniquenessOfMatcher

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

Overview

:nodoc:

Instance Attribute Summary

Attributes inherited from ValidationMatcher

#failure_message

Instance Method Summary collapse

Methods included from Helpers

#default_error_message, #pretty_error_messages

Methods inherited from ValidationMatcher

#negative_failure_message

Constructor Details

#initialize(attribute) ⇒ ValidateUniquenessOfMatcher

Returns a new instance of ValidateUniquenessOfMatcher.



40
41
42
# File 'lib/shoulda/active_record/matchers/validate_uniqueness_of_matcher.rb', line 40

def initialize(attribute)
  @attribute = attribute
end

Instance Method Details

#case_insensitiveObject



54
55
56
57
# File 'lib/shoulda/active_record/matchers/validate_uniqueness_of_matcher.rb', line 54

def case_insensitive
  @case_insensitive = true
  self
end

#descriptionObject



59
60
61
62
63
64
65
# File 'lib/shoulda/active_record/matchers/validate_uniqueness_of_matcher.rb', line 59

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

#matches?(subject) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
70
71
72
73
74
# File 'lib/shoulda/active_record/matchers/validate_uniqueness_of_matcher.rb', line 67

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

#scoped_to(*scopes) ⇒ Object



44
45
46
47
# File 'lib/shoulda/active_record/matchers/validate_uniqueness_of_matcher.rb', line 44

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

#with_message(message) ⇒ Object



49
50
51
52
# File 'lib/shoulda/active_record/matchers/validate_uniqueness_of_matcher.rb', line 49

def with_message(message)
  @expected_message = message
  self
end