From e324cd9e044c13469abe3c9706cfaa65051d47e2 Mon Sep 17 00:00:00 2001 From: Peter Hellberg Date: Thu, 6 May 2010 16:19:20 +0200 Subject: [PATCH] Added the tag cloud --- app.rb | 6 ++++-- lib/ur_helpers.rb | 18 +++++++++++++++++- views/index.haml | 8 +++++++- views/style.sass | 4 +++- 4 files changed, 31 insertions(+), 5 deletions(-) diff --git a/app.rb b/app.rb index 5e26fcb..e582b7f 100644 --- a/app.rb +++ b/app.rb @@ -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'], @@ -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) @@ -77,4 +79,4 @@ class Search get '/stylesheets/style.css' do content_type :css sass :style -end \ No newline at end of file +end diff --git a/lib/ur_helpers.rb b/lib/ur_helpers.rb index 7a99629..51c39f8 100644 --- a/lib/ur_helpers.rb +++ b/lib/ur_helpers.rb @@ -1,3 +1,5 @@ +require 'lib/tag_cloud' + # Monkeypatch class Object def blank? @@ -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 @@ -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) diff --git a/views/index.haml b/views/index.haml index 95121c4..6261bd6 100644 --- a/views/index.haml +++ b/views/index.haml @@ -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 } diff --git a/views/style.sass b/views/style.sass index dcf4953..2a417a1 100644 --- a/views/style.sass +++ b/views/style.sass @@ -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