2014-09-02 05:15:08 -04:00
|
|
|
require 'sanitize'
|
|
|
|
|
2013-05-23 14:26:51 -04:00
|
|
|
class Search
|
|
|
|
|
|
|
|
class GroupedSearchResults
|
2014-09-02 05:15:08 -04:00
|
|
|
include ActiveModel::Serialization
|
2013-05-23 14:26:51 -04:00
|
|
|
|
2014-09-02 05:15:08 -04:00
|
|
|
class TextHelper
|
|
|
|
extend ActionView::Helpers::TextHelper
|
2013-05-23 14:26:51 -04:00
|
|
|
end
|
|
|
|
|
2017-07-17 11:57:13 -04:00
|
|
|
attr_reader(
|
|
|
|
:type_filter,
|
|
|
|
:posts,
|
|
|
|
:categories,
|
|
|
|
:users,
|
|
|
|
:more_posts,
|
|
|
|
:more_categories,
|
|
|
|
:more_users,
|
|
|
|
:term,
|
|
|
|
:search_context,
|
|
|
|
:include_blurbs
|
|
|
|
)
|
|
|
|
|
|
|
|
attr_accessor :search_log_id
|
2014-09-02 05:15:08 -04:00
|
|
|
|
2015-07-27 02:13:11 -04:00
|
|
|
def initialize(type_filter, term, search_context, include_blurbs, blurb_length)
|
2014-09-02 05:15:08 -04:00
|
|
|
@type_filter = type_filter
|
|
|
|
@term = term
|
|
|
|
@search_context = search_context
|
|
|
|
@include_blurbs = include_blurbs
|
2015-07-27 02:46:50 -04:00
|
|
|
@blurb_length = blurb_length || 200
|
2014-09-02 05:15:08 -04:00
|
|
|
@posts = []
|
|
|
|
@categories = []
|
|
|
|
@users = []
|
2013-05-23 14:26:51 -04:00
|
|
|
end
|
|
|
|
|
2016-03-18 01:26:20 -04:00
|
|
|
def find_user_data(guardian)
|
|
|
|
if user = guardian.user
|
|
|
|
topics = @posts.map(&:topic)
|
|
|
|
topic_lookup = TopicUser.lookup_for(user, topics)
|
|
|
|
topics.each { |ft| ft.user_data = topic_lookup[ft.id] }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-09-02 05:15:08 -04:00
|
|
|
def blurb(post)
|
2015-07-27 02:13:11 -04:00
|
|
|
GroupedSearchResults.blurb_for(post.cooked, @term, @blurb_length)
|
2014-09-02 05:15:08 -04:00
|
|
|
end
|
2013-05-23 14:26:51 -04:00
|
|
|
|
2014-09-02 05:15:08 -04:00
|
|
|
def add(object)
|
|
|
|
type = object.class.to_s.downcase.pluralize
|
2013-05-23 14:26:51 -04:00
|
|
|
|
2014-09-02 05:15:08 -04:00
|
|
|
if !@type_filter.present? && send(type).length == Search.per_facet
|
|
|
|
instance_variable_set("@more_#{type}".to_sym, true)
|
2013-05-23 14:26:51 -04:00
|
|
|
else
|
2014-09-02 05:15:08 -04:00
|
|
|
(send type) << object
|
2013-05-23 14:26:51 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-06-24 15:08:22 -04:00
|
|
|
|
2015-07-27 02:46:50 -04:00
|
|
|
def self.blurb_for(cooked, term=nil, blurb_length=200)
|
2016-12-21 21:13:14 -05:00
|
|
|
cooked = SearchIndexer::HtmlScrubber.scrub(cooked).squish
|
2015-06-24 15:08:22 -04:00
|
|
|
|
|
|
|
blurb = nil
|
|
|
|
if term
|
|
|
|
terms = term.split(/\s+/)
|
2015-07-27 02:13:11 -04:00
|
|
|
blurb = TextHelper.excerpt(cooked, terms.first, radius: blurb_length / 2, seperator: " ")
|
2015-06-24 15:08:22 -04:00
|
|
|
end
|
2015-07-27 02:13:11 -04:00
|
|
|
blurb = TextHelper.truncate(cooked, length: blurb_length, seperator: " ") if blurb.blank?
|
2015-06-24 15:08:22 -04:00
|
|
|
Sanitize.clean(blurb)
|
|
|
|
end
|
2013-05-23 14:26:51 -04:00
|
|
|
end
|
|
|
|
|
2014-02-16 22:34:14 -05:00
|
|
|
end
|