Merge remote-tracking branch 'upstream/pull/2125'
[rails.git] / app / helpers / browse_helper.rb
1 require "cgi"
2
3 module BrowseHelper
4   def printable_name(object, version = false)
5     id = if object.id.is_a?(Array)
6            object.id[0]
7          else
8            object.id
9          end
10     name = t "printable_name.with_id", :id => id.to_s
11     name = t "printable_name.with_version", :id => name, :version => object.version.to_s if version
12
13     # don't look at object tags if redacted, so as to avoid giving
14     # away redacted version tag information.
15     unless object.redacted?
16       locale = I18n.locale.to_s
17
18       locale = locale.sub(/-[^-]+/, "") while locale =~ /-[^-]+/ && !object.tags.include?("name:#{I18n.locale}")
19
20       if object.tags.include? "name:#{locale}"
21         name = t "printable_name.with_name_html", :name => content_tag(:bdi, object.tags["name:#{locale}"].to_s), :id => content_tag(:bdi, name)
22       elsif object.tags.include? "name"
23         name = t "printable_name.with_name_html", :name => content_tag(:bdi, object.tags["name"].to_s), :id => content_tag(:bdi, name)
24       elsif object.tags.include? "ref"
25         name = t "printable_name.with_name_html", :name => content_tag(:bdi, object.tags["ref"].to_s), :id => content_tag(:bdi, name)
26       end
27     end
28
29     name
30   end
31
32   def link_class(type, object)
33     classes = [type]
34
35     if object.redacted?
36       classes << "deleted"
37     else
38       classes += icon_tags(object).flatten.map { |t| h(t) }
39       classes << "deleted" unless object.visible?
40     end
41
42     classes.join(" ")
43   end
44
45   def link_title(object)
46     if object.redacted?
47       ""
48     else
49       h(icon_tags(object).map { |k, v| k + "=" + v }.to_sentence)
50     end
51   end
52
53   def link_follow(object)
54     "nofollow" if object.tags.empty?
55   end
56
57   def type_and_paginated_count(type, pages)
58     if pages.page_count == 1
59       t "browse.changeset.#{type}",
60         :count => pages.item_count
61     else
62       t "browse.changeset.#{type}_paginated",
63         :x => pages.current_page.first_item,
64         :y => pages.current_page.last_item,
65         :count => pages.item_count
66     end
67   end
68
69   private
70
71   ICON_TAGS = %w[aeroway amenity barrier building highway historic landuse leisure man_made natural railway shop tourism waterway].freeze
72
73   def icon_tags(object)
74     object.tags.find_all { |k, _v| ICON_TAGS.include? k }.sort
75   end
76 end