RubyOnRails
I finally figured out how to create a tag cloud. Yay for me! It was rather difficult, not because the actual implementation was difficult, rather, because I don’t really understand the Ruby programming language yet. I am learning more every day though. I was able to take an example of a tag cloud from craz8’s weblog and work my own solution. I think my solution is a little more elegant because I was able to remove a recursion, hopefully making it faster, but definitely making it look prettier (the code that is).
Here is some code from the helper that builds hash for the tag cloud:
def tag_cloud(tagged_items)
separation = 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
endend
Here is the call from the view that displays the tag cloud:
<% @page_title = "tag cloud"%>
<% @page_sub_header = "tag cloud" %>
<% tag_cloud(@tagged_items) do |tag, count, fsize| %>
<%= link_to(h(tag), {:action=> ’show’, :id => tag.id}, {:style => “font-size: #{fsize}em”}) %>
<% end %>
Easy wasn’t it?