Sha256: c306e4a4ac71c22d046c06d1eed3612f3e146475b34be3eb6d50e14b72f67fc3

Contents?: true

Size: 1.28 KB

Versions: 2

Compression:

Stored size: 1.28 KB

Contents

require "lotus/validations"

# Implements ::validates and friends, and #valid?.
module Reform::Form::Lotus
  class Errors < Lotus::Validations::Errors
    def merge!(errors, prefix)
      new_errors = {}
      errors.instance_variable_get(:@errors).each do |name, err|
        field = (prefix+[name]).join(".")
        new_errors[field] = err
      end
      #   next if messages[field] and messages[field].include?(msg)

      new_errors.each { |field, err| add(field, *err) } # TODO: use namespace feature in Lotus here!
    end

    def inspect
      @errors.to_s
    end

    def messages
      self
    end
  end


  def self.included(base)
    # base.send(:include, Lotus::Validations)
    base.extend(ClassMethods)
  end


  module ClassMethods
    def validates(name, options)
      validations.add(name, options)
    end

    def validate(name, *)
      # DISCUSS: lotus does not support that?
      # validations.add(name, options)
    end

    def validations
      @validations ||= Lotus::Validations::ValidationSet.new
    end
  end

  def build_errors
    Errors.new
  end

  private

  def valid?
    # DISCUSS: by using @fields here, we avoid setters being called. win!
    validator = Lotus::Validations::Validator.new(self.class.validations, @fields, errors)
    validator.validate
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
reform-2.1.0 lib/reform/form/lotus.rb
reform-2.1.0.rc1 lib/reform/form/lotus.rb