Sha256: 36af5538c429959382a1a9d617c98d404b4cdc92af2d867b8376138e9b5d387c

Contents?: true

Size: 1.46 KB

Versions: 2

Compression:

Stored size: 1.46 KB

Contents

module DataMapper
  module ValidationsExt
    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

    # Run validations on the resource
    #
    # @return [Boolean]
    #   true if the resource is valid
    #
    # @api public
    def validate
      valid?
    end

    # Run validations on the associated parent resources
    #
    # @api semipublic
    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

    # Run validations on the associated child resources
    #
    # @api semipublic
    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

    Model.append_inclusions self
  end # ValidationsExt
end # DataMapper

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dm-validations-ext-0.0.2 lib/dm-validations-ext.rb
dm-validations-ext-0.0.1 lib/dm-validations-ext.rb