Commit a334f6d7c29ed44bc13c3e8e6a6d1f761b72f77f

Authored by Larissa Reis
1 parent 44297261

Fixes sort in tag cloud so it considers special chars

(ActionItem2251)
app/helpers/tags_helper.rb
@@ -20,12 +20,12 @@ module TagsHelper @@ -20,12 +20,12 @@ module TagsHelper
20 # 20 #
21 # <tt>options</tt> can include one or more of the following: 21 # <tt>options</tt> can include one or more of the following:
22 # 22 #
23 - # * <tt>:max_size</tt>: font size for the tag with largest count  
24 - # * <tt>:min_size</tt>: font size for the tag with smallest count 23 + # * <tt>:max_size</tt>: font size for the tag with largest count
  24 + # * <tt>:min_size</tt>: font size for the tag with smallest count
25 # * <tt>:show_count</tt>: whether to show the count of contents for each tag. Defauls to <tt>false</tt>. 25 # * <tt>:show_count</tt>: whether to show the count of contents for each tag. Defauls to <tt>false</tt>.
26 - # 26 + #
27 # The algorithm for generating the different sizes and positions is a 27 # The algorithm for generating the different sizes and positions is a
28 - # courtesy of Aurelio: http://www.colivre.coop.br/Aurium/Nuvem 28 + # courtesy of Aurelio: http://www.colivre.coop.br/Aurium/Nuvem
29 # (pt_BR only). 29 # (pt_BR only).
30 def tag_cloud(tags, tagname_option, url, options = {}) 30 def tag_cloud(tags, tagname_option, url, options = {})
31 31
@@ -41,7 +41,15 @@ module TagsHelper @@ -41,7 +41,15 @@ module TagsHelper
41 max = tags.values.max.to_f 41 max = tags.values.max.to_f
42 min = tags.values.min.to_f 42 min = tags.values.min.to_f
43 43
44 - tags.sort_by{ |k,v| k.downcase }.map do |tag,count| 44 + # Uses iconv to translate utf8 strings to ascii.
  45 + # If can't translate a character, ignore it.
  46 + require 'iconv'
  47 + utf8_to_ascii = Iconv.new("ASCII//TRANSLIT//IGNORE","UTF8")
  48 + # Sorts first based on translated strings and then, if they are equal, based on the original form.
  49 + # This way variant characters falls on the same level as their base characters and don't end up
  50 + # at the end of the tag list.
  51 + # Example: AA ÁA AB Z instead of AA AB Z ÁA
  52 + tags.collect{ |k,v| [utf8_to_ascii.iconv(k).downcase, [k,v]] }.sort.collect { |ascii, t| t }.map do |tag,count|
45 if ( max == min ) 53 if ( max == min )
46 v = 0.5 54 v = 0.5
47 else 55 else
test/unit/tags_helper_test.rb
@@ -18,4 +18,14 @@ class TagsHelperTest &lt; Test::Unit::TestCase @@ -18,4 +18,14 @@ class TagsHelperTest &lt; Test::Unit::TestCase
18 assert_equal %w(aTag beTag tag1 Tag2 Tag3).join("\n"), result 18 assert_equal %w(aTag beTag tag1 Tag2 Tag3).join("\n"), result
19 end 19 end
20 20
  21 + should 'order tags alphabetically with special characters' do
  22 + result = tag_cloud(
  23 + { 'aula'=>9, 'área'=>2, 'area'=>2, 'avião'=>2, 'armário'=>2,
  24 + 'A'=>1, 'Á'=>1, 'AB'=>1, 'ÁA'=>1 },
  25 + :id,
  26 + { :host=>'noosfero.org', :controller=>'test', :action=>'tag' }
  27 + )
  28 + assert_equal %w(A Á ÁA AB area área armário aula avião).join("\n"), result
  29 + end
  30 +
21 end 31 end