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

Inherits:
Shoulda::Matchers::ActiveModel::ValidationMatcher show all
Includes:
Shoulda::Matchers::ActiveModel::Helpers
Defined in:
lib/shoulda/matchers/active_record/validate_uniqueness_of_matcher.rb

Defined Under Namespace

Classes: AttributeSetters, ExistingRecordInvalid, NonCaseSwappableValueError

Instance Attribute Summary

Attributes included from Shoulda::Matchers::ActiveModel::Qualifiers::IgnoringInterferenceByWriter

#ignore_interference_by_writer

Instance Method Summary collapse

Methods included from Shoulda::Matchers::ActiveModel::Helpers

#default_error_message, #format_validation_errors, #pretty_error_messages

Methods inherited from Shoulda::Matchers::ActiveModel::ValidationMatcher

#description, #expects_custom_validation_message?, #expects_strict?, #failure_message, #failure_message_when_negated, #on, #strict, #with_message

Methods included from Shoulda::Matchers::ActiveModel::Qualifiers::IgnoringInterferenceByWriter

#ignoring_interference_by_writer

Constructor Details

#initialize(attribute) ⇒ ValidateUniquenessOfMatcher

Returns a new instance of ValidateUniquenessOfMatcher.



269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/shoulda/matchers/active_record/validate_uniqueness_of_matcher.rb', line 269

def initialize(attribute)
  super(attribute)
  @expected_message = :taken
  @options = {
    case_sensitivity_strategy: :sensitive,
  }
  @existing_record_created = false
  @failure_reason = nil
  @failure_reason_when_negated = nil
  @attribute_setters = {
    existing_record: AttributeSetters.new,
    new_record: AttributeSetters.new,
  }
end

Instance Method Details

#allow_blankObject



308
309
310
311
# File 'lib/shoulda/matchers/active_record/validate_uniqueness_of_matcher.rb', line 308

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

#allow_nilObject



299
300
301
302
# File 'lib/shoulda/matchers/active_record/validate_uniqueness_of_matcher.rb', line 299

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

#case_insensitiveObject



289
290
291
292
# File 'lib/shoulda/matchers/active_record/validate_uniqueness_of_matcher.rb', line 289

def case_insensitive
  @options[:case_sensitivity_strategy] = :insensitive
  self
end

#does_not_match?(given_record) ⇒ Boolean

Returns:

  • (Boolean)


345
346
347
348
349
350
351
352
353
354
355
356
357
358
# File 'lib/shoulda/matchers/active_record/validate_uniqueness_of_matcher.rb', line 345

def does_not_match?(given_record)
  @given_record = given_record
  @all_records = model.all

  does_not_match_presence_of_scopes? ||
    does_not_match_scopes_configuration? ||
    does_not_match_uniqueness_without_scopes? ||
    does_not_match_uniqueness_with_case_sensitivity_strategy? ||
    does_not_match_uniqueness_with_scopes? ||
    does_not_match_allow_nil? ||
    does_not_match_allow_blank?
ensure
  Uniqueness::TestModels.remove_all
end

#expects_to_allow_blank?Boolean

Returns:

  • (Boolean)


313
314
315
# File 'lib/shoulda/matchers/active_record/validate_uniqueness_of_matcher.rb', line 313

def expects_to_allow_blank?
  @options[:allow_blank] == true
end

#expects_to_allow_nil?Boolean

Returns:

  • (Boolean)


304
305
306
# File 'lib/shoulda/matchers/active_record/validate_uniqueness_of_matcher.rb', line 304

def expects_to_allow_nil?
  @options[:allow_nil] == true
end

#ignoring_case_sensitivityObject



294
295
296
297
# File 'lib/shoulda/matchers/active_record/validate_uniqueness_of_matcher.rb', line 294

def ignoring_case_sensitivity
  @options[:case_sensitivity_strategy] = :ignore
  self
end

#matches?(given_record) ⇒ Boolean

Returns:

  • (Boolean)


329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# File 'lib/shoulda/matchers/active_record/validate_uniqueness_of_matcher.rb', line 329

def matches?(given_record)
  @given_record = given_record
  @all_records = model.all

  matches_presence_of_attribute? &&
    matches_presence_of_scopes? &&
    matches_scopes_configuration? &&
    matches_uniqueness_without_scopes? &&
    matches_uniqueness_with_case_sensitivity_strategy? &&
    matches_uniqueness_with_scopes? &&
    matches_allow_nil? &&
    matches_allow_blank?
ensure
  Uniqueness::TestModels.remove_all
end

#scoped_to(*scopes) ⇒ Object



284
285
286
287
# File 'lib/shoulda/matchers/active_record/validate_uniqueness_of_matcher.rb', line 284

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

#simple_descriptionObject



317
318
319
320
321
322
323
324
325
326
327
# File 'lib/shoulda/matchers/active_record/validate_uniqueness_of_matcher.rb', line 317

def simple_description
  description = "validate that :#{@attribute} is"
  description << description_for_case_sensitive_qualifier
  description << ' unique'

  if @options[:scopes].present?
    description << " within the scope of #{inspected_expected_scopes}"
  end

  description
end