2019-04-04 09:33:41 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# Various helpers methods
|
2015-05-05 03:10:25 +02:00
|
|
|
module ApplicationHelper
|
2019-04-04 09:33:41 +02:00
|
|
|
require 'message_format'
|
|
|
|
|
|
|
|
##
|
|
|
|
# Verify if the provided attribute is in the provided attributes array, whatever it exists or not
|
|
|
|
# @param attributes {Array|nil}
|
|
|
|
# @param attribute {String}
|
|
|
|
##
|
|
|
|
def attribute_requested?(attributes, attribute)
|
|
|
|
attributes.try(:include?, attribute)
|
|
|
|
end
|
|
|
|
|
2020-05-26 10:53:49 +02:00
|
|
|
def bootstrap_class_for(flash_type)
|
2019-04-04 09:33:41 +02:00
|
|
|
{ flash: 'alert-success', alert: 'alert-danger', notice: 'alert-info' }[flash_type.to_sym] || flash_type.to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
def flash_messages(_opts = {})
|
|
|
|
flash.each do |msg_type, message|
|
|
|
|
concat(content_tag(:div, message, class: "flash-message alert #{bootstrap_class_for(msg_type)} fade in") do
|
|
|
|
concat content_tag(:button, 'x', class: 'close', data: { dismiss: 'alert' })
|
|
|
|
concat message
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def print_slot(starting, ending)
|
|
|
|
"#{starting.strftime('%H:%M')} - #{ending.strftime('%H:%M')}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def class_exists?(class_name)
|
|
|
|
klass = Module.const_get(class_name)
|
|
|
|
klass.is_a?(Class)
|
|
|
|
rescue NameError
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Allow to treat a rails i18n key as a MessageFormat interpolated pattern. Used in ruby views (API/mails)
|
|
|
|
# @param key {String} Ruby-on-Rails I18n key (from config/locales/xx.yml)
|
|
|
|
# @param interpolations {Hash} list of variables to interpolate, following ICU MessageFormat syntax
|
|
|
|
##
|
|
|
|
def _t(key, interpolations)
|
|
|
|
message = MessageFormat.new(I18n.t(scope_key_by_partial(key)), I18n.locale.to_s)
|
|
|
|
text = message.format(interpolations)
|
|
|
|
if html_safe_translation_key?(key)
|
2022-12-01 13:03:19 +01:00
|
|
|
text.html_safe # rubocop:disable Rails/OutputSafety
|
2019-04-04 09:33:41 +02:00
|
|
|
else
|
|
|
|
text
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def bool_to_sym(bool)
|
|
|
|
bool ? :true : :false # rubocop:disable Lint/BooleanSymbol
|
|
|
|
end
|
|
|
|
|
2020-05-26 10:53:49 +02:00
|
|
|
def str_to_bool(str, default = 'true')
|
|
|
|
str.to_s.casecmp(default).zero?
|
|
|
|
end
|
|
|
|
|
2019-04-04 09:33:41 +02:00
|
|
|
def amount_to_f(amount)
|
|
|
|
amount / 100.00
|
|
|
|
end
|
2016-07-18 18:16:54 +02:00
|
|
|
|
2022-12-01 15:43:16 +01:00
|
|
|
# Return the given amount in centimes, without floating-point imprecision errors
|
|
|
|
def to_centimes(amount)
|
|
|
|
(BigDecimal(amount.to_s) * 100.0).to_f
|
|
|
|
end
|
|
|
|
|
2022-11-23 17:35:39 +01:00
|
|
|
# Return the given parameter as it, or as an array if it can be parsed as an array
|
|
|
|
def may_array(param)
|
2022-12-07 12:43:36 +01:00
|
|
|
return param if param.is_a?(Array)
|
|
|
|
|
2022-11-23 17:35:39 +01:00
|
|
|
return param unless param&.chars&.first == '[' && param&.chars&.last == ']'
|
|
|
|
|
|
|
|
param.gsub(/[\[\]]/i, '').split(',')
|
|
|
|
end
|
|
|
|
|
2019-04-04 09:33:41 +02:00
|
|
|
private
|
|
|
|
|
|
|
|
## inspired by gems/actionview-4.2.5/lib/action_view/helpers/translation_helper.rb
|
2022-12-01 13:03:19 +01:00
|
|
|
# rubocop:disable Rails/HelperInstanceVariable
|
2019-04-04 09:33:41 +02:00
|
|
|
def scope_key_by_partial(key)
|
|
|
|
if key.to_s.first == '.'
|
2022-11-23 17:35:39 +01:00
|
|
|
raise "Cannot use t(#{key.inspect}) shortcut because path is not available" unless @virtual_path # rubocop:disable Rails/HelperInstanceVariable
|
2019-04-04 09:33:41 +02:00
|
|
|
|
2022-11-23 17:35:39 +01:00
|
|
|
@virtual_path.gsub(%r{/_?}, '.') + key.to_s # rubocop:disable Rails/HelperInstanceVariable
|
2019-04-04 09:33:41 +02:00
|
|
|
else
|
|
|
|
key
|
|
|
|
end
|
|
|
|
end
|
2022-12-01 13:03:19 +01:00
|
|
|
# rubocop:enable Rails/HelperInstanceVariable
|
2019-04-04 09:33:41 +02:00
|
|
|
|
|
|
|
def html_safe_translation_key?(key)
|
|
|
|
key.to_s =~ /(\b|_|\.)html$/
|
|
|
|
end
|
2015-05-05 03:10:25 +02:00
|
|
|
end
|