Class: Validity::Record

Inherits:
Object
  • Object
show all
Includes:
Test::Unit::Assertions, 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.



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

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



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

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



35
36
37
38
39
40
41
42
43
44
# File 'lib/validity/record.rb', line 35

def belongs_to(field, target = nil)
  clazz = @record.class
  assert_respond_to @record, field, "#{clazz} cannot find associated #{field}"

  one = @record.send(field)
  assert_not_nil one, "#{clazz} does not have associated #{field}"
  if target
    assert_equal target, one, "#{field.to_s.capitalize} associated with this #{clazz.to_s.downcase} is not the target #{field}"
  end
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



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

def delegates(delegated, delegated_to)
  clazz = @record.class
  assert_respond_to @record, delegated, "#{clazz} does not respond to #{delegated}"
  assert_equal delegated_to.send(delegated), @record.send(delegated), "Delegated objects do not match"
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



61
62
63
64
65
66
67
68
# File 'lib/validity/record.rb', line 61

def field_presence(field)
  @record.send("#{field}=", nil)

  clazz = @record.class
  assert !@record.valid?, "#{clazz} is considered valid with nil #{field}"
  assert !@record.save, "#{clazz} saved without #{field} field"
  assert @record.errors[field].any?, "#{clazz} does not have an error on #{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



74
75
76
77
78
79
80
81
# File 'lib/validity/record.rb', line 74

def field_uniqueness(field)
  dup = @record.dup

  clazz = dup.class
  assert !dup.valid?, "#{clazz} is considered valid with duplicate #{field}"
  assert !dup.save, "#{clazz} saved with a duplicate #{field}"
  assert dup.errors[field].any?, "#{clazz} does not have an error on #{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



89
90
91
92
93
94
95
96
97
98
# File 'lib/validity/record.rb', line 89

def has_many(field, targets = nil)
  clazz = @record.class
  assert_respond_to @record, field, "#{clazz} cannot find associated #{field}"

  many = @record.send(field)
  assert !(many.nil? || many.empty?), "#{clazz} does not have associated #{field}"
  if targets
    assert_equal targets.size, many.size, "#{clazz} does not have #{targets.size} associated #{field}"
  end
end