Skip to content

Instantly share code, notes, and snippets.

@bogdan
Created May 29, 2010 10:33
Show Gist options
  • Save bogdan/418192 to your computer and use it in GitHub Desktop.
Save bogdan/418192 to your computer and use it in GitHub Desktop.
class SearchesController < ApplicationController
def show
@search = Post.search do
if params[:q].present?
keywords(params[:q]) do
highlight :body
end
end
if params[:cat].present?
with(:category_id, params[:cat].to_i)
end
if params[:tags].present?
with(:tags).all_of(params[:tags])
end
with(:published_at).less_than(Time.now)
facet :category_id
facet :tags, :limit => 10
if params[:q].blank?
order_by :published_at, :desc
end
paginate(:page => params[:page], :per_page => 10)
data_accessor_for(Post).include = :category
end
end
end
%ul.facets
%li
- if params[:cat].present?
%h4== Category: #{Category.find(params[:cat]).name} #{link_to('[x]', params.except(:cat))}
-else
%h4 Category
%ul
- @search.facet(:category_id).rows.each do |row|
%li== #{link_to(row.instance.name, params.merge(:cat => row.value).except(:page))} (#{row.count})
%li
%h4 Tag
%ul
- @search.facet(:tags).rows.each do |row|
%li== #{link_to_unless((params[:tags] || []).include?(row.value), row.value, params.merge(:tags => ((params[:tags] || []) + [row.value])).except(:page))} (#{row.count})
%h4== Displaying posts #{(@search.hits.current_page - 1) * @search.hits.per_page + 1} - #{[@search.hits.current_page * @search.hits.per_page, @search.total].min} of #{@search.total}
%ul.searchResults
- @search.each_hit_with_result do |hit, result|
%li
%h2= h(result.title)
- if params[:q].present?
%p.score= hit.score
%h4.timestamp= result.published_at.to_s
%p= best_hit_description(hit)
%h6.category== Category: #{h(result.category.name)}
%h6.tags== Tags: #{h(result.tags)}
.pagination= will_paginate(@search.hits)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment