Module: ClientSideValidations::ActiveModel::Length

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

Instance Method Summary collapse

Instance Method Details

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



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/client_side_validations/active_model/length.rb', line 6

def client_side_hash(model, attribute, force = nil)
  options = self.options.dup
  hash    = options_hash(options)

  self.class::MESSAGES.each do |option, message_type|
    count = options[option]
    next unless count

    if count.respond_to?(:call)
      next unless force

      count = resolve_proc(count, model)
    end

    options[:message] = options[message_type] if options[message_type].present?
    options.delete(:message) if options[:message].nil?
    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