Guest User

Untitled

a guest
May 26th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. class SearchesController < ApplicationController
  2. def show
  3. @search = Post.search do
  4. if params[:q].present?
  5. keywords(params[:q]) do
  6. highlight :body
  7. end
  8. end
  9. if params[:cat].present?
  10. with(:category_id, params[:cat].to_i)
  11. end
  12. if params[:tags].present?
  13. with(:tags).all_of(params[:tags])
  14. end
  15. with(:published_at).less_than(Time.now)
  16. facet :category_id
  17. facet :tags, :limit => 10
  18. if params[:q].blank?
  19. order_by :published_at, :desc
  20. end
  21. paginate(:page => params[:page], :per_page => 10)
  22. data_accessor_for(Post).include = :category
  23. end
  24. end
  25. end
  26. %ul.facets
  27. %li
  28. - if params[:cat].present?
  29. %h4== Category: #{Category.find(params[:cat]).name} #{link_to('[x]', params.except(:cat))}
  30. -else
  31. %h4 Category
  32. %ul
  33. - @search.facet(:category_id).rows.each do |row|
  34. %li== #{link_to(row.instance.name, params.merge(:cat => row.value).except(:page))} (#{row.count})
  35. %li
  36. %h4 Tag
  37. %ul
  38. - @search.facet(:tags).rows.each do |row|
  39. %li== #{link_to_unless((params[:tags] || []).include?(row.value), row.value, params.merge(:tags => ((params[:tags] || []) + [row.value])).except(:page))} (#{row.count})
  40.  
  41.  
  42. %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}
  43.  
  44. %ul.searchResults
  45. - @search.each_hit_with_result do |hit, result|
  46. %li
  47. %h2= h(result.title)
  48. - if params[:q].present?
  49. %p.score= hit.score
  50. %h4.timestamp= result.published_at.to_s
  51. %p= best_hit_description(hit)
  52. %h6.category== Category: #{h(result.category.name)}
  53. %h6.tags== Tags: #{h(result.tags)}
  54.  
  55. .pagination= will_paginate(@search.hits)
Add Comment
Please, Sign In to add comment