Class: ActiveModel::Errors

Inherits:
Object
  • Object
show all
Defined in:
lib/couch_i18n/active_model_errors.rb

Instance Method Summary collapse

Instance Method Details

#full_message(attribute, message) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/couch_i18n/active_model_errors.rb', line 30

def full_message(attribute, message)
  return message if attribute == :base
  attr_name = attribute.to_s.gsub('.', '_').humanize
  attr_name = @base.class.human_attribute_name(attribute, :default => attr_name)

  # Create key. Remove class in model name and add errors.messages
  key = (@base.class.model_name.underscore.sub(/\w+$/, '').scan(/\w+/) + ['errors.conjunction.attribute_message']).join('.')
  I18n.t(key, {
    :default   => "%{attribute} %{message}",
    :attribute => attr_name,
    :message   => message
  })
end

#full_messagesObject



26
27
28
# File 'lib/couch_i18n/active_model_errors.rb', line 26

def full_messages
  map { |attribute, message| full_message(attribute, message) }
end

#generate_message(attribute, type = :invalid, options = {}) ⇒ Object

The code for this method is base on rails revision: f3531a8fc9f661f96759f0a851540745876e5d6c The error lookup now consists of: namespace.errors.messages



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/couch_i18n/active_model_errors.rb', line 9

def generate_message(attribute, type = :invalid, options = {})
  type = options.delete(:message) if options[:message].is_a?(Symbol)

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

  # Create key. Remove class in model name and add errors.messages
  key = (@base.class.model_name.underscore.sub(/\w+$/, '').scan(/\w+/) + ['errors.messages', type]).join('.')

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

  I18n.translate(key, options)
end