From abb81fccf41ef9d2f49bd1fa13b77ed705c78cbf Mon Sep 17 00:00:00 2001 From: taitus Date: Fri, 25 Jan 2019 17:12:41 +0100 Subject: [PATCH] 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. --- app/models/concerns/globalizable.rb | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/app/models/concerns/globalizable.rb b/app/models/concerns/globalizable.rb index e511e5cd5..3523f1c29 100644 --- a/app/models/concerns/globalizable.rb +++ b/app/models/concerns/globalizable.rb @@ -35,7 +35,19 @@ module Globalizable class_methods do def validates_translation(method, options = {}) validates(method, options.merge(if: lambda { |resource| resource.translations.blank? })) - translation_class.instance_eval { validates method, options } + 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)