Module: ClientSideValidations::ActiveModel::Numericality

Defined in:
lib/client_side_validations/active_model/numericality.rb

Constant Summary collapse

OPTION_MAP =
{}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



6
7
8
# File 'lib/client_side_validations/active_model/numericality.rb', line 6

def self.included(base)
  OPTION_MAP.merge!(base::CHECKS.keys.inject({}) { |a, e| a.merge!(e => e) })
end

Instance Method Details

#client_side_hash(model, attribute, force = nil) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/client_side_validations/active_model/numericality.rb', line 10

def client_side_hash(model, attribute, force = nil)
  options = self.options.dup
  hash    = { messages: { numericality: model.errors.generate_message(attribute, :not_a_number, options) } }

  if options[:only_integer]
    hash[:messages][:only_integer] = model.errors.generate_message(attribute, :not_an_integer, options)
    hash[:only_integer] = true
  end

  hash[:allow_blank] = true if options[:allow_nil] || options[:allow_blank]

  OPTION_MAP.each do |option, message_type|
    count = options[option]
    next unless count

    if count.respond_to?(:call)
      if force
        count = count.call(model)
      else
        next
      end
    end

    hash[:messages][option] = model.errors.generate_message(attribute, message_type, options.merge(count: count))
    hash[option] = count
  end

  copy_conditional_attributes(hash, options)

  hash
end