Skip to content

Commit

Permalink
Merge remote-tracking branch 'markzalar/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
carols10cents committed Nov 18, 2011
2 parents 4355d8d + 0b4cd73 commit d9fa29a
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 6 deletions.
6 changes: 5 additions & 1 deletion app/controllers/searches_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ def show
@updates = []
if params[:q]
set_params_page
@updates = Update.where(:text => /\b#{Regexp.quote(params[:q])}\b/i).paginate(:page => params[:page], :per_page => params[:per_page], :order => :created_at.desc)
@leading_char = '\b'
if params[:q][0] == '#'
@leading_char = ''
end
@updates = Update.where(:text => /#{@leading_char}#{Regexp.quote(params[:q])}\b/i).paginate(:page => params[:page], :per_page => params[:per_page], :order => :created_at.desc)
set_pagination_buttons(@updates)
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/update.rb
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ def generate_html
end

out.gsub!(/(^|\s+)#(\w+)/) do |match|
"#{$1}<a href='/hashtags/#{$2}'>##{$2}</a>"
"#{$1}<a href='/search?q=%23#{$2}'>##{$2}</a>"
end

self.html = out
Expand Down
10 changes: 10 additions & 0 deletions test/acceptance/search_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,15 @@

assert_match @update_text, page.body
end

it "gets a match for hashtag search" do
@hashtag_update_text = "This is a test #hashtag"
Factory(:update, :text => @hashtag_update_text)
visit "/search"
fill_in "q", :with => "#hashtag"
click_button "Search"

assert has_link? "#hashtag"
end
end
end
20 changes: 19 additions & 1 deletion test/acceptance/update_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
assert_match page.body, /#{update.text}/
end

it "shows an update in reply to another upate" do
it "shows an update in reply to another update" do
update = Factory(:update)
update2 = Factory(:update)
update2.referral_id = update.id
Expand All @@ -117,6 +117,24 @@
assert_match page.body, /#{update.text}/
end

describe "update with hashtag" do
it "creates a working hashtag link" do
u = Factory(:user)
a = Factory(:authorization, :user => u)

log_in(u, a.uid)

visit "/updates"
fill_in "text", :with => "So this one time #coolstorybro"
VCR.use_cassette('publish_to_hub') {click_button "Share"}

visit "/updates"
click_link "#coolstorybro"
assert_match "Search Updates", page.body
assert has_link? "#coolstorybro"
end
end

describe "pagination" do
it "does not paginate when there are too few" do
5.times do
Expand Down
6 changes: 3 additions & 3 deletions test/models/update_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,22 +113,22 @@
describe "hashtags" do
it "makes links if hash starts a word (before create)" do
u = Factory.build(:update, :text => "This is a message with a #hashtag.")
assert_match /<a href='\/hashtags\/hashtag'>#hashtag<\/a>/, u.to_html
assert_match /<a href='\/search\?q=%23hashtag'>#hashtag<\/a>/, u.to_html
u = Factory.build(:update, :text => "This is a message with a#hashtag.")
assert_equal "This is a message with a#hashtag.", u.to_html
end

it "makes links if hash starts a word (after create)" do
u = Factory(:update, :text => "This is a message with a #hashtag.")
assert_match /<a href='\/hashtags\/hashtag'>#hashtag<\/a>/, u.to_html
assert_match /<a href='\/search\?q=%23hashtag'>#hashtag<\/a>/, u.to_html
u = Factory(:update, :text => "This is a message with a#hashtag.")
assert_equal "This is a message with a#hashtag.", u.to_html
end

it "makes links for both a hashtag and a URL (after create)" do
u = Factory(:update, :text => "This is a message with a #hashtag and mentions http://rstat.us/.")

assert_match /<a href='\/hashtags\/hashtag'>#hashtag<\/a>/, u.to_html
assert_match /<a href='\/search\?q=%23hashtag'>#hashtag<\/a>/, u.to_html
assert_match /<a href='http:\/\/rstat.us\/'>http:\/\/rstat.us\/<\/a>/, u.to_html
end

Expand Down

0 comments on commit d9fa29a

Please sign in to comment.