# How to implement tag clouds using plugin?

**URL:** <https://www.ruby-forum.com/t/how-to-implement-tag-clouds-using-plugin/56791>\
**Category:** Rails\
**Created:** [April 4, 2006, 3:16am UTC](https://www.ruby-forum.com/t/how-to-implement-tag-clouds-using-plugin/56791 "2006-04-04T03:16:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![charlie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/d6d6ee/32.png) [@charlie](https://www.ruby-forum.com/u/charlie)\
**Post date:** [April 4, 2006, 3:16am UTC](https://www.ruby-forum.com/t/how-to-implement-tag-clouds-using-plugin/56791/1 "2006-04-04T03:16:32Z")

</div>

The code on  
[http://blog.craz8.com/articles/2005/10/28/acts\_as\_taggable-is-a-cool-piece-of-code](http://blog.craz8.com/articles/2005/10/28/acts_as_taggable-is-a-cool-piece-of-code)  
is based on the acts\_as\_taggable gem,anybody has done that using the  
acts\_as\_taggable plugin?thanks!  
btw:the code above uses the tag\_count method,which is defined in the  
gem:  
def tags\_count(options = {})  
options = {:order =\> ‘count DESC’}.merge(options)

```
      o, t, o_pk, o_fk, t_pk, t_fk = table_name, 

```

tag\_model.table\_name, primary\_key, taggable\_foreign\_key,  
tag\_model.primary\_key, tag\_foreign\_key  
jt = tags\_join\_model ? tags\_join\_model.table\_name :  
tags\_join\_table

```
      sql = "SELECT #{t}.#{t_pk} AS id, #{t}.name AS name, COUNT(*) 

```

AS count FROM #{jt}, #{o}, #{t} WHERE #{jt}.#{t\_fk} = #{t}.#{t\_pk}  
AND #{jt}.#{o\_fk} = #{o}.#{o\_pk}"  
sql \<\< " AND #{sanitize\_sql(options[:conditions])}" if  
options[:conditions]  
sql \<\< " GROUP BY #{t}.name"  
sql \<\< " HAVING count #{options[:count]} " if options[:count]  
sql \<\< " ORDER BY #{options[:order]} " if options[:order]  
add\_limit!(sql, options)  
result = connection.select\_all(sql)  
count = result.inject({}) { |hsh, row| hsh[row[‘name’]] =  
row[‘count’].to\_i; hsh } unless options[:raw]

```
      count || result
    end

```

and the note in  
[http://wiki.rubyonrails.org/rails/pages/ActsAsTaggablePluginHowto](http://wiki.rubyonrails.org/rails/pages/ActsAsTaggablePluginHowto) gives  
an corresponding implementation for acts\_as\_taggable plugin :  
def tags\_count(options)  
sql = "SELECT tags.id AS id, tags.name AS name, COUNT(\*) AS count  
FROM tags, taggings, #{table\_name} "  
sql \<\< "WHERE taggings.taggable\_id = #{table\_name}.#{primary\_key} AND  
taggings.tag\_id = tags.id "  
sql \<\< "AND #{sanitize\_sql(options[:conditions])} " if  
options[:conditions]  
sql \<\< "GROUP BY tags.name "  
sql \<\< "HAVING count #{options[:count]} " if options[:count]  
sql \<\< "ORDER BY #{options[:order]} " if options[:order]  
sql \<\< "LIMIT #{options[:limit]} " if options[:limit]  
find\_by\_sql(sql)  
end

The latter tags\_count method will not return a hash just as the former  
do.  
If i copy the corresponding code from the former method to the  
latter,the tag clouds still can not work.  
Anybody can help with the plugin?

---

<div class="post-metadata">

**Author:** ![charlie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/d6d6ee/32.png) [@charlie](https://www.ruby-forum.com/u/charlie)\
**Post date:** [April 4, 2006, 3:36am UTC](https://www.ruby-forum.com/t/how-to-implement-tag-clouds-using-plugin/56791/2 "2006-04-04T03:36:11Z")

</div>

Here is the code that I use to create a tag\_cloud.

#helper  
def tag\_cloud(tagged\_items)

seperation = 2  
min = tagged\_items.values.min.to\_f  
max = tagged\_items.values.max.to\_f - min  
mult = (seperation / max)

tagged\_items.each do |tag, count, fsize|  
yield tag, count, ((count - min) \* mult) + 1  
end

end

#view  
\<% tag\_cloud(@tagged\_items) do |tag, count, fsize| %\>  
\<%= link\_to(h(tag), {:action=\> ‘search’, :phrase =\> tag},  
{:style =\> “font-size: #{fsize}em”}) %\>  
\<% end %\>

#controller  
def cloud  
@tagged\_items = Place.tags\_count(:limit =\> 100)  
end

If you have any questions just email. I lifted this code from a blog  
sometime back.

John

---

<div class="post-metadata">

**Author:** ![charlie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/d6d6ee/32.png) [@charlie](https://www.ruby-forum.com/u/charlie)\
**Post date:** [April 4, 2006, 3:47am UTC](https://www.ruby-forum.com/t/how-to-implement-tag-clouds-using-plugin/56791/3 "2006-04-04T03:47:09Z")

</div>

very grateful,but I have got the following error:

John H. wrote:

> Here is the code that I use to create a tag\_cloud.
> 
> #helper  
> def tag\_cloud(tagged\_items)
> 
> seperation = 2  
> min = tagged\_items.values.min.to\_f  
> undefined method `values’

> max = tagged\_items.values.max.to\_f - min  
> mult = (seperation / max)
> 
> tagged\_items.each do |tag, count, fsize|  
> yield tag, count, ((count - min) \* mult) + 1  
> end
> 
> end
> 
> If you have any questions just email. I lifted this code from a blog  
> sometime back.
> 
> John

---

<div class="post-metadata">

**Author:** ![charlie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/d6d6ee/32.png) [@charlie](https://www.ruby-forum.com/u/charlie)\
**Post date:** [April 4, 2006, 4:49am UTC](https://www.ruby-forum.com/t/how-to-implement-tag-clouds-using-plugin/56791/4 "2006-04-04T04:49:58Z")

</div>

Glad you got it working. I am not using that plugin, so I guess it  
would  
need a little changing.

John

---

<div class="post-metadata">

**Author:** ![charlie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/d6d6ee/32.png) [@charlie](https://www.ruby-forum.com/u/charlie)\
**Post date:** [April 27, 2006, 5:59pm UTC](https://www.ruby-forum.com/t/how-to-implement-tag-clouds-using-plugin/56791/5 "2006-04-27T17:59:47Z")

</div>

thanks for all the answers. I got it to work from the info here.  
Thanks again  
john i

---

<div class="post-metadata">

**Author:** ![charlie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/d6d6ee/32.png) [@charlie](https://www.ruby-forum.com/u/charlie)\
**Post date:** [April 4, 2006, 4:16am UTC](https://www.ruby-forum.com/t/how-to-implement-tag-clouds-using-plugin/56791/6 "2006-04-04T04:16:48Z")

</div>

I got it worked by modifying  
[http://blog.craz8.com/articles/2005/10/28/acts\_as\_taggable-is-a-cool-piece-of-code](http://blog.craz8.com/articles/2005/10/28/acts_as_taggable-is-a-cool-piece-of-code)

first: the tags\_count method in acts\_as\_taggable should be:  
def tags\_count(options)  
sql = "SELECT tags.id AS id, tags.name AS name, COUNT(\*) AS count  
FROM tags, taggings, #{table\_name} "  
sql \<\< "WHERE taggings.taggable\_id = #{table\_name}.#{primary\_key} AND  
taggings.tag\_id = tags.id "  
sql \<\< "AND #{sanitize\_sql(options[:conditions])} " if  
options[:conditions]  
sql \<\< "GROUP BY tags.name "  
sql \<\< "HAVING count #{options[:count]} " if options[:count]  
sql \<\< "ORDER BY #{options[:order]} " if options[:order]  
sql \<\< "LIMIT #{options[:limit]} " if options[:limit]  
result = find\_by\_sql(sql)  
count = result.inject({}) { |hsh, row| hsh[row[‘name’]] =  
row[‘count’].to\_i; hsh } unless options[:raw]  
count || result  
end

second:the helper:  
def tag\_cloud(tag\_cloud, category\_list)  
max, min = 0, 0  
tag\_cloud.each do |tag, count|  
max = count if count \> max  
min = count if count \< min  
end

```
divisor = ((max - min) / category_list.size) + 1

tag_cloud.each do |tag, count|
  yield tag, category_list[(count - min) / divisor]
end

```

end  
I am not so familiar with the Ruby language,but I think the original:  
tag\_cloud.each\_value do |count|  
max = count if count \> max  
min = count if count \< min  
end  
will not work unless each\_value method will be given.

Then it works.
