2
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2025-09-05 08:59:27 +08:00

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