Class: Validity::Record

Inherits:
Object
  • Object
show all
Includes:
Validity
Defined in:
lib/validity/record.rb

Overview

The Validity::Record module contains testing logic specific to ActiveRecord. Example Usage:

@user = Record.validates(User.new)
@user.field_uniqueness(:email)
@user.field_presence(:email)
Author

Matt Fornaciari ([email protected])

License

MIT

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Validity

configure, configured?, reset!, supported

Instance Attribute Details

#recordObject

Returns the value of attribute record.



13
14
15
# File 'lib/validity/record.rb', line 13

def record
  @record
end

Class Method Details

.validates(record) ⇒ Object

Creates a Validity::Record for validation.

  • Args:

    • record the ActiveRecord model to validate

  • Returns:

    • a newly created Validity::Record object

  • Raises:

    • Unconfigured if Validity is not configured



23
24
25
26
# File 'lib/validity/record.rb', line 23

def self.validates(record)
  Validity.check_configured!
  Record.new(record)
end

Instance Method Details

#belongs_to(field, target = nil) ⇒ Object

Asserts the record has a belongs_to association as indicated by the provided field and that the record equals the target record, if provided.

  • Args:

    • field the fields to check for has_many association

    • targets the target associated records



34
35
36
# File 'lib/validity/record.rb', line 34

def belongs_to(field, target = nil)
  test_class.belongs_to @record, field, target
end

#delegates(delegated, delegated_to) ⇒ Object

Asserts that the record responds to the delegated method and that the returned object is equal to the object referenced by delegated_to.

  • Args:

    • field the fields to check for presence



43
44
45
# File 'lib/validity/record.rb', line 43

def delegates(delegated, delegated_to)
  test_class.delegates @record, delegated, delegated_to
end

#field_presence(field) ⇒ Object

Asserts that the given field must be present for the record to be valid.

  • Args:

    • field the fields to check for presence



51
52
53
# File 'lib/validity/record.rb', line 51

def field_presence(field)
  test_class.field_presence @record, field
end

#field_uniqueness(field) ⇒ Object

Asserts that the given field must be unique for the record to be valid.

  • Args:

    • field the fields to check for uniqueness



59
60
61
# File 'lib/validity/record.rb', line 59

def field_uniqueness(field)
  test_class.field_uniqueness @record, field
end

#has_many(field, targets = nil) ⇒ Object

Asserts the record has a has_many association as indicated by the provided field and that the many records equal the targets records, if provided.

  • Args:

    • field the fields to check for has_many association

    • targets the target associated records



69
70
71
# File 'lib/validity/record.rb', line 69

def has_many(field, targets = nil)
  test_class.has_many @record, field, targets
end