Tags now link to meaningless query in PostsController

This commit is contained in:
Raphael Sofaer 2011-03-10 17:56:40 -08:00
parent f19857cc40
commit ea98b1ca81
2 changed files with 50 additions and 14 deletions

View file

@ -40,10 +40,28 @@ class StatusMessage < Post
def formatted_message(opts={})
return self.raw_message unless self.raw_message
escaped_message = opts[:plain_text] ? self.raw_message: ERB::Util.h(self.raw_message)
mentioned_message = self.format_mentions(escaped_message, opts)
self.format_tags(mentioned_message, opts)
end
def format_tags(text, opts={})
return text if opts[:plain_text]
regex = /(^|\s)#(\w+)/
form_message = text.gsub(regex) do |matched_string|
tag = self.tags.detect do |t|
t.name == $~[2]
end
tag ? "#{$~[1]}<a href=\"/p?tag=#{tag.name}\" class=\"tag\">##{ERB::Util.h(tag.name)}</a>" : ERB::Util.h($~[2])
end
form_message
end
def format_mentions(text, opts = {})
people = self.mentioned_people
regex = /@\{([^;]+); ([^\}]+)\}/
escaped_message = opts[:plain_text] ? raw_message : ERB::Util.h(raw_message)
form_message = escaped_message.gsub(regex) do |matched_string|
form_message = text.gsub(regex) do |matched_string|
person = people.detect{ |p|
p.diaspora_handle == $~[2]
}

View file

@ -6,6 +6,11 @@ require 'spec_helper'
describe StatusMessage do
include ActionView::Helpers::UrlHelper
include Rails.application.routes.url_helpers
def controller
mock()
end
before do
@user = alice
@ -68,12 +73,6 @@ describe StatusMessage do
end
describe 'mentions' do
def controller
mock()
end
include ActionView::Helpers::UrlHelper
include Rails.application.routes.url_helpers
before do
@people = [alice, bob, eve].map{|u| u.person}
@test_string = <<-STR
@ -83,9 +82,9 @@ STR
@sm = Factory.create(:status_message, :message => @test_string )
end
describe '#formatted_message' do
describe '#format_mentions' do
it 'adds the links in the formated message text' do
@sm.formatted_message.should == <<-STR
@sm.format_mentions(@sm.raw_message).should == <<-STR
#{link_to('@' << @people[0].name, person_path(@people[0]), :class => 'mention')} can mention people like Raphael #{link_to('@' << @people[1].name, person_path(@people[1]), :class => 'mention')}
can mention people like Raphaellike Raphael #{link_to('@' << @people[2].name, person_path(@people[2]), :class => 'mention')} can mention people like Raph
STR
@ -94,13 +93,13 @@ STR
context 'with :plain_text option' do
it 'removes the mention syntax and displays the unformatted name' do
status = Factory(:status_message, :message => "@{Barack Obama; barak@joindiaspora.com } is so cool @{Barack Obama; barak@joindiaspora.com } ")
status.formatted_message(:plain_text => true).should == 'Barack Obama is so cool Barack Obama '
status.format_mentions(status.raw_message, :plain_text => true).should == 'Barack Obama is so cool Barack Obama '
end
end
it 'leaves the name of people that cannot be found' do
@sm.stub(:mentioned_people).and_return([])
@sm.formatted_message.should == <<-STR
@sm.format_mentions(@sm.raw_message).should == <<-STR
Raphael can mention people like Raphael Ilya
can mention people like Raphaellike Raphael Daniel can mention people like Raph
STR
@ -113,8 +112,10 @@ STR
.values
p.save!
@sm.formatted_message.should_not include(@people[0].profile.first_name)
@sm.format_mentions(@sm.raw_message).should_not include(@people[0].profile.first_name)
end
end
describe '#formatted_message' do
it 'escapes the message' do
xss = "</a> <script> alert('hey'); </script>"
@sm.message << xss
@ -177,6 +178,23 @@ STR
before do
@sm = Factory.build(:status_message)
end
describe '#format_tags' do
before do
@str = '#what #hey'
@sm.message = @str
@sm.build_tags
@sm.save
@sm.reload
end
it 'links the tag to /p' do
link = link_to('#what', posts_path(:tag => 'what'), :class => 'tag')
@sm.format_tags(@str).should include(link)
end
it 'responds to plain_text' do
@sm.format_tags(@str, :plain_text => true).should == @str
end
end
describe '#build_tags' do
it 'builds the tags' do
@sm.message = '#what'