Class: Validity::Record
- Inherits:
-
Object
- Object
- Validity::Record
- 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
-
#record ⇒ Object
readonly
Returns the value of attribute record.
Class Method Summary collapse
-
.validates(record) ⇒ Object
Creates a Validity::Record for validation.
Instance Method Summary collapse
-
#belongs_to(field, target = nil) ⇒ Object
Asserts the record has a
belongs_toassociation as indicated by the providedfieldand that the record equals thetargetrecord, if provided. -
#delegates(delegated, delegated_to) ⇒ Object
Asserts that the record responds to the
delegatedmethod and that the returned object is equal to the object referenced bydelegated_to. -
#field_presence(field) ⇒ Object
Asserts that the given
fieldmust be present for the record to be valid. -
#field_uniqueness(field) ⇒ Object
Asserts that the given
fieldmust be unique for the record to be valid. -
#has_many(field, targets = nil) ⇒ Object
Asserts the record has a
has_manyassociation as indicated by the providedfieldand that the many records equal thetargetsrecords, if provided.
Methods included from Validity
configure, configured?, reset!, supported
Instance Attribute Details
#record ⇒ Object
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:
-
recordthe ActiveRecord model to validate
-
-
Returns:
-
a newly created Validity::Record object
-
-
Raises:
-
Unconfiguredif 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:
-
fieldthe fields to check for has_many association -
targetsthe 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:
-
fieldthe 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:
-
fieldthe 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:
-
fieldthe 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:
-
fieldthe fields to check for has_many association -
targetsthe 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 |