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
# 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
  @attribute_setters = {
    existing_record: AttributeSetters.new,
    new_record: AttributeSetters.new,
  }
end

Instance Method Details

#allow_blankObject



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

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

#allow_nilObject



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

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:



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

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

#case_insensitiveObject



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

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

#does_not_match?(given_record) ⇒ Boolean

Returns:

  • (Boolean)


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

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)


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

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

#expects_to_allow_nil?Boolean

Returns:

  • (Boolean)


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

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

#ignoring_case_sensitivityObject



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

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

#matches?(given_record) ⇒ Boolean

Returns:

  • (Boolean)


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

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



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

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

#simple_descriptionObject



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

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