module Globalize::Validations::Concern

Private Instance Methods

globalized_errors_for_locale(translated_attribute_names, locale) click to toggle source

Return all translated attributes with errors for the given locale, including their error messages

# File lib/globalize-validations/concern.rb, line 47
def globalized_errors_for_locale(translated_attribute_names, locale)
  errors.add(:base, :invalid)

  translated_attribute_names.each do |attribute|
    errors.messages.delete(attribute.to_sym).to_a.each do |error|
      translation.errors.add(attribute, error)
    end
  end
end
globalized_errors_for_locales(attribute_names, locales) click to toggle source

Return all translated attributes with errors for the given locales, including their error messages

# File lib/globalize-validations/concern.rb, line 27
def globalized_errors_for_locales(attribute_names, locales)
  locales.map!(&:to_s)
  additional_locales = locales - [I18n.locale.to_s]

  if locales.include? I18n.locale.to_s
    # Track errors for current locale
    globalized_errors_for_locale(attribute_names, I18n.locale)
  end

  # Validates the given object against each locale except the current one
  # and track their errors
  additional_locales.each do |locale|
    Globalize.with_locale(locale) do
      globalized_errors_for_locale(attribute_names, locale) if invalid?
    end
  end
end
validates_globalized_attributes() click to toggle source

This validation will perform a validation round against each globalized locales and add errors for globalized attributes names

# File lib/globalize-validations/concern.rb, line 10
def validates_globalized_attributes
  # Only validates globalized attributes from the admin locale
  return unless Globalize.locale == I18n.locale

  # Define which locales to validate against
  locales = if globalize_validations_locales.respond_to?(:call)
              globalize_validations_locales.call(self)
            else
              globalize_validations_locales
            end
  locales ||= []

  globalized_errors_for_locales(translated_attribute_names, locales)
end