Sha256: 112deefdf375c9db0e114b853486dcd3163c5fc0bbcee0aebe540e8860f428ee

Contents?: true

Size: 862 Bytes

Versions: 5

Compression:

Stored size: 862 Bytes

Contents

module FreeForm
  module Validation
    def self.included(base)
      base.extend(ClassMethods)
    end
        
    module ClassMethods
      def validate_models
        validate :model_validity
      end
    end
      
    protected
    def model_validity
      model_validity = true
      self.class.models.each do |form_model|
        if send(form_model).is_a?(Array)
          send(form_model).each { |model| model_validity = validate_and_append_errors(model) && model_validity }
        else
          model_validity = validate_and_append_errors(send(form_model)) && model_validity
        end
      end
      return model_validity
    end
  
    def validate_and_append_errors(model)
      unless model.valid?
        model.errors.each { |error, message| self.errors.add(error, message) }
        return false
      end
      return true
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
freeform-0.0.4 lib/freeform/form/validation.rb
freeform-0.0.3.rc4 lib/freeform/form/validation.rb
freeform-0.0.3.rc3 lib/freeform/form/validation.rb
freeform-0.0.3.rc2 lib/freeform/form/validation.rb
freeform-0.0.3.rc1 lib/freeform/form/validation.rb