Module: DataMapper::ValidationsExt

Defined in:
lib/ixtlan/datamapper/validations_ext.rb

Instance Method Summary collapse

Instance Method Details

#_save(execute_hooks = true) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/ixtlan/datamapper/validations_ext.rb', line 4

def _save(execute_hooks = true)
  result = super

  # TODO: should we wrap it with run_once?
  unless result
    validate          if dirty_self?
    validate_parents  if dirty_parents?
    validate_children if dirty_children?
  end

  result
end

#validateBoolean

Run validations on the resource

Returns:

  • (Boolean)

    true if the resource is valid



23
24
25
# File 'lib/ixtlan/datamapper/validations_ext.rb', line 23

def validate
  valid?
end

#validate_childrenObject

Run validations on the associated child resources



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ixtlan/datamapper/validations_ext.rb', line 44

def validate_children
  child_associations.each do |collection|
    if collection.dirty?
      collection.each do |child|
        unless child.valid?
          relationship_errors = (errors[collection.relationship.name] ||= [])
          unless relationship_errors.include?(child.errors)
            relationship_errors << child.errors
          end
        end
      end
    end
  end
end

#validate_parentsObject

Run validations on the associated parent resources



30
31
32
33
34
35
36
37
38
39
# File 'lib/ixtlan/datamapper/validations_ext.rb', line 30

def validate_parents
  parent_relationships.each do |relationship|
    parent = relationship.get(self)
    unless parent.valid?
      unless errors[relationship.name].include?(parent.errors)
        errors[relationship.name] = parent.errors
      end
    end
  end
end