tag cloud

git-svn-id: http://svn.barleysodas.com/barleysodas/trunk@46 0f7b21a7-9e3a-4941-bbeb-ce5c7c368fa7
master
andrew 2007-11-28 07:16:41 +00:00
parent 83b2373bdf
commit 8717d364ca
10 changed files with 81 additions and 0 deletions

View File

@ -9,6 +9,10 @@ class BeersController < ApplicationController
@secondary_title = 'Browsing all beers' @secondary_title = 'Browsing all beers'
@pages, @beers = paginate :beers, :include => 'page', :per_page => 50, @pages, @beers = paginate :beers, :include => 'page', :per_page => 50,
:order => 'beers.title ASC' :order => 'beers.title ASC'
@tags = Page.tags(:limit => 25, :order => "name DESC",
:owner_type => 'Beer')
respond_to do |format| respond_to do |format|
format.html # index.rhtml format.html # index.rhtml
format.xml { render :xml => @beers.to_xml } format.xml { render :xml => @beers.to_xml }

View File

@ -9,6 +9,10 @@ class BreweriesController < ApplicationController
@secondary_title = 'Browsing all breweries' @secondary_title = 'Browsing all breweries'
@pages, @breweries = paginate :breweries, :include => 'page', @pages, @breweries = paginate :breweries, :include => 'page',
:order => 'breweries.title ASC', :per_page => 50 :order => 'breweries.title ASC', :per_page => 50
@tags = Page.tags(:limit => 25, :order => "name DESC",
:owner_type => 'Beer')
respond_to do |format| respond_to do |format|
format.html # index.rhtml format.html # index.rhtml
format.xml { render :xml => @breweries.to_xml } format.xml { render :xml => @breweries.to_xml }

View File

@ -15,6 +15,8 @@ class PagesController < ApplicationController
@pages, @wiki_pages = paginate :page, :per_page => 25, @pages, @wiki_pages = paginate :page, :per_page => 25,
:order => 'title ASC', :conditions => [ cond_ary.join(' AND ') ] :order => 'title ASC', :conditions => [ cond_ary.join(' AND ') ]
@tags = Page.tags(:limit => 25, :order => "name DESC")
respond_to do |format| respond_to do |format|
format.html # index.rhtml format.html # index.rhtml
end end

View File

@ -46,4 +46,26 @@ module ApplicationHelper
def discussions_allowed? def discussions_allowed?
@page and @page.allow_discussions? @page and @page.allow_discussions?
end end
##
# Generates a Tag cloud.
#
def tag_cloud(tags, classes = tag_cloud_styles)
max, min = 0, 0
tags.each do |t|
max = t.count.to_i if t.count.to_i > max
min = t.count.to_i if t.count.to_i < min
end
divisor = ((max - min) / classes.size) + 1
tags.each do |t|
yield t.name, classes[(t.count.to_i - min) / divisor]
end
end
##
# Returns an array of Tag styles for a cloud.
#
def tag_cloud_styles
%w(tag_cloud_1 tag_cloud_2 tag_cloud_3 tag_cloud_4 tag_cloud_5)
end
end end

View File

@ -40,6 +40,27 @@ class Page < ActiveRecord::Base
title.to_s.gsub(/_/, ' ') title.to_s.gsub(/_/, ' ')
end end
##
# Returns a list of Tag models for a type of Page.
#
# Can take options of :order, :limit, and :owner_type
#
def self.tags(options = {})
query = "select tags.id, name, count(*) as count"
query << " from tags_pages, tags, pages"
query << " where tags.id = tag_id"
if options[:owner_type].nil?
query << " and pages.owner_type IS NULL"
else
query << " and pages.owner_type = '#{options[:owner_type]}'"
end
query << " and tags_pages.page_id = pages.id"
query << " group by tags.id, tags.name"
query << " order by #{options[:order]}" if options[:order] != nil
query << " limit #{options[:limit]}" if options[:limit] != nil
tags = Tag.find_by_sql(query)
end
protected protected
## ##

View File

@ -20,4 +20,5 @@
<% content_for :sidebar do -%> <% content_for :sidebar do -%>
<%= link_to "New Beer", new_beer_path, { :title => 'Create a new beer' } -%><br /> <%= link_to "New Beer", new_beer_path, { :title => 'Create a new beer' } -%><br />
<%= render :partial => 'shared/tag_cloud' %>
<% end -%> <% end -%>

View File

@ -21,4 +21,5 @@
<% content_for :sidebar do -%> <% content_for :sidebar do -%>
<%= link_to 'New brewery', new_brewery_path -%><br /> <%= link_to 'New brewery', new_brewery_path -%><br />
<%= render :partial => 'shared/tag_cloud' %>
<% end -%> <% end -%>

View File

@ -11,4 +11,5 @@
<% content_for :sidebar do -%> <% content_for :sidebar do -%>
<%= new_page_link -%><br /> <%= new_page_link -%><br />
<%= render :partial => 'shared/tag_cloud' %>
<% end -%> <% end -%>

View File

@ -0,0 +1,9 @@
<% unless @tags.empty? -%>
<div id="tag_cloud">
<hr />
<p>Tag Cloud</p>
<% tag_cloud @tags, tag_cloud_styles do |name, css_class| %>
<span class="<%= css_class -%>"><%= name -%></span>
<% end %>
</div>
<% end -%>

View File

@ -398,3 +398,19 @@
margin: 0; padding: 0 0 0 1em; margin: 0; padding: 0 0 0 1em;
display: inline; display: inline;
} }
/* Tag Cloud Styles */
.tag_cloud_1 {font-size: 1.0em;}
.tag_cloud_2 {font-size: 1.2em;}
.tag_cloud_3 {font-size: 1.4em;}
.tag_cloud_4 {font-size: 1.6em;}
.tag_cloud_5 {font-size: 1.8em;}
.tag_cloud_6 {font-size: 2.0em;}
#tag_cloud p {
text-align: center;
margin: 0px;
padding: 0px;
font-size: 1.1em;
font-weight: bold;
}