2014-09-02 17:15:08 +08:00
|
|
|
require 'sanitize'
|
|
|
|
|
2013-05-24 02:26:51 +08:00
|
|
|
class Search
|
|
|
|
|
|
|
|
class GroupedSearchResults
|
2014-09-02 17:15:08 +08:00
|
|
|
include ActiveModel::Serialization
|
2013-05-24 02:26:51 +08:00
|
|
|
|
2014-09-02 17:15:08 +08:00
|
|
|
class TextHelper
|
|
|
|
extend ActionView::Helpers::TextHelper
|
2013-05-24 02:26:51 +08:00
|
|
|
end
|
|
|
|
|
2014-09-02 17:15:08 +08:00
|
|
|
attr_reader :type_filter,
|
|
|
|
:posts, :categories, :users,
|
|
|
|
:more_posts, :more_categories, :more_users,
|
|
|
|
:term, :search_context, :include_blurbs
|
|
|
|
|
|
|
|
def initialize(type_filter, term, search_context, include_blurbs)
|
|
|
|
@type_filter = type_filter
|
|
|
|
@term = term
|
|
|
|
@search_context = search_context
|
|
|
|
@include_blurbs = include_blurbs
|
|
|
|
@posts = []
|
|
|
|
@categories = []
|
|
|
|
@users = []
|
2013-05-24 02:26:51 +08:00
|
|
|
end
|
|
|
|
|
2014-09-02 17:15:08 +08:00
|
|
|
def blurb(post)
|
2015-06-25 03:08:22 +08:00
|
|
|
GroupedSearchResults.blurb_for(post.cooked, @term)
|
2014-09-02 17:15:08 +08:00
|
|
|
end
|
2013-05-24 02:26:51 +08:00
|
|
|
|
2014-09-02 17:15:08 +08:00
|
|
|
def add(object)
|
|
|
|
type = object.class.to_s.downcase.pluralize
|
2013-05-24 02:26:51 +08:00
|
|
|
|
2014-09-02 17:15:08 +08:00
|
|
|
if !@type_filter.present? && send(type).length == Search.per_facet
|
|
|
|
instance_variable_set("@more_#{type}".to_sym, true)
|
2013-05-24 02:26:51 +08:00
|
|
|
else
|
2014-09-02 17:15:08 +08:00
|
|
|
(send type) << object
|
2013-05-24 02:26:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-06-25 03:08:22 +08:00
|
|
|
|
|
|
|
def self.blurb_for(cooked, term=nil)
|
|
|
|
cooked = SearchObserver::HtmlScrubber.scrub(cooked).squish
|
|
|
|
|
|
|
|
blurb = nil
|
|
|
|
if term
|
|
|
|
terms = term.split(/\s+/)
|
|
|
|
blurb = TextHelper.excerpt(cooked, terms.first, radius: 100)
|
|
|
|
end
|
|
|
|
blurb = TextHelper.truncate(cooked, length: 200) if blurb.blank?
|
|
|
|
Sanitize.clean(blurb)
|
|
|
|
end
|
2013-05-24 02:26:51 +08:00
|
|
|
end
|
|
|
|
|
2014-02-17 11:34:14 +08:00
|
|
|
end
|