From 3f496db4b42bd3a10a14a1449b4c6ecb45293f46 Mon Sep 17 00:00:00 2001 From: Katrina Owen Date: Sat, 2 Mar 2013 07:14:52 -0500 Subject: [PATCH] Split up the font size calculation The font size is a view concern, but the relative size belongs in the model layer. --- app/helpers/stats_helper.rb | 5 +++++ app/models/stats/tag_cloud.rb | 4 ++-- app/views/stats/_tags.html.erb | 2 +- test/unit/tag_cloud_test.rb | 8 ++++---- 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/app/helpers/stats_helper.rb b/app/helpers/stats_helper.rb index 65e2f8bb..9e14d04b 100755 --- a/app/helpers/stats_helper.rb +++ b/app/helpers/stats_helper.rb @@ -1,2 +1,7 @@ module StatsHelper + + def font_size(cloud, tag) + 9 + 2 * cloud.relative_size(tag) + end + end diff --git a/app/models/stats/tag_cloud.rb b/app/models/stats/tag_cloud.rb index ef03c8fc..67865f27 100644 --- a/app/models/stats/tag_cloud.rb +++ b/app/models/stats/tag_cloud.rb @@ -13,8 +13,8 @@ module Stats tags.empty? end - def font_size(tag) - (9 + 2*(tag.count-min)/divisor) + def relative_size(tag) + (tag.count - min) / divisor end private diff --git a/app/views/stats/_tags.html.erb b/app/views/stats/_tags.html.erb index 98140297..bd719a75 100755 --- a/app/views/stats/_tags.html.erb +++ b/app/views/stats/_tags.html.erb @@ -9,7 +9,7 @@ tag_cloud.tags.each do |t| %><%= link_to t.name, tag_path(t.name), { - :style => "font-size: " + "#{tag_cloud.font_size(t)}pt", + :style => "font-size: " + "#{font_size(tag_cloud, t)}pt", :title => "#{t.count} #{t('common.actions_midsentence', :count => t.count)}"} -%><% end diff --git a/test/unit/tag_cloud_test.rb b/test/unit/tag_cloud_test.rb index d40bcc51..6ba8b5d1 100644 --- a/test/unit/tag_cloud_test.rb +++ b/test/unit/tag_cloud_test.rb @@ -11,13 +11,13 @@ class TagCloudTest < Test::Unit::TestCase assert_equal %w(aye bee See), Stats::TagCloud.new(tags).tags.map(&:name) end - def test_tag_font_size + def test_tag_relative_size tags = [FakeTag.new("bee", 1), FakeTag.new("See", 10), FakeTag.new("aye", 100)] cloud = Stats::TagCloud.new(tags) - assert_equal 9, cloud.font_size(FakeTag.new("whatever", 1)) - assert_equal 18, cloud.font_size(FakeTag.new("whatever", 50)) - assert_equal 28, cloud.font_size(FakeTag.new("whatever", 100)) + assert_equal 0, cloud.relative_size(FakeTag.new("whatever", 1)) + assert_equal 4, cloud.relative_size(FakeTag.new("whatever", 50)) + assert_equal 9, cloud.relative_size(FakeTag.new("whatever", 100)) end end