From d120a5d1395d79ac1807867b8e9767b8822a056f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Wed, 10 Apr 2013 14:54:10 +0200 Subject: [PATCH] FIX: setting min_topic_title_length is ignored --- app/models/post.rb | 6 +++--- app/models/stripped_length_validator.rb | 3 ++- app/models/topic.rb | 2 +- spec/models/site_setting_spec.rb | 11 +++-------- 4 files changed, 9 insertions(+), 13 deletions(-) diff --git a/app/models/post.rb b/app/models/post.rb index e582b3bea83..c35bf36e5ac 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -26,7 +26,7 @@ class Post < ActiveRecord::Base has_many :post_actions validates_presence_of :raw, :user_id, :topic_id - validates :raw, stripped_length: { in: SiteSetting.post_length } + validates :raw, stripped_length: { in: -> { SiteSetting.post_length } } validate :raw_quality validate :max_mention_validator validate :max_images_validator @@ -40,8 +40,8 @@ class Post < ActiveRecord::Base scope :by_newest, order('created_at desc, id desc') scope :with_user, includes(:user) - scope :public_posts, lambda { joins(:topic).where('topics.archetype <> ?', [Archetype.private_message]) } - scope :private_posts, lambda { joins(:topic).where('topics.archetype = ?', Archetype.private_message) } + scope :public_posts, -> { joins(:topic).where('topics.archetype <> ?', Archetype.private_message) } + scope :private_posts, -> { joins(:topic).where('topics.archetype = ?', Archetype.private_message) } def self.hidden_reasons @hidden_reasons ||= Enum.new(:flag_threshold_reached, :flag_threshold_reached_again) diff --git a/app/models/stripped_length_validator.rb b/app/models/stripped_length_validator.rb index b84ff0b0090..b77e786ee9e 100644 --- a/app/models/stripped_length_validator.rb +++ b/app/models/stripped_length_validator.rb @@ -2,7 +2,8 @@ class StrippedLengthValidator < ActiveModel::EachValidator def validate_each(record, attribute, value) unless value.nil? stripped_length = value.strip.length - range = options[:in] + # 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 diff --git a/app/models/topic.rb b/app/models/topic.rb index a4a4107c9f6..237475c8d9c 100644 --- a/app/models/topic.rb +++ b/app/models/topic.rb @@ -27,7 +27,7 @@ class Topic < ActiveRecord::Base validate :title_quality validates_presence_of :title - validates :title, length: { in: SiteSetting.topic_title_length } + validate :title, -> { SiteSetting.topic_title_length.include? :length } serialize :meta_data, ActiveRecord::Coders::Hstore diff --git a/spec/models/site_setting_spec.rb b/spec/models/site_setting_spec.rb index 0ab69e35014..95d2ca9d83e 100644 --- a/spec/models/site_setting_spec.rb +++ b/spec/models/site_setting_spec.rb @@ -146,19 +146,14 @@ describe SiteSetting do describe 'topic_title_length' do it 'returns a range of min/max topic title length' do - SiteSetting.min_topic_title_length = 1 - SiteSetting.max_topic_title_length = 2 - - SiteSetting.topic_title_length.should == (1..2) + SiteSetting.topic_title_length.should == + (SiteSetting.defaults[:min_topic_title_length]..SiteSetting.defaults[:max_topic_title_length]) end end describe 'post_length' do it 'returns a range of min/max post length' do - SiteSetting.min_post_length = 1 - SiteSetting.max_post_length = 2 - - SiteSetting.post_length.should == (1..2) + SiteSetting.post_length.should == (SiteSetting.defaults[:min_post_length]..SiteSetting.defaults[:max_post_length]) end end end