5
0
Fork 0
mirror of https://0xacab.org/sutty/sutty synced 2024-11-15 01:21:42 +00:00

crear una tabla para los valores anidados

y secar un poco!
This commit is contained in:
f 2018-06-19 17:38:43 -03:00
parent bd78dd6ce4
commit 8ab6b60f90
No known key found for this signature in database
GPG key ID: F3FDAB97B5F9F7E7
8 changed files with 76 additions and 25 deletions

View file

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

View file

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

View file

@ -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'

View file

@ -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'

View file

@ -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')

View file

@ -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'

View file

@ -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?

View file

@ -0,0 +1,2 @@
= text_area_tag field_name_for_post_as_string(name), value,
class: 'form-control', required: template.required?