Created
June 14, 2018 10:04
-
-
Save PragmaticEd/6a2a52a165c7fef102e1912e70862ba6 to your computer and use it in GitHub Desktop.
Globalize gem presence validation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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