Skip to content

Commit

Permalink
Ensures TagsHelper#tag_cloud can handle tags with no taggings.
Browse files Browse the repository at this point in the history
  • Loading branch information
james2m committed Oct 5, 2011
1 parent dcc0c62 commit 12a59ae
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/acts_as_taggable_on/tags_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ def tag_cloud(tags, classes)
max_count = tags.sort_by(&:count).last.count.to_f

tags.each do |tag|
index = ((tag.count / max_count) * (classes.size - 1)).round
yield tag, classes[index]
index = ((tag.count / max_count) * (classes.size - 1))
yield tag, classes[index.nan? ? 0 : index.round]
end
end
end
Expand Down
16 changes: 16 additions & 0 deletions spec/acts_as_taggable_on/tags_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,20 @@
tags["c++"].should == "sucky"
tags["php"].should == "sucky"
end

it "should handle tags with zero counts (build for empty)" do
bob = ActsAsTaggableOn::Tag.create(:name => "php")
tom = ActsAsTaggableOn::Tag.create(:name => "java")
eve = ActsAsTaggableOn::Tag.create(:name => "c++")

tags = { }

@helper.tag_cloud(ActsAsTaggableOn::Tag.all, ["sucky", "awesome"]) do |tag, css_class|
tags[tag.name] = css_class
end

tags["java"].should == "sucky"
tags["c++"].should == "sucky"
tags["php"].should == "sucky"
end
end

0 comments on commit 12a59ae

Please sign in to comment.