Skip to content

Instantly share code, notes, and snippets.

@PragmaticEd
Created June 14, 2018 10:04
Show Gist options
  • Save PragmaticEd/6a2a52a165c7fef102e1912e70862ba6 to your computer and use it in GitHub Desktop.
Save PragmaticEd/6a2a52a165c7fef102e1912e70862ba6 to your computer and use it in GitHub Desktop.
Globalize gem presence validation
# Usage in model:
#
# validates :title, translation_presence: true,
# validates :title, translation_presence: {message: ''}
#
class TranslationPresenceValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
record.translations.each do |translation|
if translation[attribute].blank?
record.errors.add("#{attribute}_#{translation.locale}", options[:message] || :blank)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment