Module: Admin::ConfigurationHelper

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

Instance Method Summary collapse

Instance Method Details

#definition_for(key) ⇒ Object



75
76
77
78
79
# File 'app/helpers/admin/configuration_helper.rb', line 75

def definition_for(key)
  if setting = setting_for(key)
    setting.definition
  end
end

#edit_config(key, _options = {}) ⇒ Object

Renders the setting as label and appropriate input field:

edit_setting("admin.title")
=> <label for="admin_title">Admin title<label><input type="text" name="config['admin.title']" id="admin_title" value="TrustyCms CMS" />

edit_config("defaults.page.status")
=>
<label for="defaults_page_status">Default page status<label>
<select type="text" name="config['defaults.page.status']" id="defaults_page_status">
  <option value="Draft">Draft</option>
  ...
</select>

edit_setting("user.allow_password_reset?")
=> <label for="user_allow_password_reset_">Admin title<label><input type="checkbox" name="config['user.allow_password_reset?']" id="user_allow_password_reset_" value="1" checked="checked" />


44
45
46
47
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/configuration_helper.rb', line 44

def edit_config(key, _options = {})
  setting = setting_for(key)
  domkey = key.gsub(/\W/, '_')
  name = "trusty_config[#{key}]"
  title = t("trusty_config.#{key}").titlecase
  title << (:span, " (#{t("units.#{setting.units}")})", class: 'units') if setting.units
  value = params[key.to_sym].nil? ? setting.value : params[key.to_sym]
  html = ''
  if setting.boolean?
    html << hidden_field_tag(name, 0)
    html << check_box_tag(name, 1, value, class: 'setting', id: domkey)
    html << (:label, title.html_safe, class: 'checkbox', for: domkey)
  elsif setting.selector?
    html << (:label, title.html_safe, for: domkey)
    html << select_tag(name, options_for_select(setting.definition.selection, value), class: 'setting', id: domkey)
  else
    html << (:label, title.html_safe, for: domkey)
    html << text_field_tag(name, value, class: 'textbox', id: domkey)
  end
  if setting.errors[:value].present?
    html << (:span, [setting.errors[:value]].flatten.first, class: 'error')
    html = (:span, html.html_safe, class: 'error-with-field')
  end
  html.html_safe
end

#setting_for(key) ⇒ Object



70
71
72
73
# File 'app/helpers/admin/configuration_helper.rb', line 70

def setting_for(key)
  @trusty_config ||= {} # normally initialized in Admin::ConfigurationController
  @trusty_config[key] ||= TrustyCms.config.find_or_initialize_by(key: key)
end

#show_config(key, options = {}) ⇒ Object

Renders the setting as label and value:

show_config("admin.title")
=> <label for="admin_title">Admin title<label><span id="admin_title">TrustyCms CMS</span>


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/helpers/admin/configuration_helper.rb', line 9

def show_config(key, options = {})
  setting = setting_for(key)
  setting.valid?
  domkey = key.gsub(/\W/, '_')
  html = ''
  html << (:label, t("trusty_config.#{key}").titlecase, for: domkey)
  if setting.boolean?
    value = setting.checked? ? t('yes') : t('no')
    html << (:span, value, id: domkey, class: "#{value} #{options[:class]}")
  else
    value = setting.selected_value || setting.value
    html << (:span, value, id: domkey, class: options[:class])
  end
  html << (:span, " #{t("units.#{setting.units}")}", class: 'units') if setting.units
  html << (:span, " #{t('warning')}: #{[setting.errors[:value]].flatten.first}", class: 'warning') if setting.errors.messages[:value].present?
  Rails.logger.error(html)
  html.html_safe
end