Module: DataMapper::NestedAttributes::AssociationValidation

Defined in:
lib/dm-accepts_nested_attributes/association_validation.rb

Instance Method Summary collapse

Instance Method Details

#check_validations(context = :default) ⇒ Object

everything works the same if this method isn’t overwritten with a no-op however, i suspect that this is the case because the registered before(:save) hook somehow gets lost when overwriting Resource#save here in this module. I’ll leave it in for now, to make the purpose clear



42
43
44
# File 'lib/dm-accepts_nested_attributes/association_validation.rb', line 42

def check_validations(context = :default)
  true # no-op, validations are checked inside #save
end

#save_child_associations(saved, context) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/dm-accepts_nested_attributes/association_validation.rb', line 6

def save_child_associations(saved, context)
  return super if context.nil? # preserve save! behavior
  child_associations.each do |a|
    if a.respond_to?(:valid?)
      a.errors.each { |e| self.errors.add(:general, e) } unless a.valid?(context)
    else
      self.errors.add(:general, "child association is missing")
    end
    saved |= a.save
  end
  saved
end

#save_parent_associations(saved, context) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/dm-accepts_nested_attributes/association_validation.rb', line 23

def save_parent_associations(saved, context)
  parent_associations.each do |a|
    if a.respond_to?(:each) 
      a.each do |r|
        r.errors.each { |e| self.errors.add(:general, e) } unless r.valid?(context)
      end
    else                  
      a.errors.each { |e| self.errors.add(:general, e) } unless a.valid?(context)
    end
    saved |= a.save
  end
  saved
end

#save_selfObject



19
20
21
# File 'lib/dm-accepts_nested_attributes/association_validation.rb', line 19

def save_self
  self.valid? && super
end