Class: ActiveModel::ErrorCollecting::HumanMessageFormatter

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/active_model/error_collecting/human_message_formatter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base, error_message) ⇒ HumanMessageFormatter

Returns a new instance of HumanMessageFormatter.



10
11
12
# File 'lib/active_model/error_collecting/human_message_formatter.rb', line 10

def initialize(base, error_message)
  @base, @error_message = base, error_message
end

Instance Attribute Details

#baseObject (readonly)

Returns the value of attribute base.



8
9
10
# File 'lib/active_model/error_collecting/human_message_formatter.rb', line 8

def base
  @base
end

#error_messageObject (readonly)

Returns the value of attribute error_message.



8
9
10
# File 'lib/active_model/error_collecting/human_message_formatter.rb', line 8

def error_message
  @error_message
end

Instance Method Details

#format_messageObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/active_model/error_collecting/human_message_formatter.rb', line 18

def format_message
  return message if message && error_message.type.nil?

  keys = i18n_keys
  key  = keys.shift

  value = (attribute != :base ? base.send(:read_attribute_for_validation, attribute) : nil)

  options = {
    :default => keys,
    :model => base.class.model_name.human,
    :attribute => base.class.human_attribute_name(attribute),
    :value => value
  }.merge(self.options)

  I18n.translate key, options
end

#typeObject



14
15
16
# File 'lib/active_model/error_collecting/human_message_formatter.rb', line 14

def type
  @error_message.type || :invalid
end