Class: Aequitas::MessageTransformer::DefaultI18n

Inherits:
Aequitas::MessageTransformer show all
Defined in:
lib/aequitas/message_transformer.rb

Overview

class DefaultStatic

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Aequitas::MessageTransformer

default

Constructor Details

#initializeDefaultI18n

Returns a new instance of DefaultI18n.



108
109
110
# File 'lib/aequitas/message_transformer.rb', line 108

def initialize
  require 'i18n'
end

Instance Attribute Details

#i18n_namespaceObject



136
137
138
# File 'lib/aequitas/message_transformer.rb', line 136

def i18n_namespace
  @i18n_namespace || 'aequitas.validation.errors'
end

Instance Method Details

#transform(violation) ⇒ Object

Raises:

  • (ArgumentError)


112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/aequitas/message_transformer.rb', line 112

def transform(violation)
  raise ArgumentError, "+violation+ must be specified" if violation.nil?

  resource       = violation.resource
  # TODO: resource#model and Model#model_name are assumptions from DM
  #   Figure out a more flexible way to lookup error messages in I18n
  model_name     = resource.model.model_name
  attribute_name = violation.attribute_name
  # TODO: Include attribute value in Violation; it may have changed by now
  # attribute_value = violation.attribute_value

  options = {
    :model     => ::I18n.translate("models.#{model_name}"),
    :attribute => ::I18n.translate("attributes.#{model_name}.#{attribute_name}"),
    # TODO: Include attribute value in Violation; it may have changed by now
    :value     => resource.validation_attribute_value(attribute_name),
  }.merge(violation.info)

  ::I18n.translate("#{i18n_namespace}.#{violation.type}", options)
end