Class: Shoulda::Matchers::ActiveRecord::ValidateUniquenessOfMatcher
Defined Under Namespace
Classes: AttributeSetters, ExistingRecordInvalid, NonCaseSwappableValueError
Instance Attribute Summary
#ignore_interference_by_writer
Instance Method Summary
collapse
#default_error_message, #format_validation_errors, #pretty_error_messages
#description, #expects_custom_validation_message?, #expects_strict?, #failure_message, #failure_message_when_negated, #on, #strict, #with_message
#ignoring_interference_by_writer
Constructor Details
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_blank ⇒ Object
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_nil ⇒ Object
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
|
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_insensitive ⇒ Object
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
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
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
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_sensitivity ⇒ Object
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
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_description ⇒ Object
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
|