Files
nairobi/app/models/concerns/globalizable.rb
taitus abb81fccf4 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.
2019-06-27 09:21:18 +02:00

58 lines
1.8 KiB
Ruby

module Globalizable
extend ActiveSupport::Concern
included do
globalize_accessors
accepts_nested_attributes_for :translations, allow_destroy: true
def locales_not_marked_for_destruction
translations.reject(&:_destroy).map(&:locale)
end
def description
self.read_attribute(:description).try :html_safe
end
if self.paranoid? && translation_class.attribute_names.include?("hidden_at")
translation_class.send :acts_as_paranoid, column: :hidden_at
end
scope :with_translation, -> { joins("LEFT OUTER JOIN #{translations_table_name} ON #{table_name}.id = #{translations_table_name}.#{reflections["translations"].foreign_key} AND #{translations_table_name}.locale='#{I18n.locale }'") }
private
def searchable_globalized_values
values = {}
translations.each do |translation|
Globalize.with_locale(translation.locale) do
values.merge! searchable_translations_definitions
end
end
values
end
end
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 }
end
end
end