refactor validators

add a new setting for min pm body length
use that setting for flags
scale entropy check down for pms
This commit is contained in:
Sam
2013-06-13 18:18:17 +10:00
parent b027f7d8da
commit f7de9f17d5
12 changed files with 91 additions and 23 deletions

View File

@@ -1,5 +1,9 @@
class PostValidator < ActiveModel::Validator
require_dependency 'validators/stripped_length_validator'
module Validators; end
class Validators::PostValidator < ActiveModel::Validator
def validate(record)
presence(record)
stripped_length(record)
raw_quality(record)
max_mention_validator(record)
max_images_validator(record)
@@ -7,8 +11,19 @@ class PostValidator < ActiveModel::Validator
unique_post_validator(record)
end
def presence(post)
[:raw,:user_id,:topic_id].each do |attr_name|
post.errors.add(attr_name, :blank, options) if post.send(attr_name).blank?
end
end
def stripped_length(post)
range = post.topic.try(:private_message?) ? SiteSetting.private_message_post_length : SiteSetting.post_length
Validators::StrippedLengthValidator.validate(post, :raw, post.raw, range)
end
def raw_quality(post)
sentinel = TextSentinel.body_sentinel(post.raw)
sentinel = TextSentinel.body_sentinel(post.raw, private_message: post.topic.try(:private_message?))
post.errors.add(:raw, I18n.t(:is_invalid)) unless sentinel.valid?
end

View File

@@ -1,15 +1,20 @@
class StrippedLengthValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
module Validators; end
class Validators::StrippedLengthValidator < ActiveModel::EachValidator
def self.validate(record, attribute, value, range)
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
record.errors.add attribute, (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
record.errors.add attribute, (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'))
record.errors.add attribute, (I18n.t('errors.messages.blank'))
end
end
def validate_each(record, attribute, value)
# the `in` parameter might be a lambda when the range is dynamic
range = options[:in].lambda? ? options[:in].call : options[:in]
self.class.validate(record, attribute, value, range)
end
end