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.

Parameters:

  • form (ActionView::Helpers::FormBuilder)

    Form object.

  • key (Symbol, String)

    Configuration key.

  • options (Hash) (defaults to: {})

    Options passed to the form builder.

Options Hash (options):

  • :required (Boolean)

    Wether field is shown as required (default not).

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

    Rules for as: :recurring_select

Returns:

  • (String)

    Form input for configuration key.



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, 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
  if options[:as] == :select_recurring
    block ||= proc {
      config_input_field form, key, options.merge(options[:input_html])
    }
  end
  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.

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :checked_value (String)

    Value for boolean when checked (default true)

  • :unchecked_value (String)

    Value for boolean when not checked (default false)

Returns:

  • (String)

    Form input field for configuration key.

See Also:



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, 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] if options[:value].present?
    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.

Parameters:

  • form (ActionView::Helpers::FormBuilder)

    Form object.

  • key (Symbol, String)

    Configuration key.

Returns:

  • (String)

    Label name in form for configuration key.

See Also:



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).

Parameters:

  • form (ActionView::Helpers::FormBuilder)

    Form object.

  • key (Symbol, String)

    Configuration key of a boolean (e.g. use_messages).

Returns:

  • (String)

    Tooltip element (span)



123
124
125
# File 'app/helpers/admin/configs_helper.rb', line 123

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

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

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

Parameters:

  • form (ActionView::Helpers::FormBuilder)

    Form object.

  • key (Symbol, String)

    Configuration key of a boolean (e.g. use_messages).

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :label (String)

    Label to show

Returns:

  • (String)

    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, options = {}, &block)
  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]}", &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.

Parameters:

  • key (String)

    Configuration key

  • value (String)

    Configuration value

Returns:

  • (String)

    Configuration value suitable for rendering in HTML.



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
     :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