Forum: Ruby on Rails How to implement tag clouds using plugin?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
9d069b3237bc411726d3a83d9077508d?d=identicon&s=25 Charlie (Guest)
on 2006-04-04 03:16
The code on
http://blog.craz8.com/articles/2005/10/28/acts_as_...
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/ActsAsTagg... 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?
47093444301bbde90d0aef5fa5c3ac86?d=identicon&s=25 John Hornbeck (Guest)
on 2006-04-04 03:36
(Received via mailing list)
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
9d069b3237bc411726d3a83d9077508d?d=identicon&s=25 Charlie (Guest)
on 2006-04-04 03:47
very grateful,but I have got the following error:

John Hornbeck 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
9d069b3237bc411726d3a83d9077508d?d=identicon&s=25 Charlie (Guest)
on 2006-04-04 04:16
I got it worked by modifying
http://blog.craz8.com/articles/2005/10/28/acts_as_...

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.
47093444301bbde90d0aef5fa5c3ac86?d=identicon&s=25 John Hornbeck (Guest)
on 2006-04-04 04:49
(Received via mailing list)
Glad you got it working.  I am not using that plugin, so I guess it
would
need a little changing.

John
316c03a99e8ed311e5462d94f085fc2e?d=identicon&s=25 john i (Guest)
on 2006-04-27 17:59
thanks for all the answers. I got it to work from the info here.
Thanks again
john i
This topic is locked and can not be replied to.