FEATURE: highlight search results

This commit is contained in:
Sam 2014-09-03 17:08:47 +10:00
parent d4f08f442c
commit 9c29c1c072
3 changed files with 8 additions and 13 deletions

View File

@ -8,10 +8,6 @@ class Search
class TextHelper
extend ActionView::Helpers::TextHelper
def self.sanitize(text)
# we run through sanitize at the end so it does not matter
text
end
end
attr_reader :type_filter,
@ -33,15 +29,14 @@ class Search
cooked = SearchObserver::HtmlScrubber.scrub(post.cooked).squish
terms = @term.split(/\s+/)
blurb = TextHelper.excerpt(cooked, terms.first, radius: 100)
# TODO highlight term
# terms.each do |term|
# blurb = TextHelper.highlight(blurb, term)
# end
blurb = TextHelper.truncate(cooked, length: 200) if blurb.blank?
blurb = Sanitize.clean(blurb)
Sanitize.clean(blurb)
terms.each do |term|
blurb.gsub!(Regexp.new("(#{Regexp.escape(term)})", Regexp::IGNORECASE), "<span class='highlighted'>\\1</span>")
end
blurb
end
def add(object)

View File

@ -145,7 +145,7 @@ describe Search do
p = result.posts[0]
p.topic.id.should == topic.id
p.id.should == reply.id
result.blurb(p).should == "this reply has no quotes"
result.blurb(p).should == "this reply has no <span class='highlighted'>quotes</span>"
end
end

View File

@ -15,7 +15,7 @@ describe SearchController do
response.should be_success
data = JSON.parse(response.body)
data['posts'][0]['id'].should == my_post.id
data['posts'][0]['blurb'].should == 'this is my really awesome post'
data['posts'][0]['blurb'].should == 'this is my really <span class=\'highlighted\'>awesome</span> post'
data['topics'][0]['id'].should == my_post.topic_id
end
end