diff --git a/app/controllers/stats_controller.rb b/app/controllers/stats_controller.rb index b84c95e2..cdfdfb46 100644 --- a/app/controllers/stats_controller.rb +++ b/app/controllers/stats_controller.rb @@ -553,14 +553,12 @@ class StatsController < ApplicationController def get_stats_tags cloud = Stats::TagCloud.new(current_user) - cloud.compute @tags_for_cloud = cloud.tags @tags_min = cloud.min @tags_divisor = cloud.divisor cloud = Stats::TagCloud.new(current_user, @cut_off_3months) - cloud.compute @tags_for_cloud_90days = cloud.tags @tags_min_90days = cloud.min @tags_divisor_90days = cloud.divisor diff --git a/app/models/stats/tag_cloud.rb b/app/models/stats/tag_cloud.rb index 2b2e15bc..1d9a1c42 100644 --- a/app/models/stats/tag_cloud.rb +++ b/app/models/stats/tag_cloud.rb @@ -3,30 +3,47 @@ module Stats class TagCloud - attr_reader :user, :cutoff, :levels, - :tags, :min, :divisor + attr_reader :user, :cutoff, :levels def initialize(user, cutoff = nil) @user = user @cutoff = cutoff @levels = 10 end - def compute - @tags = top_tags - max, @min = 0, 0 - @tags.each { |t| - max = [t.count.to_i, max].max - @min = [t.count.to_i, @min].min - } - @divisor = ((max - @min) / levels) + 1 + def tags + @tags ||= top_tags + end + + def max + @max ||= tag_counts.max + end + + # 2013-02-28: Possible bug. + # The original code always set the minimum to zero. + # This might need to use tag_counts.min + # https://github.com/TracksApp/tracks/commit/8c26ea7cb596c97e37213c0cc994e66ee5fd27b0#commitcomment-2719199 + def min + 0 + end + + def divisor + @divisor ||= ((max - min) / levels) + 1 end private + def tag_counts + @tag_counts ||= tags.map {|t| t.count.to_i} + end + def top_tags - params = [sql, user.id] - params += [cutoff, cutoff] if cutoff - Tag.find_by_sql(params).sort_by { |tag| tag.name.downcase } + Tag.find_by_sql(query_options).sort_by { |tag| tag.name.downcase } + end + + def query_options + options = [sql, user.id] + options += [cutoff, cutoff] if cutoff + options end def sql