Allow create translations without length validation

We have changed validate_translation method on Globalize concern.
The objective is skip length validations when locale is distinct to
default_locale.

First we force apply :length validations when locale is equal to
default_locale. After we reject :length from options and apply rest
of validation options only when we have more than 1 options.

Ej: options = { length: "maximum: 10" }
When reject :length option in this example, options is equal to a
empty hash and we cant execute validations.
This commit is contained in:
taitus
2019-01-25 17:12:41 +01:00
committed by voodoorai2000
parent 21f347778b
commit abb81fccf4

View File

@@ -35,8 +35,20 @@ module Globalizable
class_methods do
def validates_translation(method, options = {})
validates(method, options.merge(if: lambda { |resource| resource.translations.blank? }))
if options.include?(:length)
lenght_validate = { length: options[:length] }
translation_class.instance_eval do
validates method, lenght_validate.merge(if: lambda { |translation| translation.locale == I18n.default_locale })
end
if options.count > 1
translation_class.instance_eval do
validates method, options.reject { |key| key == :length }
end
end
else
translation_class.instance_eval { validates method, options }
end
end
def translation_class_delegate(method)
translation_class.instance_eval { delegate method, to: :globalized_model }