Skip to content

Instantly share code, notes, and snippets.

@rossnelson
Last active December 18, 2015 20:49
Show Gist options
  • Save rossnelson/5843388 to your computer and use it in GitHub Desktop.
Save rossnelson/5843388 to your computer and use it in GitHub Desktop.
sunspot results
class Result
attr_accessor :title, :body, :url
def teaser_body
body.gsub(%r{</?[^>]+?>}, '')
end
def self.parse(results)
results.map do |r|
"Result::#{r.class}".constantize.new(r.id)
end
end
end
class Result
class Page < Result
def initialize(id)
@object = ::Page.find id
@title = @object.meta_title
@body = @object.meta_description
@url = @object.menu_item.url
end
end
end
class Result
class Post < Result
def initialize(id)
@object = ::Post.find id
@title = @object.title
@body = @object.body
@url = @object.menu_item.url
end
end
end
def index
@search = Sunspot.search(Dust.config.searchables) do
fulltext(params[:search][:query])
paginate :page => 1, :per_page => 50
end
@results = Result.parse(@search.results)
end
- @results.each do |result|
.result
%h3= link_to result.title, result.url
%p
- if result.teaser_body.blank?
Empty Body
- else
= truncate result.teaser_body, :length => 500
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment