]> git.openstreetmap.org Git - rails.git/blob - app/helpers/browse_helper.rb
Merge remote-tracking branch 'upstream/pull/2642'
[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.new(name_locales(object))
15
16       Rails.logger.info "available_locales = #{available_locales.map(&:to_s)}"
17
18       preferred_locales = if current_user
19                             current_user.preferred_languages
20                           else
21                             Locale.new(I18n.locale).candidates
22                           end
23
24       Rails.logger.info "preferred_locales = #{preferred_locales.expand.map(&:to_s)}"
25
26       locale = available_locales.preferred(preferred_locales)
27
28       Rails.logger.info "locale = #{locale}"
29
30       if object.tags.include? "name:#{locale}"
31         name = t "printable_name.with_name_html", :name => content_tag(:bdi, object.tags["name:#{locale}"].to_s), :id => content_tag(:bdi, name)
32       elsif object.tags.include? "name"
33         name = t "printable_name.with_name_html", :name => content_tag(:bdi, object.tags["name"].to_s), :id => content_tag(:bdi, name)
34       elsif object.tags.include? "ref"
35         name = t "printable_name.with_name_html", :name => content_tag(:bdi, object.tags["ref"].to_s), :id => content_tag(:bdi, name)
36       end
37     end
38
39     name
40   end
41
42   def link_class(type, object)
43     classes = [type]
44
45     if object.redacted?
46       classes << "deleted"
47     else
48       classes += icon_tags(object).flatten.map { |t| h(t) }
49       classes << "deleted" unless object.visible?
50     end
51
52     classes.join(" ")
53   end
54
55   def link_title(object)
56     if object.redacted?
57       ""
58     else
59       h(icon_tags(object).map { |k, v| k + "=" + v }.to_sentence)
60     end
61   end
62
63   def link_follow(object)
64     "nofollow" if object.tags.empty?
65   end
66
67   def type_and_paginated_count(type, pages)
68     if pages.page_count == 1
69       t "browse.changeset.#{type}",
70         :count => pages.item_count
71     else
72       t "browse.changeset.#{type}_paginated",
73         :x => pages.current_page.first_item,
74         :y => pages.current_page.last_item,
75         :count => pages.item_count
76     end
77   end
78
79   private
80
81   ICON_TAGS = %w[aeroway amenity barrier building highway historic landuse leisure man_made natural railway shop tourism waterway].freeze
82
83   def icon_tags(object)
84     object.tags.find_all { |k, _v| ICON_TAGS.include? k }.sort
85   end
86
87   def name_locales(object)
88     object.tags.keys.map { |k| Regexp.last_match(1) if k =~ /^name:(.*)$/ }.flatten
89   end
90 end