Extract methods in tag cloud

This gets rid of the compute method, and makes each value that got set
in it its own little method.
This commit is contained in:
Katrina Owen 2013-02-28 21:53:19 -05:00
parent 447178bd7d
commit 61e04a8258
2 changed files with 30 additions and 15 deletions

View file

@ -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

View file

@ -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