Module: Admin::ConfigsHelper
- Defined in:
- app/helpers/admin/configs_helper.rb
Instance Method Summary collapse
-
#config_input(form, key, options = {}, &block) ⇒ String
Returns form input for configuration key.
-
#config_input_field(form, key, options = {}) ⇒ String
Form input field for configuration key.
-
#config_input_label(form, key) ⇒ String
Label name in form for configuration key.
-
#config_tooltip(form, key, options = {}, &block) ⇒ String
Tooltip element (span).
-
#config_use_heading(form, key, options = {}, &block) ⇒ String
Form heading with checkbox with block passed in expandable
fieldset
. -
#show_config_value(key, value) ⇒ String
Returns configuration value suitable for rendering in HTML.
Instance Method Details
#config_input(form, key, options = {}, &block) ⇒ String
TODO:
find way to pass current value to time_zone input without using default
Returns form input for configuration key.
For configuration keys that contain a {Hash}, {ActiveView::Helpers::FormBuilder#fields_for fields_for} can be used.
When the key is not {FoodsoftConfig#allowed_key? allowed}, +nil+ is returned.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'app/helpers/admin/configs_helper.rb', line 12 def config_input(form, key, = {}, &block) return unless @cfg.allowed_key? key [:label] ||= config_input_label(form, key) [:required] ||= false [:input_html] ||= {} form, key, [:input_html] form, key, [:input_html] if [:as] == :boolean [:input_html][:checked] = 'checked' if v = [:input_html].delete(:value) && v != 'false' [:checked_value] = 'true' if [:checked_value].nil? [:unchecked_value] = 'false' if [:unchecked_value].nil? elsif [:collection] || [:as] == :select [:selected] = [:input_html].delete(:value) return form.input key, , &block elsif [:as] == :time_zone [:default] = [:input_html].delete(:value) return form.input key, , &block end if [:as] == :select_recurring block ||= proc { config_input_field form, key, .merge([:input_html]) } end form.input key, , &block end |
#config_input_field(form, key, options = {}) ⇒ String
TODO:
find out how to pass checked_value
and unchecked_value
to input_field
Returns Form input field for configuration key.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'app/helpers/admin/configs_helper.rb', line 53 def config_input_field(form, key, = {}) return unless @cfg.allowed_key? :key [:required] ||= false form, key, form, key, if [:as] == :boolean checked_value = .delete(:checked_value) || 'true' unchecked_value = .delete(:unchecked_value) || 'false' [:checked] = 'checked' if v = .delete(:value) && v != 'false' # different key for hidden field so that allow clocking on label focuses the control form.hidden_field(key, id: "#{key}_", value: unchecked_value, as: :hidden) + form.check_box(key, , checked_value, false) elsif [:as] == :select_recurring [:value] = FoodsoftDateUtil.rule_from([:value]) [:rules] ||= [] [:rules].unshift [:value] if [:value].present? [:rules].push [I18n.t('recurring_select.not_recurring'), '{}'] if .delete(:allow_blank) # blank after current value form.select_recurring key, .delete(:rules).uniq, else form.input_field key, end end |
#config_input_label(form, key) ⇒ String
Returns Label name in form for configuration key.
43 44 45 46 |
# File 'app/helpers/admin/configs_helper.rb', line 43 def config_input_label(form, key) cfg_path = form.lookup_model_names[1..-1] + [key] I18n.t("config.keys.#{cfg_path.map(&:to_s).join('.')}") end |
#config_tooltip(form, key, options = {}, &block) ⇒ String
Returns Tooltip element (span).
123 124 125 |
# File 'app/helpers/admin/configs_helper.rb', line 123 def config_tooltip(form, key, = {}, &block) content_tag :span, (form, key, ), &block end |
#config_use_heading(form, key, options = {}, &block) ⇒ String
Returns Form heading with checkbox with block passed in expandable fieldset
.
81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'app/helpers/admin/configs_helper.rb', line 81 def config_use_heading(form, key, = {}, &block) head = content_tag :label do lbl = [:label] || config_input_label(form, key) field = config_input_field(form, key, as: :boolean, boolean_style: :inline, data: { toggle: 'collapse', target: "##{key}-fields" }) content_tag :h4 do # put in span to keep space for tooltip at right content_tag :span, (lbl + field).html_safe, (form, key, {}) end end fields = content_tag(:fieldset, id: "#{key}-fields", class: "collapse#{' in' if @cfg[key]}", &block) head + fields end |
#show_config_value(key, value) ⇒ String
Returns configuration value suitable for rendering in HTML.
Makes keys different from +app_config.yml+ configuration bold,
protects sensitive values like keys and passwords, and makes
links from URLs.
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'app/helpers/admin/configs_helper.rb', line 102 def show_config_value(key, value) if key =~ /passw|secr|key/ '(protected)' elsif value.is_a? Hash content_tag :ul do value.map do |k, v| content_tag :li, content_tag(:tt, "#{k}: ") + show_config_value(k, v).to_s end.join.html_safe end elsif value.is_a? Enumerable content_tag :ul, value.map { |v| content_tag :li, h(v) }.join.html_safe elsif key =~ /url|website|www|homepage/ link_to(value, value.to_s).html_safe else value end end |