write formatted_message

This commit is contained in:
Raphael 2011-02-02 16:56:31 -08:00 committed by Raphael Sofaer
parent 51e129b5c6
commit 216a2c3cdb
2 changed files with 40 additions and 1 deletions

View file

@ -22,6 +22,24 @@ class StatusMessage < Post
get_youtube_title message
end
def formatted_message
people = self.mentioned_people
regex = /@\{([^;]+); ([^\}]+)\}/
message.gsub(regex) do |matched_string|
people.detect{ |p|
p.diaspora_handle == matched_string.match(regex).captures.last
}.name
end
end
def mentioned_people
regex = /@\{([^;]+); ([^\}]+)\}/
identifiers = self.message.scan(regex).map do |match|
match.last
end
Person.where(:diaspora_handle => identifiers)
end
def to_activity
<<-XML
<entry>

View file

@ -51,9 +51,30 @@ describe StatusMessage do
status = Factory.build(:status_message, :message => message)
status.should_not be_valid
end
describe 'mentions' do
before do
@people = [alice, bob, eve].map{|u| u.person}
@test_string = <<-STR
@{Raphael; #{@people[0].diaspora_handle}} can mention people like Raphael @{Ilya; #{@people[1].diaspora_handle}}
can mention people like Raphaellike Raphael @{Daniel; #{@people[2].diaspora_handle}} can mention people like Raph
STR
@sm = Factory.create(:status_message, :message => @test_string )
end
it 'adds the links in the formated message text' do
@sm.formatted_message.should == <<-STR
#{@people[0].name} can mention people like Raphael #{@people[1].name}
can mention people like Raphaellike Raphael #{@people[2].name} can mention people like Raph
STR
end
it 'extracts the mentioned people from the message' do
@sm.mentioned_people.to_set.should == @people.to_set
end
end
describe "XML" do
before do
@message = Factory.create(:status_message, :message => "I hate WALRUSES!", :person => @user.person)