Extract callbacks and validations for Post

Move Post create callbacks to PostCreate
Extract Post validations
Move stripped_length_validator to lib/validators
This commit is contained in:
Navin
2013-06-09 18:48:44 +02:00
parent 26a81b30c2
commit 3fdba0019b
4 changed files with 89 additions and 78 deletions

View File

@@ -0,0 +1,15 @@
class StrippedLengthValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
unless value.nil?
stripped_length = value.strip.length
# the `in` parameter might be a lambda when the range is dynamic
range = options[:in].lambda? ? options[:in].call : options[:in]
record.errors.add attribute, (options[:message] || I18n.t('errors.messages.too_short', count: range.begin)) unless
stripped_length >= range.begin
record.errors.add attribute, (options[:message] || I18n.t('errors.messages.too_long', count: range.end)) unless
stripped_length <= range.end
else
record.errors.add attribute, (options[:message] || I18n.t('errors.messages.blank'))
end
end
end