Merge pull request #172 from jeremybanks/master

Do not strip leading and trailing whitespace from raw posts
This commit is contained in:
Robin Ward
2013-02-19 08:15:39 -08:00
4 changed files with 45 additions and 16 deletions

View File

@ -29,7 +29,7 @@ class Post < ActiveRecord::Base
has_many :post_actions
validates_presence_of :raw, :user_id, :topic_id
validates :raw, length: {in: SiteSetting.min_post_length..SiteSetting.max_post_length}
validates :raw, stripped_length: {in: SiteSetting.min_post_length..SiteSetting.max_post_length}
validate :raw_quality
validate :max_mention_validator
validate :max_images_validator
@ -57,10 +57,6 @@ class Post < ActiveRecord::Base
TopicUser.auto_track(self.user_id, self.topic_id, TopicUser::NotificationReasons::CREATED_POST)
end
before_validation do
self.raw.strip! if self.raw.present?
end
def raw_quality
sentinel = TextSentinel.new(self.raw, min_entropy: SiteSetting.body_min_entropy)
@ -212,7 +208,7 @@ class Post < ActiveRecord::Base
# We only filter quotes when there is exactly 1
return cooked unless (quote_count == 1)
parent_raw = parent_post.raw.sub(/\[quote.+\/quote\]/m, '').strip
parent_raw = parent_post.raw.sub(/\[quote.+\/quote\]/m, '')
if raw[parent_raw] or (parent_raw.size < SHORT_POST_CHARS)
return cooked.sub(/\<aside.+\<\/aside\>/m, '')

View File

@ -0,0 +1,14 @@
class StrippedLengthValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
unless value.nil?
stripped_length = value.strip.length
range = 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