class Formalism::R18nErrors::Errors::ErrorTranslator

class for translation hash creation

Public Class Methods

new(translations, form_errors) click to toggle source
# File lib/formalism/r18n_errors/errors.rb, line 18
def initialize(translations, form_errors)
        @translations = translations
        @form_errors = form_errors
        @result = Hash.new { |hash, key| hash[key] = Hash.new(&hash.default_proc) }
end

Public Instance Methods

translate(prefixes) click to toggle source
# File lib/formalism/r18n_errors/errors.rb, line 24
def translate(prefixes)
        @form_errors.each do |model_key, errors|
                errors.each do |error|
                        if error[:rest_keys]
                                insert_translation_in_result(error, model_key, prefixes)
                        else
                                merge_nested_translations_with_result(error, prefixes)
                        end
                end
        end
        @result
end

Private Instance Methods

insert_translation_in_result(error, model_key, prefixes) click to toggle source
# File lib/formalism/r18n_errors/errors.rb, line 39
def insert_translation_in_result(error, model_key, prefixes)
        translation_keys = error[:rest_keys][0..-2].unshift(model_key)
        edge_key = error[:rest_keys].last
        args = error[:translation_args]

        @result.dig(*translation_keys)[edge_key] =
                @translations.dig(*prefixes + translation_keys)[edge_key, args]
end
merge_nested_translations_with_result(error, prefixes) click to toggle source
# File lib/formalism/r18n_errors/errors.rb, line 48
def merge_nested_translations_with_result(error, prefixes)
        nested_hash = error[:nested_errors].translations_hash(*prefixes)
        nested_hash = nested_hash[nil] while nested_hash.key?(nil)
        @result.merge! nested_hash
end