Module: ClientSideValidations::ActiveModel::Validations

Defined in:
lib/client_side_validations/active_model.rb

Instance Method Summary collapse

Instance Method Details

#client_side_validation_hash(force = nil) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/client_side_validations/active_model.rb', line 26

def client_side_validation_hash(force = nil)
  _validators.inject({}) do |attr_hash, attr|
    return attr_hash if [nil, :block].include?(attr[0])

    validator_hash = attr[1].each_with_object(Hash.new { |h, k| h[k] = [] }) do |validator, kind_hash|
      next nil unless can_use_for_client_side_validation?(attr[0], validator, force)

      client_side_hash = validator.client_side_hash(self, attr[0], extract_force_option(attr[0], force))
      if client_side_hash
        kind_hash[validator.kind] << client_side_hash.except(:on, :if, :unless)
      end
    end

    if validator_hash.present?
      attr_hash.merge!(attr[0] => validator_hash)
    else
      attr_hash
    end
  end
end