Skip to content

Commit

Permalink
Added the tag cloud
Browse files Browse the repository at this point in the history
  • Loading branch information
peterhellberg committed May 6, 2010
1 parent 69a0253 commit e324cd9
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
6 changes: 4 additions & 2 deletions app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,13 @@ class Search
end

search_result = UR::Product.search(search_params)
tag_cloud = (search_result.ok?) ? build_tag_cloud(search_result) : false

haml :index, :locals => {
:page_title => 'UR Produktsök',
:current_page => current_page,
:search => search_result,
:tag_cloud => tag_cloud,
:facet_order => [
['search_product_type', 'Typ'],
['typicalagerange', 'Målgrupp'],
Expand All @@ -65,7 +67,7 @@ class Search
post '/autocomplete.json' do
content_type :json
term = URI.escape(params[:value])
url = UR::Search::SEARCH_SERVICE_URL +
url = UR::Search::SEARCH_SERVICE_URL +
"/terms?wt=json&terms.fl=search_ao&terms.prefix=#{term}"
response = JSON.parse(RestClient.get(url).body)

Expand All @@ -77,4 +79,4 @@ class Search
get '/stylesheets/style.css' do
content_type :css
sass :style
end
end
18 changes: 17 additions & 1 deletion lib/ur_helpers.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'lib/tag_cloud'

# Monkeypatch
class Object
def blank?
Expand All @@ -8,6 +10,21 @@ def blank?
module Sinatra
module URHelpers

def build_tag_cloud(search_result)
cloud = TagCloud.new

# puts search_result.facets.inspect
tags = search_result.facets['ao'].slice(0,15)

if tags.size > 1
tags.each do |tag|
cloud.add(tag.value, facet_link('ao', tag), tag.hits)
end
end

cloud
end

def should_show_results?
!(params[:fq].blank? && params[:q].blank?)
end
Expand Down Expand Up @@ -48,7 +65,6 @@ def remove_facet_link(name, facet)
end

# Translations

def translated_facet(name, facet)
case name
when 'search_product_type' then translated_product_type(facet.value)
Expand Down
8 changes: 7 additions & 1 deletion views/index.haml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,13 @@
%div.grid-4
%div.sidebar
%h3
="Filtrera träffar"
= "Filtrera träffar"
- if tag_cloud
%div.tag_cloud
%h4= "Ämnesord"
%ul
%li
= tag_cloud.html_and_css(15)
- facet_order.each do |facet_name, facet_title|
- if !search.facets[facet_name].nil? && search.facets[facet_name].size > 0
%div.facet{ :class => facet_name }
Expand Down
4 changes: 3 additions & 1 deletion views/style.sass
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ body

div.main_content
color= !ur_dark_gray

p.easy_to_read_description
font-weight: bold
margin-bottom: 0.6em
h3
font-size: 1.5em
color= !ur_gray
Expand Down

0 comments on commit e324cd9

Please sign in to comment.