Module: Admin::ConfigsHelper

Defined in:
app/helpers/admin/configs_helper.rb

Instance Method Summary collapse

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.

Options Hash (options):

  • :required (Boolean)

    Wether field is shown as required (default not).

  • :rules (Array<IceCube::Rule>)

    Rules for as: :recurring_select


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/helpers/admin/configs_helper.rb', line 12

def config_input(form, key, options = {}, &block)
  return unless @cfg.allowed_key? key
  options[:label] ||= config_input_label(form, key)
  options[:required] ||= false
  options[:input_html] ||= {}
  config_input_field_options form, key, options[:input_html]
  config_input_tooltip_options form, key, options[:input_html]
  if options[:as] == :boolean
    options[:input_html][:checked] = 'checked' if v=options[:input_html].delete(:value) && v!='false'
    options[:checked_value] = 'true' if options[:checked_value].nil?
    options[:unchecked_value] = 'false' if options[:unchecked_value].nil?
  elsif options[:collection] || options[:as] == :select
    options[:selected] = options[:input_html].delete(:value)
    return form.input key, options, &block
  elsif options[:as] == :time_zone
    options[:default] = options[:input_html].delete(:value)
    return form.input key, options, &block
  end
  block ||= proc { config_input_field form, key, options.merge(options[:input_html]) } if options[:as] == :select_recurring
  form.input key, options, &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.

Options Hash (options):

  • :checked_value (String)

    Value for boolean when checked (default true)

  • :unchecked_value (String)

    Value for boolean when not checked (default false)

See Also:


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/helpers/admin/configs_helper.rb', line 48

def config_input_field(form, key, options = {})
  return unless @cfg.allowed_key? :key
  options[:required] ||= false
  config_input_field_options form, key, options
  config_input_tooltip_options form, key, options
  if options[:as] == :boolean
    checked_value = options.delete(:checked_value) || 'true'
    unchecked_value = options.delete(:unchecked_value) || 'false'
    options[:checked] = 'checked' if v=options.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, options, checked_value, false)
  elsif options[:as] == :select_recurring
    options[:value] = FoodsoftDateUtil.rule_from(options[:value])
    options[:rules] ||= []
    options[:rules].unshift options[:value] unless options[:value].blank?
    options[:rules].push [I18n.t('recurring_select.not_recurring'), '{}'] if options.delete(:allow_blank) # blank after current value
    form.select_recurring key, options.delete(:rules).uniq, options
  else
    form.input_field key, options
  end
end

#config_input_label(form, key) ⇒ String

Returns Label name in form for configuration key.

See Also:


38
39
40
41
# File 'app/helpers/admin/configs_helper.rb', line 38

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


118
119
120
# File 'app/helpers/admin/configs_helper.rb', line 118

def config_tooltip(form, key, options={}, &block)
   :span, config_input_tooltip_options(form, key, options), &block
end

#config_use_heading(form, key, options = {}) ⇒ String

Returns Form heading with checkbox with block passed in expandable fieldset.

Options Hash (options):

  • :label (String)

    Label to show


74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'app/helpers/admin/configs_helper.rb', line 74

def config_use_heading(form, key, options = {})
  head =  :label do
   lbl = options[:label] || config_input_label(form, key)
   field = config_input_field(form, key, as: :boolean, boolean_style: :inline,
                              data: {toggle: 'collapse', target: "##{key}-fields"})
    :h4 do
     # put in span to keep space for tooltip at right
      :span, (lbl + field).html_safe, config_input_tooltip_options(form, key, {})
   end
  end
  fields = (:fieldset, id: "#{key}-fields", class: "collapse#{' in' if @cfg[key]}") do
    yield
  end
  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.

97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'app/helpers/admin/configs_helper.rb', line 97

def show_config_value(key, value)
  if key =~ /passw|secr|key/
    '(protected)'
  elsif value.is_a? Hash
     :ul do
      value.map do |k,v|
         :li, (:tt, "#{k}: ") + show_config_value(k, v).to_s
      end.join.html_safe
    end
  elsif value.is_a? Enumerable
     :ul, value.map {|v|  :li, h(v)}.join.html_safe
  elsif key =~ /url|website|www|homepage/
    link_to(value, value.to_s).html_safe
  else
    value
  end
end