Don't count @mentions as links in a post

This commit is contained in:
Robin Ward 2013-03-25 13:12:15 -04:00
parent 6568b4aaa9
commit c372e36cb6
2 changed files with 19 additions and 4 deletions

View file

@ -110,7 +110,17 @@ class Post < ActiveRecord::Base
def link_count def link_count
return 0 unless raw.present? return 0 unless raw.present?
cooked_document.search("a[href]").count
# Don't include @mentions in the link count
total = 0
cooked_document.search("a[href]").each do |l|
html_class = l.attributes['class']
if html_class.present?
next if html_class.to_s == 'mention' && l.attributes['href'].to_s =~ /^\/users\//
end
total +=1
end
total
end end
def max_mention_validator def max_mention_validator

View file

@ -149,16 +149,21 @@ describe Post do
let(:visitor) { Fabricate(:user, trust_level: TrustLevel.levels[:visitor]) } let(:visitor) { Fabricate(:user, trust_level: TrustLevel.levels[:visitor]) }
let(:post_one_link) { Fabricate.build(:post, post_args.merge(raw: "[sherlock](http://www.bbc.co.uk/programmes/b018ttws)", user: visitor)) } let(:post_one_link) { Fabricate.build(:post, post_args.merge(raw: "[sherlock](http://www.bbc.co.uk/programmes/b018ttws)", user: visitor)) }
let(:post_two_links) { Fabricate.build(:post, post_args.merge(raw: "<a href='http://discourse.org'>discourse</a> <a href='http://twitter.com'>twitter</a>", user: visitor)) } let(:post_two_links) { Fabricate.build(:post, post_args.merge(raw: "<a href='http://discourse.org'>discourse</a> <a href='http://twitter.com'>twitter</a>", user: visitor)) }
let(:post_with_mentions) { Fabricate.build(:post, post_args.merge(raw: "hello @#{visitor.username} how are you doing?") )}
it "returns 0 images for an empty post" do it "returns 0 links for an empty post" do
Fabricate.build(:post).link_count.should == 0 Fabricate.build(:post).link_count.should == 0
end end
it "finds images from markdown" do it "returns 0 links for a post with mentions" do
post_with_mentions.link_count.should == 0
end
it "finds links from markdown" do
post_one_link.link_count.should == 1 post_one_link.link_count.should == 1
end end
it "finds images from HTML" do it "finds links from HTML" do
post_two_links.link_count.should == 2 post_two_links.link_count.should == 2
end end