From 8f6d57014a54cb1f92ac3badc2398dc755b07c38 Mon Sep 17 00:00:00 2001 From: Katrina Owen Date: Thu, 28 Feb 2013 21:08:48 -0500 Subject: [PATCH] Extract tag cloud class from stats controller --- app/controllers/stats_controller.rb | 56 ++++--------------------- app/models/stats/tag_cloud.rb | 65 +++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 48 deletions(-) create mode 100644 app/models/stats/tag_cloud.rb diff --git a/app/controllers/stats_controller.rb b/app/controllers/stats_controller.rb index 4c05d78e..a0cc9b29 100644 --- a/app/controllers/stats_controller.rb +++ b/app/controllers/stats_controller.rb @@ -552,55 +552,15 @@ class StatsController < ApplicationController end def get_stats_tags - # tag cloud code inspired by this article - # http://www.juixe.com/techknow/index.php/2006/07/15/acts-as-taggable-tag-cloud/ + cloud = Stats::TagCloud.new(current_user, @cut_off_3months) + cloud.compute - levels=10 - # TODO: parameterize limit - - # Get the tag cloud for all tags for actions - query = "SELECT tags.id, name, count(*) AS count" - query << " FROM taggings, tags, todos" - query << " WHERE tags.id = tag_id" - query << " AND taggings.taggable_id = todos.id" - query << " AND todos.user_id="+current_user.id.to_s+" " - query << " AND taggings.taggable_type='Todo' " - query << " GROUP BY tags.id, tags.name" - query << " ORDER BY count DESC, name" - query << " LIMIT 100" - @tags_for_cloud = Tag.find_by_sql(query).sort_by { |tag| tag.name.downcase } - - max, @tags_min = 0, 0 - @tags_for_cloud.each { |t| - max = [t.count.to_i, max].max - @tags_min = [t.count.to_i, @tags_min].min - } - - @tags_divisor = ((max - @tags_min) / levels) + 1 - - # Get the tag cloud for all tags for actions - query = "SELECT tags.id, tags.name AS name, count(*) AS count" - query << " FROM taggings, tags, todos" - query << " WHERE tags.id = tag_id" - query << " AND todos.user_id=? " - query << " AND taggings.taggable_type='Todo' " - query << " AND taggings.taggable_id=todos.id " - query << " AND (todos.created_at > ? OR " - query << " todos.completed_at > ?) " - query << " GROUP BY tags.id, tags.name" - query << " ORDER BY count DESC, name" - query << " LIMIT 100" - @tags_for_cloud_90days = Tag.find_by_sql( - [query, current_user.id, @cut_off_3months, @cut_off_3months] - ).sort_by { |tag| tag.name.downcase } - - max_90days, @tags_min_90days = 0, 0 - @tags_for_cloud_90days.each { |t| - max_90days = [t.count.to_i, max_90days].max - @tags_min_90days = [t.count.to_i, @tags_min_90days].min - } - - @tags_divisor_90days = ((max_90days - @tags_min_90days) / levels) + 1 + @tags_for_cloud = cloud.tags_for_cloud + @tags_min = cloud.tags_min + @tags_divisor = cloud.tags_divisor + @tags_for_cloud_90days = cloud.tags_for_cloud_90days + @tags_min_90days = cloud.tags_min_90days + @tags_divisor_90days = cloud.tags_divisor_90days end def get_ids_from (actions, week_from, week_to, at_end) diff --git a/app/models/stats/tag_cloud.rb b/app/models/stats/tag_cloud.rb new file mode 100644 index 00000000..78a49067 --- /dev/null +++ b/app/models/stats/tag_cloud.rb @@ -0,0 +1,65 @@ +# tag cloud code inspired by this article +# http://www.juixe.com/techknow/index.php/2006/07/15/acts-as-taggable-tag-cloud/ +module Stats + class TagCloud + + attr_reader :current_user, + :tags_for_cloud, :tags_min, :tags_divisor, + :tags_for_cloud_90days, :tags_min_90days, :tags_divisor_90days + def initialize(current_user, cut_off_3months) + @current_user = current_user + @cut_off_3months = cut_off_3months + end + + def compute + levels=10 + # TODO: parameterize limit + + # Get the tag cloud for all tags for actions + query = "SELECT tags.id, name, count(*) AS count" + query << " FROM taggings, tags, todos" + query << " WHERE tags.id = tag_id" + query << " AND taggings.taggable_id = todos.id" + query << " AND todos.user_id="+current_user.id.to_s+" " + query << " AND taggings.taggable_type='Todo' " + query << " GROUP BY tags.id, tags.name" + query << " ORDER BY count DESC, name" + query << " LIMIT 100" + @tags_for_cloud = Tag.find_by_sql(query).sort_by { |tag| tag.name.downcase } + + max, @tags_min = 0, 0 + @tags_for_cloud.each { |t| + max = [t.count.to_i, max].max + @tags_min = [t.count.to_i, @tags_min].min + } + + @tags_divisor = ((max - @tags_min) / levels) + 1 + + # Get the tag cloud for all tags for actions + query = "SELECT tags.id, tags.name AS name, count(*) AS count" + query << " FROM taggings, tags, todos" + query << " WHERE tags.id = tag_id" + query << " AND todos.user_id=? " + query << " AND taggings.taggable_type='Todo' " + query << " AND taggings.taggable_id=todos.id " + query << " AND (todos.created_at > ? OR " + query << " todos.completed_at > ?) " + query << " GROUP BY tags.id, tags.name" + query << " ORDER BY count DESC, name" + query << " LIMIT 100" + @tags_for_cloud_90days = Tag.find_by_sql( + [query, current_user.id, @cut_off_3months, @cut_off_3months] + ).sort_by { |tag| tag.name.downcase } + + max_90days, @tags_min_90days = 0, 0 + @tags_for_cloud_90days.each { |t| + max_90days = [t.count.to_i, max_90days].max + @tags_min_90days = [t.count.to_i, @tags_min_90days].min + } + + @tags_divisor_90days = ((max_90days - @tags_min_90days) / levels) + 1 + + end + + end +end