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