Sha256: 14531d99d5685e3c828b8c579252b0d6d6fd849b1275e0cfb7e62ad2e47fc056

Contents?: true

Size: 1.2 KB

Versions: 3

Compression:

Stored size: 1.2 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 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

3 entries across 3 versions & 1 rubygems

Version Path
reform-2.0.5 lib/reform/form/lotus.rb
reform-2.0.4 lib/reform/form/lotus.rb
reform-2.0.3 lib/reform/form/lotus.rb