Merge pull request #2273 from paully21/development-branch
Add blurb of post to search results via API
This commit is contained in:
commit
b2cb4b27a1
|
@ -8,9 +8,12 @@ class SearchController < ApplicationController
|
||||||
|
|
||||||
def query
|
def query
|
||||||
params.require(:term)
|
params.require(:term)
|
||||||
|
|
||||||
search_args = {guardian: guardian}
|
search_args = {guardian: guardian}
|
||||||
search_args[:type_filter] = params[:type_filter] if params[:type_filter].present?
|
search_args[:type_filter] = params[:type_filter] if params[:type_filter].present?
|
||||||
|
if params[:include_blurbs].present?
|
||||||
|
search_args[:include_blurbs] = params[:include_blurbs] == "true"
|
||||||
|
end
|
||||||
|
|
||||||
search_context = params[:search_context]
|
search_context = params[:search_context]
|
||||||
if search_context.present?
|
if search_context.present?
|
||||||
|
|
|
@ -44,6 +44,7 @@ class Search
|
||||||
@opts = opts || {}
|
@opts = opts || {}
|
||||||
@guardian = @opts[:guardian] || Guardian.new
|
@guardian = @opts[:guardian] || Guardian.new
|
||||||
@search_context = @opts[:search_context]
|
@search_context = @opts[:search_context]
|
||||||
|
@include_blurbs = @opts[:include_blurbs] || false
|
||||||
@limit = Search.per_facet * Search.facets.size
|
@limit = Search.per_facet * Search.facets.size
|
||||||
@results = GroupedSearchResults.new(@opts[:type_filter])
|
@results = GroupedSearchResults.new(@opts[:type_filter])
|
||||||
|
|
||||||
|
@ -101,7 +102,7 @@ class Search
|
||||||
extra_posts = posts_query(expected_topics * Search.burst_factor)
|
extra_posts = posts_query(expected_topics * Search.burst_factor)
|
||||||
extra_posts = extra_posts.where("posts.topic_id NOT in (?)", @results.topic_ids) if @results.topic_ids.present?
|
extra_posts = extra_posts.where("posts.topic_id NOT in (?)", @results.topic_ids) if @results.topic_ids.present?
|
||||||
extra_posts.each do |p|
|
extra_posts.each do |p|
|
||||||
@results.add_result(SearchResult.from_post(p, @search_context, @term))
|
@results.add_result(SearchResult.from_post(p, @search_context, @term, @include_blurbs))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -213,7 +214,7 @@ class Search
|
||||||
|
|
||||||
|
|
||||||
posts.each do |p|
|
posts.each do |p|
|
||||||
@results.add_result(SearchResult.from_post(p, @search_context, @term))
|
@results.add_result(SearchResult.from_post(p, @search_context, @term, @include_blurbs))
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -5,7 +5,7 @@ class Search
|
||||||
extend ActionView::Helpers::TextHelper
|
extend ActionView::Helpers::TextHelper
|
||||||
end
|
end
|
||||||
|
|
||||||
attr_accessor :type, :id, :topic_id
|
attr_accessor :type, :id, :topic_id, :blurb
|
||||||
|
|
||||||
# Category attributes
|
# Category attributes
|
||||||
attr_accessor :color, :text_color
|
attr_accessor :color, :text_color
|
||||||
|
@ -16,7 +16,7 @@ class Search
|
||||||
def initialize(row)
|
def initialize(row)
|
||||||
row.symbolize_keys!
|
row.symbolize_keys!
|
||||||
@type = row[:type].to_sym
|
@type = row[:type].to_sym
|
||||||
@url, @id, @title, @topic_id = row[:url], row[:id], row[:title], row[:topic_id]
|
@url, @id, @title, @topic_id, @blurb = row[:url], row[:id], row[:title], row[:topic_id], row[:blurb]
|
||||||
end
|
end
|
||||||
|
|
||||||
def as_json(options = nil)
|
def as_json(options = nil)
|
||||||
|
@ -24,6 +24,7 @@ class Search
|
||||||
json[:avatar_template] = @avatar_template if @avatar_template.present?
|
json[:avatar_template] = @avatar_template if @avatar_template.present?
|
||||||
json[:color] = @color if @color.present?
|
json[:color] = @color if @color.present?
|
||||||
json[:text_color] = @text_color if @text_color.present?
|
json[:text_color] = @text_color if @text_color.present?
|
||||||
|
json[:blurb] = @blurb if @blurb.present?
|
||||||
json
|
json
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -40,11 +41,11 @@ class Search
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.from_topic(t, custom_title=nil)
|
def self.from_topic(t, options = {})
|
||||||
SearchResult.new(type: :topic, topic_id: t.id, id: t.id, title: custom_title || t.title, url: t.relative_url)
|
SearchResult.new(type: :topic, topic_id: t.id, id: t.id, title: options[:custom_title] || t.title, url: t.relative_url, blurb: options[:custom_blurb])
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.from_post(p, context, term)
|
def self.from_post(p, context, term, include_blurbs=false)
|
||||||
custom_title =
|
custom_title =
|
||||||
if context && context.id == p.topic_id
|
if context && context.id == p.topic_id
|
||||||
# TODO: rewrite this
|
# TODO: rewrite this
|
||||||
|
@ -58,14 +59,18 @@ class Search
|
||||||
excerpt: excerpt
|
excerpt: excerpt
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
if include_blurbs
|
||||||
|
#add a blurb from the post to the search results
|
||||||
|
custom_blurb = TextHelper.excerpt(p.raw, term.split(/\s+/)[0], radius: 100)
|
||||||
|
custom_blurb = TextHelper.truncate(p.raw, length: 200) if custom_blurb.blank?
|
||||||
|
end
|
||||||
if p.post_number == 1
|
if p.post_number == 1
|
||||||
# we want the topic link when it's the OP
|
# we want the topic link when it's the OP
|
||||||
SearchResult.from_topic(p.topic, custom_title)
|
SearchResult.from_topic(p.topic, {custom_title: custom_title, custom_blurb: custom_blurb})
|
||||||
elsif context && context.id == p.topic_id
|
elsif context && context.id == p.topic_id
|
||||||
SearchResult.new(type: :topic, topic_id: p.topic_id, id: "_#{p.id}", title: custom_title, url: p.url)
|
SearchResult.new(type: :topic, topic_id: p.topic_id, id: "_#{p.id}", title: custom_title, url: p.url, blurb: custom_blurb)
|
||||||
else
|
else
|
||||||
SearchResult.new(type: :topic, topic_id: p.topic_id, id: p.topic.id, title: p.topic.title, url: p.url)
|
SearchResult.new(type: :topic, topic_id: p.topic_id, id: p.topic.id, title: p.topic.title, url: p.url, blurb: custom_blurb)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -5,6 +5,10 @@ require_dependency 'search'
|
||||||
|
|
||||||
describe Search do
|
describe Search do
|
||||||
|
|
||||||
|
class TextHelper
|
||||||
|
extend ActionView::Helpers::TextHelper
|
||||||
|
end
|
||||||
|
|
||||||
before do
|
before do
|
||||||
ActiveRecord::Base.observers.enable :search_observer
|
ActiveRecord::Base.observers.enable :search_observer
|
||||||
end
|
end
|
||||||
|
@ -175,13 +179,14 @@ describe Search do
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'searching the OP' do
|
context 'searching the OP' do
|
||||||
let!(:post) { Fabricate(:post, topic: topic, user: topic.user) }
|
let!(:post) { Fabricate(:post_with_long_raw_content, topic: topic, user: topic.user) }
|
||||||
let(:result) { first_of_type(Search.new('hello', type_filter: 'topic').execute, 'topic') }
|
let(:result) { first_of_type(Search.new('hundred', type_filter: 'topic', include_blurbs: true).execute, 'topic') }
|
||||||
|
|
||||||
it 'returns a result correctly' do
|
it 'returns a result correctly' do
|
||||||
result.should be_present
|
result.should be_present
|
||||||
result[:title].should == topic.title
|
result[:title].should == topic.title
|
||||||
result[:url].should == topic.relative_url
|
result[:url].should == topic.relative_url
|
||||||
|
result[:blurb].should == TextHelper.excerpt(post.raw, 'hundred', radius: 100)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -26,6 +26,17 @@ describe SearchController do
|
||||||
xhr :get, :query, term: 'test', type_filter: 'topic'
|
xhr :get, :query, term: 'test', type_filter: 'topic'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "performs the query and returns results including blurbs" do
|
||||||
|
guardian = Guardian.new
|
||||||
|
Guardian.stubs(:new).returns(guardian)
|
||||||
|
|
||||||
|
search = mock()
|
||||||
|
Search.expects(:new).with('test', guardian: guardian, include_blurbs: true).returns(search)
|
||||||
|
search.expects(:execute)
|
||||||
|
|
||||||
|
xhr :get, :query, term: 'test', include_blurbs: 'true'
|
||||||
|
end
|
||||||
|
|
||||||
context "search context" do
|
context "search context" do
|
||||||
|
|
||||||
it "raises an error with an invalid context type" do
|
it "raises an error with an invalid context type" do
|
||||||
|
|
|
@ -4,6 +4,12 @@ Fabricator(:post) do
|
||||||
raw "Hello world"
|
raw "Hello world"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Fabricator(:post_with_long_raw_content, from: :post) do
|
||||||
|
raw 'This is a sample post with semi-long raw content. The raw content is also more than
|
||||||
|
two hundred characters to satisfy any test conditions that require content longer
|
||||||
|
than the typical test post raw content.'
|
||||||
|
end
|
||||||
|
|
||||||
Fabricator(:post_with_youtube, from: :post) do
|
Fabricator(:post_with_youtube, from: :post) do
|
||||||
cooked '<p><a href="http://www.youtube.com/watch?v=9bZkp7q19f0" class="onebox" target="_blank">http://www.youtube.com/watch?v=9bZkp7q19f0</a></p>'
|
cooked '<p><a href="http://www.youtube.com/watch?v=9bZkp7q19f0" class="onebox" target="_blank">http://www.youtube.com/watch?v=9bZkp7q19f0</a></p>'
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue