Module: Reform::Form::ActiveModel::Validations

Included in:
Validator
Defined in:
lib/reform/form/active_model/validations.rb

Overview

AM::Validations for your form. Provides ::validates, ::validate, #validate, and #valid?.

Most of this file contains unnecessary wiring to make ActiveModel’s error message magic work. Since Rails still thinks it’s a good idea to do things like object.class.human_attribute_name, we have some hacks in here to provide that. If it doesn’t work for you, don’t blame us.

Defined Under Namespace

Classes: Group, Validator

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(includer) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/reform/form/active_model/validations.rb', line 12

def self.included(includer)
  includer.instance_eval do
    include Reform::Form::ActiveModel

    class << self
      extend Uber::Delegates
      # # Hooray! Delegate translation back to Reform's Validator class which contains AM::Validations.
      delegates :active_model_really_sucks, :human_attribute_name, :lookup_ancestors, :i18n_scope # Rails 3.1.

      def validation_group_class
        Group
      end

      # this is to allow calls like Form::human_attribute_name (note that this is on the CLASS level) to be resolved.
      # those calls happen when adding errors in a custom validation method, which is defined on the form (as an instance method).
      def active_model_really_sucks
        Class.new(Validator).tap do |v|
          v.model_name = model_name
        end
      end
    end
  end # ::included
end

Instance Method Details

#build_errorsObject



36
37
38
# File 'lib/reform/form/active_model/validations.rb', line 36

def build_errors
  Errors.new(self)
end

#read_attribute_for_validation(name) ⇒ Object

The concept of “composition” has still not arrived in Rails core and they rely on 400 methods being available in one object. This is why we need to provide parts of the I18N API in the form.



42
43
44
# File 'lib/reform/form/active_model/validations.rb', line 42

def read_attribute_for_validation(name)
  send(name)
end