From c750765bcaa958ff2cfaeb8d5dd83428ff6cb50d Mon Sep 17 00:00:00 2001 From: kikito Date: Wed, 9 Sep 2015 18:30:27 +0200 Subject: [PATCH] Modifies the tag sanitizer to truncate tags longer than 40 I could not make a nice ActAsTaggable error message, this is way faster --- lib/tag_sanitizer.rb | 3 ++- spec/lib/tag_sanitizer_spec.rb | 6 ++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/tag_sanitizer.rb b/lib/tag_sanitizer.rb index f44b06adf..4d15a510b 100644 --- a/lib/tag_sanitizer.rb +++ b/lib/tag_sanitizer.rb @@ -1,4 +1,5 @@ class TagSanitizer + TAG_MAX_LENGTH = 40 DISALLOWED_STRINGS = %w(? < > = /) @@ -7,7 +8,7 @@ class TagSanitizer DISALLOWED_STRINGS.each do |s| tag.gsub!(s, '') end - tag + tag.truncate(TAG_MAX_LENGTH) end def sanitize_tag_list(tag_list) diff --git a/spec/lib/tag_sanitizer_spec.rb b/spec/lib/tag_sanitizer_spec.rb index e1fd6499b..dde5aa483 100644 --- a/spec/lib/tag_sanitizer_spec.rb +++ b/spec/lib/tag_sanitizer_spec.rb @@ -12,6 +12,12 @@ describe TagSanitizer do it 'filters out dangerous strings' do expect(subject.sanitize_tag('user_id=1')).to eq('user_id1') end + + it 'sets up a max length for each tag' do + long_tag = '1' * (TagSanitizer::TAG_MAX_LENGTH + 100) + + expect(subject.sanitize_tag(long_tag).size).to eq(TagSanitizer::TAG_MAX_LENGTH) + end end describe '#sanitize_tag_list' do