From 023d18cab2f97af2f2aa87459a4d19cd677d6715 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Sat, 29 Jun 2013 17:57:10 -0400 Subject: [PATCH] FIX: Admins should be able to post short titles. This is especially important for when syndicating a blog via the wordpress plugin into Discourse. --- lib/post_creator.rb | 4 ++-- .../topic_title_length_validator.rb | 23 +++++++++++++------ spec/models/topic_spec.rb | 14 +++++++++++ 3 files changed, 32 insertions(+), 9 deletions(-) diff --git a/lib/post_creator.rb b/lib/post_creator.rb index 7bdcd409b..eb14c4b8e 100644 --- a/lib/post_creator.rb +++ b/lib/post_creator.rb @@ -185,8 +185,8 @@ class PostCreator def setup_post post = @topic.posts.new(raw: @opts[:raw], - user: @user, - reply_to_post_number: @opts[:reply_to_post_number]) + user: @user, + reply_to_post_number: @opts[:reply_to_post_number]) # Attributes we pass through to the post instance if present [:post_type, :no_bump, :cooking_options, :image_sizes, :acting_user, :invalidate_oneboxes].each do |a| diff --git a/lib/validators/topic_title_length_validator.rb b/lib/validators/topic_title_length_validator.rb index 00451c733..82d9d54f9 100644 --- a/lib/validators/topic_title_length_validator.rb +++ b/lib/validators/topic_title_length_validator.rb @@ -1,12 +1,21 @@ class TopicTitleLengthValidator < ActiveModel::EachValidator def validate_each(record, attribute, value) - if record.private_message? - private_message_title_validator = ActiveModel::Validations::LengthValidator.new({attributes: :title, in: SiteSetting.private_message_title_length, allow_blank: true}) - private_message_title_validator.validate_each(record, attribute, value) - else - topic_title_validator = ActiveModel::Validations::LengthValidator.new({attributes: :title, in: SiteSetting.topic_title_length, allow_blank: true}) - topic_title_validator.validate_each(record, attribute, value) - end + title_validator(record).validate_each(record, attribute, value) end + + private + + def title_validator(record) + length_range = if record.user.try(:admin?) + 1..SiteSetting.max_topic_title_length + elsif record.private_message? + SiteSetting.private_message_title_length + else + SiteSetting.topic_title_length + end + + ActiveModel::Validations::LengthValidator.new({attributes: :title, in: length_range, allow_blank: true}) + end + end diff --git a/spec/models/topic_spec.rb b/spec/models/topic_spec.rb index ab89de8f4..6442cc7b3 100644 --- a/spec/models/topic_spec.rb +++ b/spec/models/topic_spec.rb @@ -69,6 +69,20 @@ describe Topic do end end + context 'admin topic title' do + let(:admin) { Fabricate(:admin) } + + it 'allows really short titles' do + pm = Fabricate.build(:private_message_topic, user: admin, title: 'a') + expect(pm).to be_valid + end + + it 'but not blank' do + pm = Fabricate.build(:private_message_topic, title: '') + expect(pm).to_not be_valid + end + end + context 'topic title uniqueness' do let!(:topic) { Fabricate(:topic) }