Sha256: 48137e5fb63b7a8470ca86c83846933aac3d19d3131e3c4159b5dfa46ca131ba

Contents?: true

Size: 1019 Bytes

Versions: 1

Compression:

Stored size: 1019 Bytes

Contents

module Conformista
  # Adds validation behaviour to ActiveModel-compliant objects. This means that
  # objects this module is mixed into will only run `save` if it responds
  # positively to `valid?`.
  #
  # This model makes sure that all attributes are properly delegated before any
  # validations run, and copies any presented model errors to the host object.
  #
  # You can override how specific models are validated by overriding the
  # `validate_MODEL_NAME` method.
  module Validations
    def self.included(base)
      base.before_validation :delegate_attributes
      base.before_validation :validate_models
      base.before_validation :copy_validation_errors
      base.before_save :valid?
    end

    private

    def validate_models
      each_model do |record, name|
        send :"validate_#{name}"
      end
    end

    def copy_validation_errors
      each_model do |record, name|
        record.errors.each do |key, value|
          errors.add key, value
        end
      end
    end
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
conformista-0.0.1 lib/conformista/validations.rb