Class: Validity::ActiveRecord::Wrapper

Inherits:
Object
  • Object
show all
Includes:
Test::Unit::Assertions
Defined in:
lib/validity/active_record.rb

Overview

The Wrapper class is just a thin wrapper around the ActiveRecord model which defines several methods to validate a model behaves as it should. Example Usage:

validates(@model).field_presence(:required)

Instance Method Summary collapse

Constructor Details

#initialize(record) ⇒ Wrapper

Creates a new instance of Validity::ActiveRecord::Wrapper to test against



22
23
24
# File 'lib/validity/active_record.rb', line 22

def initialize(record)
  @record = record
end

Instance Method Details

#belongs_to(field, target = nil) ⇒ Object

Asserts that the record has a belongs_to association and that the associated record equals the target record, if provided.



28
29
30
31
32
33
34
35
36
37
# File 'lib/validity/active_record.rb', line 28

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



41
42
43
44
45
# File 'lib/validity/active_record.rb', line 41

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 field field must be present for the record to be valid



48
49
50
51
52
53
54
55
# File 'lib/validity/active_record.rb', line 48

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 field field must be unique for the record to be valid



58
59
60
61
62
63
64
65
# File 'lib/validity/active_record.rb', line 58

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 that the record has a has_many association and that the associated records equal the targets records, if provided.



69
70
71
72
73
74
75
76
77
78
# File 'lib/validity/active_record.rb', line 69

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