Sha256: f408fce59716409475e4886e369b626ce066e479fcc2834904befb84c593183b

Contents?: true

Size: 1.18 KB

Versions: 5

Compression:

Stored size: 1.18 KB

Contents

require "lotus/validations"

# Implements ::validates and friends, and #valid?.
module Reform::Form::Lotus
  class Errors < Lotus::Validations::Errors
    def merge!(errors, prefix)
      errors.instance_variable_get(:@errors).each do |name, err|
        field = (prefix+[name]).join(".")
        add(field, *err) # TODO: use namespace feature in Lotus here!
      end
      #   next if messages[field] and messages[field].include?(msg)
    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 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

  def build_errors
    Errors.new
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
reform-2.0.2 lib/reform/form/lotus.rb
reform-2.0.1 lib/reform/form/lotus.rb
reform-2.0.0 lib/reform/form/lotus.rb
reform-2.0.0.rc3 lib/reform/form/lotus.rb
reform-2.0.0.rc2 lib/reform/form/lotus.rb