mirror of
https://github.com/TracksApp/tracks.git
synced 2026-02-06 07:51:48 +01:00
Merge branch 'tag-cloud' of https://github.com/kytrinyx/tracks
This commit is contained in:
commit
8b0f3e986a
9 changed files with 159 additions and 77 deletions
38
app/models/stats/tag_cloud.rb
Normal file
38
app/models/stats/tag_cloud.rb
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
# 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 :levels, :tags
|
||||
def initialize(tags)
|
||||
@levels = 10
|
||||
@tags = tags.sort_by { |tag| tag.name.downcase }
|
||||
end
|
||||
|
||||
def empty?
|
||||
tags.empty?
|
||||
end
|
||||
|
||||
def font_size(tag)
|
||||
(9 + 2*(tag.count-min)/divisor)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def max
|
||||
@max ||= counts.max
|
||||
end
|
||||
|
||||
def min
|
||||
@min ||= counts.min
|
||||
end
|
||||
|
||||
def divisor
|
||||
@divisor ||= ((max - min) / levels) + 1
|
||||
end
|
||||
|
||||
def counts
|
||||
@counts ||= tags.map {|t| t.count}
|
||||
end
|
||||
end
|
||||
end
|
||||
38
app/models/stats/tag_cloud_query.rb
Normal file
38
app/models/stats/tag_cloud_query.rb
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
module Stats
|
||||
class TagCloudQuery
|
||||
|
||||
attr_reader :user, :cutoff
|
||||
def initialize(user, cutoff = nil)
|
||||
@user = user
|
||||
@cutoff = cutoff
|
||||
end
|
||||
|
||||
def result
|
||||
Tag.find_by_sql(query_options)
|
||||
end
|
||||
|
||||
def query_options
|
||||
options = [sql, user.id]
|
||||
options += [cutoff, cutoff] if cutoff
|
||||
options
|
||||
end
|
||||
|
||||
def sql
|
||||
# TODO: parameterize limit
|
||||
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 "
|
||||
if cutoff
|
||||
query << " AND (todos.created_at > ? OR "
|
||||
query << " todos.completed_at > ?) "
|
||||
end
|
||||
query << " GROUP BY tags.id, tags.name"
|
||||
query << " ORDER BY count DESC, name"
|
||||
query << " LIMIT 100"
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue