demarches-normaliennes/app/helpers/application_helper.rb

113 lines
3 KiB
Ruby
Raw Normal View History

module ApplicationHelper
2018-01-11 18:09:01 +01:00
include SanitizeUrl
def sanitize_url(url)
if !url.nil?
super(url, schemes: ['http', 'https'], replace_evil_with: root_url)
end
2018-01-11 18:09:01 +01:00
end
2018-08-09 11:15:23 +02:00
def flash_class(level, sticky = false)
case level
2018-08-09 11:15:23 +02:00
when "notice" then "alert-success#{sticky ? ' sticky' : ''}"
when "alert" then "alert-danger#{sticky ? ' sticky' : ''}"
end
end
2017-06-22 16:45:57 +02:00
2018-08-09 11:15:23 +02:00
def render_to_element(selector, partial:, outer: false, locals: {})
method = outer ? 'outerHTML' : 'innerHTML'
html = escape_javascript(render partial: partial, locals: locals)
# rubocop:disable Rails/OutputSafety
raw("document.querySelector('#{selector}').#{method} = \"#{html}\";")
# rubocop:enable Rails/OutputSafety
end
def render_flash(timeout: false, sticky: false)
if flash.any?
html = render_to_element('#flash_messages', partial: 'layouts/flash_messages', locals: { sticky: sticky }, outer: true)
flash.clear
if timeout
html += remove_element('#flash_messages', timeout: timeout, inner: true)
end
html
end
end
def remove_element(selector, timeout: 0, inner: false)
script = "(function() {";
script << "var el = document.querySelector('#{selector}');"
method = (inner ? "el.innerHTML = ''" : "el.parentNode.removeChild(el)")
script << "setTimeout(function() { #{method}; }, #{timeout});";
script << "})();"
# rubocop:disable Rails/OutputSafety
raw(script);
# rubocop:enable Rails/OutputSafety
end
2018-08-28 16:39:42 +02:00
def disable_element(selector)
# rubocop:disable Rails/OutputSafety
raw("document.querySelector('#{selector}').disabled = true;")
# rubocop:enable Rails/OutputSafety
end
def enable_element(selector)
# rubocop:disable Rails/OutputSafety
raw("document.querySelector('#{selector}').disabled = false;")
# rubocop:enable Rails/OutputSafety
end
2017-06-22 16:45:57 +02:00
def current_email
2018-05-30 18:45:46 +02:00
current_user&.email ||
current_gestionnaire&.email ||
current_administrateur&.email
2017-06-22 16:45:57 +02:00
end
def staging?
ENV['APP_NAME'] == 'tps_dev'
end
2018-08-29 15:00:35 +02:00
def contact_link(title, options = {})
2018-08-30 15:10:18 +02:00
tags, type, dossier_id = options.values_at(:tags, :type, :dossier_id)
options.except!(:tags, :type, :dossier_id)
2018-08-29 15:00:35 +02:00
if Flipflop.support_form?
2018-08-30 15:10:18 +02:00
params = { tags: tags, type: type, dossier_id: dossier_id }.compact
2018-08-29 15:00:35 +02:00
link_to title, contact_url(params), options
else
mail_to CONTACT_EMAIL, title,
options.merge(subject: "Question à propos de demarches-simplifiees.fr")
end
end
def root_path_for_profile(nav_bar_profile)
case nav_bar_profile
when :gestionnaire
gestionnaire_procedures_path
when :user
dossiers_path
else
root_path
end
end
def ensure_safe_json(json)
JSON.parse(json).to_json
rescue Exception => e
Raven.capture_exception(e)
{}
end
2018-08-09 16:45:56 +02:00
def sentry_config
sentry = Rails.application.secrets.sentry
if sentry
{
dsn: sentry[:browser],
id: current_user&.id,
email: current_email
}.to_json
else
{}
end
end
end