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