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.
This commit is contained in:
parent
3e70398c40
commit
2143e0f9ba
3 changed files with 17 additions and 5 deletions
|
|
@ -1,8 +1,13 @@
|
||||||
class ActsAsTaggableOn::Tag
|
class ActsAsTaggableOn::Tag
|
||||||
|
|
||||||
def followed_count
|
def followed_count
|
||||||
@followed_count ||= TagFollowing.where(:tag_id => self.id).count
|
@followed_count ||= TagFollowing.where(:tag_id => self.id).count
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.tag_text_regexp
|
||||||
|
@@tag_text_regexp ||= (RUBY_VERSION.include?('1.9') ? "[[:alnum:]]_-" : "\\w-")
|
||||||
|
end
|
||||||
|
|
||||||
def self.autocomplete(name)
|
def self.autocomplete(name)
|
||||||
where("name LIKE ?", "#{name.downcase}%")
|
where("name LIKE ?", "#{name.downcase}%")
|
||||||
end
|
end
|
||||||
|
|
@ -12,7 +17,7 @@ class ActsAsTaggableOn::Tag
|
||||||
# Special case for love, because the world needs more love.
|
# Special case for love, because the world needs more love.
|
||||||
'<3'
|
'<3'
|
||||||
elsif name
|
elsif name
|
||||||
name.gsub(/[^\w-]/, '')
|
name.gsub(/[^#{self.tag_text_regexp}]/, '')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -25,7 +25,7 @@ module Diaspora
|
||||||
end
|
end
|
||||||
|
|
||||||
def tag_strings
|
def tag_strings
|
||||||
regex = /(?:^|\s)#([\w-]+|<3)/
|
regex = /(?:^|\s)#([#{ActsAsTaggableOn::Tag.tag_text_regexp}]+|<3)/u
|
||||||
matches = self.
|
matches = self.
|
||||||
send( self.class.field_with_tags ).
|
send( self.class.field_with_tags ).
|
||||||
scan(regex).
|
scan(regex).
|
||||||
|
|
@ -41,7 +41,7 @@ module Diaspora
|
||||||
return text if opts[:plain_text]
|
return text if opts[:plain_text]
|
||||||
|
|
||||||
text = ERB::Util.h(text) unless opts[:no_escape]
|
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|
|
text.to_str.gsub(regex) { |matched_string|
|
||||||
pre, url_bit, clickable = $1, $2, "##{$2}"
|
pre, url_bit, clickable = $1, $2, "##{$2}"
|
||||||
|
|
|
||||||
|
|
@ -24,8 +24,15 @@ describe Diaspora::Taggable do
|
||||||
@object.save!
|
@object.save!
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'links the tag to /p' do
|
it "supports non-ascii characters" do
|
||||||
Diaspora::Taggable.format_tags(@str).should include( tag_link('vöglein') )
|
@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
|
end
|
||||||
|
|
||||||
it 'responds to plain_text' do
|
it 'responds to plain_text' do
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue