Merge pull request #139 from rthbound/breaks_up_complex_method

Break complex method apart, SettingsHelper
This commit is contained in:
uu59 2015-01-15 13:02:28 +09:00
commit 8a0ad93a24

View File

@ -25,26 +25,43 @@ module SettingsHelper
end end
def nested_field(html, form, key, opts = {}) def nested_field(html, form, key, opts = {})
child_data = form.object.class.children[key] klass = child_data(form, key)[:class]
klass = child_data[:class] options = child_data(form, key)[:options]
options = child_data[:options]
children = form.object.send(key) || {"0" => {}} children = form.object.send(key) || {"0" => {}}
children.each_pair do |index, child| children.each_pair do |index, child|
html << %Q!<div class="js-nested-column #{options[:multiple] ? "js-multiple" : ""} well well-sm">! html << open_nested_div(options[:multiple])
if options[:multiple] html << append_and_remove_links if options[:multiple]
html << %Q!<a class="btn btn-xs btn-default js-append">#{icon('fa-plus')}</a> !
html << %Q!<a class="btn btn-xs btn-default js-remove" style="display:none">#{icon('fa-minus')}</a> !
end
html << h(form.label(key)) html << h(form.label(key))
form.fields_for("#{key}[#{index}]", klass.new(child)) do |ff| html << nested_fields(form, key, index, klass, child)
klass::KEYS.each do |k|
html << field(ff, k)
end
end
html << "</div>" html << "</div>"
end end
end end
def open_nested_div(multiple)
%Q!<div class="js-nested-column #{ multiple ? "js-multiple" : "" } well well-sm">!
end
def nested_fields(form, key, index, klass, child)
nested_html = ""
form.fields_for("#{key}[#{index}]", klass.new(child)) do |ff|
klass::KEYS.each do |k|
nested_html << field(ff, k)
end
end
nested_html
end
def append_and_remove_links
%Q!<a class="btn btn-xs btn-default js-append">icon('fa-plus')}</a> ! +
%Q!<a class="btn btn-xs btn-default js-remove" style="display:none">icon('fa-minus')}</a> !
end
def child_data(form, key)
form.object.class.children[key]
end
def choice_field(html, form, key, opts = {}) def choice_field(html, form, key, opts = {})
html << h(form.label(key)) html << h(form.label(key))
html << " " # NOTE: Adding space for padding html << " " # NOTE: Adding space for padding