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