]> git.openstreetmap.org Git - rails.git/blob - app/helpers/application_helper.rb
Merge remote-tracking branch 'upstream/pull/4720'
[rails.git] / app / helpers / application_helper.rb
1 module ApplicationHelper
2   require "rexml/document"
3
4   def linkify(text)
5     if text.html_safe?
6       Rinku.auto_link(text, :urls, tag_builder.tag_options(:rel => "nofollow")).html_safe
7     else
8       Rinku.auto_link(ERB::Util.h(text), :urls, tag_builder.tag_options(:rel => "nofollow")).html_safe
9     end
10   end
11
12   def rss_link_to(args = {})
13     link_to(image_tag("RSS.png", :size => "16x16", :border => 0), args, :class => "rsssmall")
14   end
15
16   def atom_link_to(args = {})
17     link_to(image_tag("RSS.png", :size => "16x16", :border => 0), args, :class => "rsssmall")
18   end
19
20   def dir
21     if dir = params[:dir]
22       dir == "rtl" ? "rtl" : "ltr"
23     else
24       I18n.t("html.dir")
25     end
26   end
27
28   def friendly_date(date)
29     tag.time(time_ago_in_words(date), :title => l(date, :format => :friendly), :datetime => date.xmlschema)
30   end
31
32   def friendly_date_ago(date)
33     tag.time(time_ago_in_words(date, :scope => :"datetime.distance_in_words_ago"), :title => l(date, :format => :friendly), :datetime => date.xmlschema)
34   end
35
36   def body_class
37     if content_for? :body_class
38       content_for :body_class
39     else
40       "#{params[:controller]} #{params[:controller]}-#{params[:action]}"
41     end
42   end
43
44   def header_nav_link_class(path)
45     ["nav-link", current_page?(path) ? "text-secondary-emphasis" : "text-secondary"]
46   end
47
48   def application_data
49     data = {
50       :locale => I18n.locale,
51       :preferred_editor => preferred_editor,
52       :preferred_languages => preferred_languages.expand.map(&:to_s)
53     }
54
55     if current_user
56       data[:user] = current_user.id.to_json
57
58       data[:user_home] = { :lat => current_user.home_lat, :lon => current_user.home_lon } if current_user.home_location?
59     end
60
61     data[:location] = session[:location] if session[:location]
62     data[:oauth_token] = oauth_token.token if oauth_token
63
64     data
65   end
66
67   # If the flash is a hash, then it will be a partial with a hash of locals, so we can call `render` on that
68   # This allows us to render html into a flash message in a safe manner.
69   def render_flash(flash)
70     if flash.is_a?(Hash)
71       render flash.with_indifferent_access
72     else
73       flash
74     end
75   rescue StandardError
76     flash.inspect if Rails.env.development?
77   end
78 end