Sha256: b937362ceb9854691b17ebd6da83626d4c39e9d2bb29ea2ea1d9d832fa816187

Contents?: true

Size: 994 Bytes

Versions: 2

Compression:

Stored size: 994 Bytes

Contents

module DataMapper
  module NestedAttributes
    
    module ValidationErrorCollecting
      
      # collect errors on parent associations
      def before_save_parent_association(association, context)
        if association.respond_to?(:each) 
          association.each do |r|
            unless r.valid?(context)
              r.errors.each { |e| self.errors.add(:general, e) }
            end
          end
        else
          unless association.valid?(context)
            association.errors.each { |e| self.errors.add(:general, e) }
          end
        end
      end

      # collect errors on child associations
      def before_save_child_association(association, context)
        if association.respond_to?(:valid?)
          unless association.valid?(context)
            association.errors.each { |e| self.errors.add(:general, e) }
          end
        else
          self.errors.add(:general, "child association is missing")
        end
      end
      
    end
    
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
snusnu-dm-accepts_nested_attributes-0.10.0 lib/dm-accepts_nested_attributes/error_collecting.rb
snusnu-dm-accepts_nested_attributes-0.11.0 lib/dm-accepts_nested_attributes/error_collecting.rb