Last active
December 18, 2015 20:49
-
-
Save rossnelson/5843388 to your computer and use it in GitHub Desktop.
sunspot results
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| - @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