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.



299
300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'lib/shoulda/matchers/active_record/validate_uniqueness_of_matcher.rb', line 299

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



352
353
354
355
# File 'lib/shoulda/matchers/active_record/validate_uniqueness_of_matcher.rb', line 352

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

#allow_nilObject



343
344
345
346
# File 'lib/shoulda/matchers/active_record/validate_uniqueness_of_matcher.rb', line 343

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

#alternatives(values) ⇒ ValidateUniquenessOfMatcher

Examples:

it { should validate_uniqueness_of(:title).alternatives('Alternative Title') }
it { should validate_uniqueness_of(:title).alternatives(['Title 1', 'Title 2']) }

Parameters:

  • values (String, Array<String>)

    Alternative value(s) to use for testing uniqueness instead of using the ‘succ` operator on the existing value.

Returns:



328
329
330
331
# File 'lib/shoulda/matchers/active_record/validate_uniqueness_of_matcher.rb', line 328

def alternatives(values)
  @options[:alternatives] = values
  self
end

#case_insensitiveObject



333
334
335
336
# File 'lib/shoulda/matchers/active_record/validate_uniqueness_of_matcher.rb', line 333

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

#does_not_match?(given_record) ⇒ Boolean

Returns:

  • (Boolean)


389
390
391
392
393
394
395
396
397
398
399
400
401
402
# File 'lib/shoulda/matchers/active_record/validate_uniqueness_of_matcher.rb', line 389

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)


357
358
359
# File 'lib/shoulda/matchers/active_record/validate_uniqueness_of_matcher.rb', line 357

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

#expects_to_allow_nil?Boolean

Returns:

  • (Boolean)


348
349
350
# File 'lib/shoulda/matchers/active_record/validate_uniqueness_of_matcher.rb', line 348

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

#ignoring_case_sensitivityObject



338
339
340
341
# File 'lib/shoulda/matchers/active_record/validate_uniqueness_of_matcher.rb', line 338

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

#matches?(given_record) ⇒ Boolean

Returns:

  • (Boolean)


373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# File 'lib/shoulda/matchers/active_record/validate_uniqueness_of_matcher.rb', line 373

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



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

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

#simple_descriptionObject



361
362
363
364
365
366
367
368
369
370
371
# File 'lib/shoulda/matchers/active_record/validate_uniqueness_of_matcher.rb', line 361

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