diff --git a/app/controllers/beers_controller.rb b/app/controllers/beers_controller.rb
index bc89bc8..9fd47a3 100644
--- a/app/controllers/beers_controller.rb
+++ b/app/controllers/beers_controller.rb
@@ -9,6 +9,10 @@ class BeersController < ApplicationController
@secondary_title = 'Browsing all beers'
@pages, @beers = paginate :beers, :include => 'page', :per_page => 50,
:order => 'beers.title ASC'
+
+ @tags = Page.tags(:limit => 25, :order => "name DESC",
+ :owner_type => 'Beer')
+
respond_to do |format|
format.html # index.rhtml
format.xml { render :xml => @beers.to_xml }
diff --git a/app/controllers/breweries_controller.rb b/app/controllers/breweries_controller.rb
index 18aff98..54e32b5 100644
--- a/app/controllers/breweries_controller.rb
+++ b/app/controllers/breweries_controller.rb
@@ -9,6 +9,10 @@ class BreweriesController < ApplicationController
@secondary_title = 'Browsing all breweries'
@pages, @breweries = paginate :breweries, :include => 'page',
:order => 'breweries.title ASC', :per_page => 50
+
+ @tags = Page.tags(:limit => 25, :order => "name DESC",
+ :owner_type => 'Beer')
+
respond_to do |format|
format.html # index.rhtml
format.xml { render :xml => @breweries.to_xml }
diff --git a/app/controllers/pages_controller.rb b/app/controllers/pages_controller.rb
index 046981a..b67919e 100644
--- a/app/controllers/pages_controller.rb
+++ b/app/controllers/pages_controller.rb
@@ -15,6 +15,8 @@ class PagesController < ApplicationController
@pages, @wiki_pages = paginate :page, :per_page => 25,
:order => 'title ASC', :conditions => [ cond_ary.join(' AND ') ]
+ @tags = Page.tags(:limit => 25, :order => "name DESC")
+
respond_to do |format|
format.html # index.rhtml
end
diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb
index 355e4e7..eb31244 100644
--- a/app/helpers/application_helper.rb
+++ b/app/helpers/application_helper.rb
@@ -46,4 +46,26 @@ module ApplicationHelper
def discussions_allowed?
@page and @page.allow_discussions?
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
diff --git a/app/models/page.rb b/app/models/page.rb
index 02c0a3a..f87e128 100644
--- a/app/models/page.rb
+++ b/app/models/page.rb
@@ -40,6 +40,27 @@ class Page < ActiveRecord::Base
title.to_s.gsub(/_/, ' ')
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
##
diff --git a/app/views/beers/index.rhtml b/app/views/beers/index.rhtml
index 930a156..a07d5fb 100644
--- a/app/views/beers/index.rhtml
+++ b/app/views/beers/index.rhtml
@@ -20,4 +20,5 @@
<% content_for :sidebar do -%>
<%= link_to "New Beer", new_beer_path, { :title => 'Create a new beer' } -%>
+ <%= render :partial => 'shared/tag_cloud' %>
<% end -%>
diff --git a/app/views/breweries/index.rhtml b/app/views/breweries/index.rhtml
index 1f5897d..edffbae 100644
--- a/app/views/breweries/index.rhtml
+++ b/app/views/breweries/index.rhtml
@@ -21,4 +21,5 @@
<% content_for :sidebar do -%>
<%= link_to 'New brewery', new_brewery_path -%>
+ <%= render :partial => 'shared/tag_cloud' %>
<% end -%>
diff --git a/app/views/pages/index.rhtml b/app/views/pages/index.rhtml
index bec6e3e..8b6d446 100644
--- a/app/views/pages/index.rhtml
+++ b/app/views/pages/index.rhtml
@@ -11,4 +11,5 @@
<% content_for :sidebar do -%>
<%= new_page_link -%>
+ <%= render :partial => 'shared/tag_cloud' %>
<% end -%>
diff --git a/app/views/shared/_tag_cloud.rhtml b/app/views/shared/_tag_cloud.rhtml
new file mode 100644
index 0000000..d1def40
--- /dev/null
+++ b/app/views/shared/_tag_cloud.rhtml
@@ -0,0 +1,9 @@
+<% unless @tags.empty? -%>
+
Tag Cloud
+<% tag_cloud @tags, tag_cloud_styles do |name, css_class| %> + <%= name -%> +<% end %> +