Module: MakeTaggable::TagsHelper

Defined in:
lib/make_taggable/tags_helper.rb

Overview

View helpers for rendering tags. Mixed into Action View automatically.

Instance Method Summary collapse

Instance Method Details

#tag_cloud(tags, classes) {|tag, css_class| ... } ⇒ Array

Yields each tag with the CSS class matching how often it is used, for building a tag cloud.

Classes are handed out in the order given, from least to most used, so the first class is the smallest and the last is the largest.

Examples:

<% tag_cloud(@tags, %w[css1 css2 css3 css4]) do |tag, css_class| %>
  <%= link_to tag.name, tag_path(tag.name), class: css_class %>
<% end %>

Parameters:

  • tags (Array<MakeTaggable::Tag>)

    tags carrying a taggings_count

  • classes (Array<String>)

    the CSS classes, smallest first

Yield Parameters:

  • tag (MakeTaggable::Tag)

    the tag

  • css_class (String)

    the class for that tag's frequency

Returns:

  • (Array)

    the tags, or an empty array when none were given



25
26
27
28
29
30
31
32
33
34
# File 'lib/make_taggable/tags_helper.rb', line 25

def tag_cloud(tags, classes)
  return [] if tags.empty?

  max_count = tags.max_by(&:taggings_count).taggings_count.to_f

  tags.each do |tag|
    index = ((tag.taggings_count / max_count) * (classes.size - 1))
    yield tag, classes[index.nan? ? 0 : index.round]
  end
end