How to implement tag clouds using plugin?


#1

The code on
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 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?


#2

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


#3

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


#4

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

John


#5

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


#6

I got it worked by modifying
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.