From 7bf9d89126872c57d294fa8b8f77303ec0e768f7 Mon Sep 17 00:00:00 2001 From: "Ryan T. Hosford" Date: Sun, 11 Jan 2015 17:26:46 -0600 Subject: [PATCH] Changes in response to CodeClimate score for file - Breaks method up for clarity and readability --- app/helpers/settings_helper.rb | 89 +++++++++++++++++++++------------- 1 file changed, 55 insertions(+), 34 deletions(-) diff --git a/app/helpers/settings_helper.rb b/app/helpers/settings_helper.rb index d368ab3..6f9d98c 100644 --- a/app/helpers/settings_helper.rb +++ b/app/helpers/settings_helper.rb @@ -2,42 +2,63 @@ module SettingsHelper def field(form, key, opts = {}) html = '
' - case form.object.column_type(key) - when :hidden - return form.hidden_field(key) - when :boolean, :flag - html << form.check_box(key, {}, "true", "false") - html << " " # NOTE: Adding space for padding - html << h(form.label(key)) - when :choice - html << h(form.label(key)) - html << " " # NOTE: Adding space for padding - html << form.select(key, form.object.values_of(key), opts) - when :nested - child_data = form.object.class.children[key] - klass = child_data[:class] - options = child_data[:options] - children = form.object.send(key) || {"0" => {}} - children.each_pair do |index, child| - html << %Q!
! - if options[:multiple] - html << %Q!#{icon('fa-plus')} ! - html << %Q! ! - end - html << h(form.label(key)) - form.fields_for("#{key}[#{index}]", klass.new(child)) do |ff| - klass::KEYS.each do |k| - html << field(ff, k) - end - end - html << "
" - end - else - html << h(form.label(key)) - html << form.text_field(key, class: "form-control") - end + field_resolver(form.object.column_type(key), html, form, key, opts) html << "
" html.html_safe end + + private + def field_resolver(type, html, form, key, opts) + case type + when :hidden + return form.hidden_field(key) + when :boolean, :flag + boolean_field(html, form, key, opts) + when :choice + choice_field(html, form, key, opts) + when :nested + nested_field(html, form, key, opts) + else + other_field(html, form, key, opts) + end + end + + def nested_field(html, form, key, opts = {}) + child_data = form.object.class.children[key] + klass = child_data[:class] + options = child_data[:options] + children = form.object.send(key) || {"0" => {}} + children.each_pair do |index, child| + html << %Q!
! + if options[:multiple] + html << %Q!#{icon('fa-plus')} ! + html << %Q! ! + end + html << h(form.label(key)) + form.fields_for("#{key}[#{index}]", klass.new(child)) do |ff| + klass::KEYS.each do |k| + html << field(ff, k) + end + end + html << "
" + end + end + + def choice_field(html, form, key, opts = {}) + html << h(form.label(key)) + html << " " # NOTE: Adding space for padding + html << form.select(key, form.object.values_of(key), opts) + end + + def boolean_field(html, form, key, opts = {}) + html << form.check_box(key, {}, "true", "false") + html << " " # NOTE: Adding space for padding + html << h(form.label(key)) + end + + def other_field(html, form, key, opts = {}) + html << h(form.label(key)) + html << form.text_field(key, class: "form-control") + end end