From 2143e0f9ba0c9c7c246e8c3e59d514487986db22 Mon Sep 17 00:00:00 2001 From: Sarah Mei Date: Sat, 5 Nov 2011 19:57:51 -0700 Subject: [PATCH] Refactor regular expression used to find tags so that it supports unicode in both ruby 1.8 and ruby 1.9. NEVER SPEAK OF THIS AGAIN. --- app/models/acts_as_taggable_on_tag.rb | 7 ++++++- lib/diaspora/taggable.rb | 4 ++-- spec/shared_behaviors/taggable.rb | 11 +++++++++-- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/app/models/acts_as_taggable_on_tag.rb b/app/models/acts_as_taggable_on_tag.rb index fdf80defa..3eef9409c 100644 --- a/app/models/acts_as_taggable_on_tag.rb +++ b/app/models/acts_as_taggable_on_tag.rb @@ -1,8 +1,13 @@ class ActsAsTaggableOn::Tag + def followed_count @followed_count ||= TagFollowing.where(:tag_id => self.id).count end + def self.tag_text_regexp + @@tag_text_regexp ||= (RUBY_VERSION.include?('1.9') ? "[[:alnum:]]_-" : "\\w-") + end + def self.autocomplete(name) where("name LIKE ?", "#{name.downcase}%") end @@ -12,7 +17,7 @@ class ActsAsTaggableOn::Tag # Special case for love, because the world needs more love. '<3' elsif name - name.gsub(/[^\w-]/, '') + name.gsub(/[^#{self.tag_text_regexp}]/, '') end end end diff --git a/lib/diaspora/taggable.rb b/lib/diaspora/taggable.rb index 0230c8a5f..13c9bb811 100644 --- a/lib/diaspora/taggable.rb +++ b/lib/diaspora/taggable.rb @@ -25,7 +25,7 @@ module Diaspora end def tag_strings - regex = /(?:^|\s)#([\w-]+|<3)/ + regex = /(?:^|\s)#([#{ActsAsTaggableOn::Tag.tag_text_regexp}]+|<3)/u matches = self. send( self.class.field_with_tags ). scan(regex). @@ -41,7 +41,7 @@ module Diaspora return text if opts[:plain_text] text = ERB::Util.h(text) unless opts[:no_escape] - regex = /(^|\s|>)#([\w-]+|<3)/ + regex =/(^|\s|>)#([#{ActsAsTaggableOn::Tag.tag_text_regexp}]+|<3)/u text.to_str.gsub(regex) { |matched_string| pre, url_bit, clickable = $1, $2, "##{$2}" diff --git a/spec/shared_behaviors/taggable.rb b/spec/shared_behaviors/taggable.rb index cec1e60fa..ea4957381 100644 --- a/spec/shared_behaviors/taggable.rb +++ b/spec/shared_behaviors/taggable.rb @@ -24,8 +24,15 @@ describe Diaspora::Taggable do @object.save! end - it 'links the tag to /p' do - Diaspora::Taggable.format_tags(@str).should include( tag_link('vöglein') ) + it "supports non-ascii characters" do + @object.tags(true).map(&:name).should include('vöglein') + end + + it 'links each tag' do + formatted_string = Diaspora::Taggable.format_tags(@str) + formatted_string.should include(tag_link('what')) + formatted_string.should include(tag_link('hey')) + formatted_string.should include(tag_link('vöglein')) end it 'responds to plain_text' do