mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
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:
15
lib/validators/stripped_length_validator.rb
Normal file
15
lib/validators/stripped_length_validator.rb
Normal 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
|
||||
Reference in New Issue
Block a user