]> git.openstreetmap.org Git - rails.git/blob - app/helpers/application_helper.rb
Fix new rubocop warnings
[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), Hash[*args], :class => "rsssmall")
14   end
15
16   def atom_link_to(*args)
17     link_to(image_tag("RSS.png", :size => "16x16", :border => 0), Hash[*args], :class => "rsssmall")
18   end
19
20   def richtext_area(object_name, method, options = {})
21     id = "#{object_name}_#{method}"
22     type = options.delete(:format) || "markdown"
23
24     tag.div(:id => "#{id}_container", :class => "richtext_container") do
25       output_buffer << tag.div(:id => "#{id}_content", :class => "richtext_content") do
26         output_buffer << text_area(object_name, method, options.merge("data-preview-url" => preview_url(:type => type)))
27         output_buffer << tag.div("", :id => "#{id}_preview", :class => "richtext_preview richtext text-break")
28       end
29
30       output_buffer << tag.div(:id => "#{id}_help", :class => "richtext_help") do
31         output_buffer << render("site/#{type}_help")
32         output_buffer << tag.div(:class => "buttons") do
33           output_buffer << submit_tag(I18n.t("site.richtext_area.edit"), :id => "#{id}_doedit", :class => "richtext_doedit deemphasize", :disabled => true)
34           output_buffer << submit_tag(I18n.t("site.richtext_area.preview"), :id => "#{id}_dopreview", :class => "richtext_dopreview deemphasize")
35         end
36       end
37     end
38   end
39
40   def dir
41     if dir = params[:dir]
42       dir == "rtl" ? "rtl" : "ltr"
43     else
44       I18n.t("html.dir")
45     end
46   end
47
48   def friendly_date(date)
49     tag.span(time_ago_in_words(date), :title => l(date, :format => :friendly))
50   end
51
52   def friendly_date_ago(date)
53     tag.span(time_ago_in_words(date, :scope => :'datetime.distance_in_words_ago'), :title => l(date, :format => :friendly))
54   end
55
56   def body_class
57     if content_for? :body_class
58       content_for :body_class
59     else
60       "#{params[:controller]} #{params[:controller]}-#{params[:action]}"
61     end
62   end
63
64   def current_page_class(path)
65     :current if current_page?(path)
66   end
67
68   def application_data
69     data = {
70       :locale => I18n.locale,
71       :preferred_editor => preferred_editor
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 } unless current_user.home_lon.nil? || current_user.home_lat.nil?
78     end
79
80     data[:location] = session[:location] if session[:location]
81
82     if oauth_token
83       data[:token] = oauth_token.token
84       data[:token_secret] = oauth_token.secret
85       data[:consumer_key] = oauth_token.client_application.key
86       data[:consumer_secret] = oauth_token.client_application.secret
87     end
88
89     data
90   end
91 end