Commit 136190f5a7e432580deab43748d46aa7f33b68e3

Authored by Daniela Feitosa
2 parents 1a384d81 a334f6d7

Merge commit 'refs/merge-requests/128' of git://gitorious.org/noosfero/noosfero …

…into merge-requests/128

(ActionItem2251)
app/helpers/tags_helper.rb
... ... @@ -20,12 +20,12 @@ module TagsHelper
20 20 #
21 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 25 # * <tt>:show_count</tt>: whether to show the count of contents for each tag. Defauls to <tt>false</tt>.
26   - #
  26 + #
27 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 29 # (pt_BR only).
30 30 def tag_cloud(tags, tagname_option, url, options = {})
31 31  
... ... @@ -41,7 +41,15 @@ module TagsHelper
41 41 max = tags.values.max.to_f
42 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 53 if ( max == min )
46 54 v = 0.5
47 55 else
... ...
test/unit/tags_helper_test.rb
... ... @@ -18,4 +18,14 @@ class TagsHelperTest &lt; ActiveSupport::TestCase
18 18 assert_equal %w(aTag beTag tag1 Tag2 Tag3).join("\n"), result
19 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 31 end
... ...