]> git.openstreetmap.org Git - rails.git/blob - app/helpers/application_helper.rb
Merge remote-tracking branch 'upstream/pull/7491'
[rails.git] / app / helpers / application_helper.rb
1 # frozen_string_literal: true
2
3 module ApplicationHelper
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 tag.i(:class => "bi bi-rss-fill fs-6", :aria => { :hidden => "true" }), args
15   end
16
17   def dir
18     if dir = params[:dir]
19       dir == "rtl" ? "rtl" : "ltr"
20     else
21       I18n.t("html.dir")
22     end
23   end
24
25   def friendly_date(date)
26     tag.time(time_ago_in_words(date), :title => l(date, :format => :friendly), :datetime => date.xmlschema)
27   end
28
29   def friendly_date_ago(date)
30     tag.time(time_ago_in_words(date, :scope => :"datetime.distance_in_words_ago"), :title => l(date, :format => :friendly), :datetime => date.xmlschema)
31   end
32
33   def body_class
34     if content_for? :body_class
35       content_for :body_class
36     else
37       controller_part = params[:controller].tr("/", "-")
38       "#{controller_part} #{controller_part}-#{params[:action]}"
39     end
40   end
41
42   def secondary_nav_items
43     items = [
44       [history_path, t("layouts.header.history"), { :class => ["geolink"] }],
45       [export_path, t("layouts.header.export"), { :class => ["geolink"] }],
46       [traces_path, t("layouts.header.gps_traces")],
47       [diary_entries_path, t("layouts.header.user_diaries")],
48       [communities_path, t("layouts.header.communities")],
49       [copyright_path, t("layouts.header.copyright")],
50       [help_path, t("layouts.header.help")],
51       [Settings.donation_url, t("layouts.header.donate"), { :target => :new }],
52       [about_path, t("layouts.header.about")]
53     ]
54
55     items.delete_at(2) if Settings.traces_disabled
56
57     if Settings.status != "database_offline" && can?(:index, Issue)
58       items.prepend([
59                       issues_path(:status => "open"),
60                       safe_join([t("layouts.header.issues"), open_issues_count], " ")
61                     ])
62     end
63
64     items
65   end
66
67   def application_data
68     data = {
69       :locale => I18n.locale,
70       :preferred_editor => preferred_editor,
71       :preferred_languages => preferred_languages.expand.map(&:to_s)
72     }
73
74     if current_user
75       data[:user] = current_user.id.to_json
76
77       data[:user_home] = { :lat => current_user.home_lat, :lon => current_user.home_lon } if current_user.home_location?
78     end
79
80     data[:location] = session[:location] if session[:location]
81     data[:oauth_token] = oauth_token.token if oauth_token
82
83     data
84   end
85
86   # If the flash is a hash, then it will be a partial with a hash of locals, so we can call `render` on that
87   # This allows us to render html into a flash message in a safe manner.
88   def render_flash(flash)
89     if flash.is_a?(Hash)
90       render flash.deep_symbolize_keys
91     else
92       flash
93     end
94   rescue StandardError
95     flash.inspect if Rails.env.development?
96   end
97 end