From 8ab6b60f902f6d3561f3ce005222bbff5c468573 Mon Sep 17 00:00:00 2001 From: f Date: Tue, 19 Jun 2018 17:38:43 -0300 Subject: [PATCH] crear una tabla para los valores anidados y secar un poco! --- app/helpers/application_helper.rb | 19 ++++++++++++++ app/models/post/template_field.rb | 17 ++++++++++-- app/views/posts/_form.haml | 26 +++---------------- .../posts/template_field/_check_box.haml | 6 +++++ app/views/posts/template_field/_select.haml | 9 +++++++ app/views/posts/template_field/_table.haml | 15 +++++++++++ app/views/posts/template_field/_text.haml | 7 +++++ .../posts/template_field/_text_area.haml | 2 ++ 8 files changed, 76 insertions(+), 25 deletions(-) create mode 100644 app/views/posts/template_field/_check_box.haml create mode 100644 app/views/posts/template_field/_select.haml create mode 100644 app/views/posts/template_field/_table.haml create mode 100644 app/views/posts/template_field/_text.haml create mode 100644 app/views/posts/template_field/_text_area.haml diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index de6be794..287ca144 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1,2 +1,21 @@ module ApplicationHelper + # Devuelve el atributo name de un campo posiblemente anidado + def field_name_for_post(names) + return ['post', names] if names.is_a? String + + names = names.dup + root = 'post' + name = names.pop + names.each do |n| + root = "#{root}[#{n}]" + end + + [root, name] + end + + def field_name_for_post_as_string(names) + f = field_name_for_post(names) + + "#{f.first}[#{f.last}]" + end end diff --git a/app/models/post/template_field.rb b/app/models/post/template_field.rb index b11f31c6..adf4ba13 100644 --- a/app/models/post/template_field.rb +++ b/app/models/post/template_field.rb @@ -25,6 +25,8 @@ class Post @type = 'text' when string? && contents.split('/', 2).count == 2 @type = 'select' + when nested? + @type = 'table' when array? @type = 'select' when boolean? @@ -88,7 +90,7 @@ class Post # Detecta si el valor es una tabla de campos def nested? - value.is_a? Hash + value.is_a?(Hash) || (array? && value.first.is_a?(Hash)) end # Un campo acepta valores abiertos si no es un array con múltiples @@ -129,7 +131,18 @@ class Post end def help - complex? && contents.dig('help') + (complex? && contents.dig('help')) || human + end + + def nested_fields + return unless nested? + + v = value + v = value.first if array? + + @nested_fields ||= v.map do |k,sv| + Post::TemplateField.new post, k, sv + end end # Obtiene los valores posibles para el campo de la plantilla diff --git a/app/views/posts/_form.haml b/app/views/posts/_form.haml index 681e8b06..22760512 100644 --- a/app/views/posts/_form.haml +++ b/app/views/posts/_form.haml @@ -78,32 +78,12 @@ - @post.template_fields.each do |template| - next unless type = template.type .form-group - = label_tag "post_#{template}" do + = label_tag "post_#{template}", id: template do - if template.private? = fa_icon 'lock', title: t('posts.private') - = template.help.present? ? template.help : template.human - - name = "post[#{template}]" + = template.help - value = @post.new? ? template.values : @post.get_front_matter(template) - - case type - - when 'text' - = text_field 'post', template, - value: value, - class: 'form-control', - required: template.required? - - when 'text_area' - = text_area_tag name, value, class: 'form-control', required: template.required? - - when 'check_box' - = hidden_field 'post', template, value: 'false' - = check_box_tag name, 'true', value == 'true', class: 'form-control' - - when 'select' - = select_tag name, options_for_select(template.values, @post.get_front_matter(template)), - { class: 'form-control select2', - multiple: template.multiple?, - required: template.required?, - data: { tags: template.open?, - placeholder: t('posts.select.placeholder')}} - - if template.open? - %small.text-muted.form-text= t('posts.open') + = render "posts/template_field/#{type}", template: template, name: template.key, value: value .invalid-feedback= t('posts.invalid') .form-group = submit_tag t('posts.save'), class: 'btn btn-success', id: 'submit-post' diff --git a/app/views/posts/template_field/_check_box.haml b/app/views/posts/template_field/_check_box.haml new file mode 100644 index 00000000..8b9a07d7 --- /dev/null +++ b/app/views/posts/template_field/_check_box.haml @@ -0,0 +1,6 @@ +- root, field_name = field_name_for_post(name) += hidden_field root, field_name, value: 'false' += check_box_tag field_name_for_post_as_string(name), + 'true', + value == 'true', + class: 'form-control' diff --git a/app/views/posts/template_field/_select.haml b/app/views/posts/template_field/_select.haml new file mode 100644 index 00000000..29c1c4e3 --- /dev/null +++ b/app/views/posts/template_field/_select.haml @@ -0,0 +1,9 @@ += select_tag field_name_for_post_as_string(name), + options_for_select(template.values, @post.get_front_matter(template)), + { class: 'form-control select2', + multiple: template.multiple?, + required: template.required?, + data: { tags: template.open?, + placeholder: t('posts.select.placeholder')}} +- if template.open? + %small.text-muted.form-text= t('posts.open') diff --git a/app/views/posts/template_field/_table.haml b/app/views/posts/template_field/_table.haml new file mode 100644 index 00000000..3f9ce3a4 --- /dev/null +++ b/app/views/posts/template_field/_table.haml @@ -0,0 +1,15 @@ +- template = local_assigns[:template] +%table.table.table-condensed.table-striped + %thead + %tr + - template.nested_fields.each do |nf| + %td= nf.help || nf.human + %tbody + %tr + - template.nested_fields.each do |nf| + - value = @post.new? ? nf.values : @post.get_front_matter(template)[nf] + %td= render "posts/template_field/#{nf.type}", + template: nf, + name: [template.key, nf.key], + value: value + %td= fa_icon 'close', title: 'Eliminar' diff --git a/app/views/posts/template_field/_text.haml b/app/views/posts/template_field/_text.haml new file mode 100644 index 00000000..4395df88 --- /dev/null +++ b/app/views/posts/template_field/_text.haml @@ -0,0 +1,7 @@ +-# Si el campo está anidado, la raíz del atributo name tiene que +-# contener los demás elementos del template superior +- root, field_name = field_name_for_post(name) += text_field root, field_name, + value: local_assigns[:value], + class: 'form-control', + required: template.required? diff --git a/app/views/posts/template_field/_text_area.haml b/app/views/posts/template_field/_text_area.haml new file mode 100644 index 00000000..3ab02f3f --- /dev/null +++ b/app/views/posts/template_field/_text_area.haml @@ -0,0 +1,2 @@ += text_area_tag field_name_for_post_as_string(name), value, + class: 'form-control', required: template.required?