Class: Rails::Angulate::Mappers::LengthValidatorMapper

Inherits:
BaseMapper
  • Object
show all
Defined in:
lib/rails/angulate/mappers/length_validator_mapper.rb

Constant Summary collapse

OPTION_MAP =
{ is: :islength, minimum: :minlength, maximum: :maxlength }.freeze

Instance Attribute Summary

Attributes inherited from BaseMapper

#attribute, #model, #validator

Instance Method Summary collapse

Methods inherited from BaseMapper

#full_message, inherited, #initialize

Constructor Details

This class inherits a constructor from Rails::Angulate::Mappers::BaseMapper

Instance Method Details

#error_messagesObject



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
41
42
# File 'lib/rails/angulate/mappers/length_validator_mapper.rb', line 16

def error_messages
  messages = {}
  default_message = validator_options[:message]

  with_i18n do |locale|
    validator.class::MESSAGES.each do |key, msg|
      if default_message.nil?
        next unless validator_options[key]

        count = validator_options[key]

        message = locale.t(
          :"messages.#{msg}",
          model: model.class.name.humanize,
          attribute: attribute.to_s.humanize,
          count: count
        )

        messages[OPTION_MAP[key]] = full_message(message)
      else
        messages[OPTION_MAP[key]] = default_message
      end
    end
  end

  messages
end

#ng_attributesObject



7
8
9
10
11
12
13
14
# File 'lib/rails/angulate/mappers/length_validator_mapper.rb', line 7

def ng_attributes
  opts = validator_options
  {}.tap do |attrs|
    validator_options.each do |key, value|
      attrs["ng-#{OPTION_MAP[key]}"] = opts[key] if opts[key].present?
    end
  end
end