mirror of
https://0xacab.org/sutty/sutty
synced 2025-01-19 14:13:38 +00:00
crear una tabla para los valores anidados
y secar un poco!
This commit is contained in:
parent
bd78dd6ce4
commit
8ab6b60f90
8 changed files with 76 additions and 25 deletions
|
@ -1,2 +1,21 @@
|
||||||
module ApplicationHelper
|
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
|
end
|
||||||
|
|
|
@ -25,6 +25,8 @@ class Post
|
||||||
@type = 'text'
|
@type = 'text'
|
||||||
when string? && contents.split('/', 2).count == 2
|
when string? && contents.split('/', 2).count == 2
|
||||||
@type = 'select'
|
@type = 'select'
|
||||||
|
when nested?
|
||||||
|
@type = 'table'
|
||||||
when array?
|
when array?
|
||||||
@type = 'select'
|
@type = 'select'
|
||||||
when boolean?
|
when boolean?
|
||||||
|
@ -88,7 +90,7 @@ class Post
|
||||||
|
|
||||||
# Detecta si el valor es una tabla de campos
|
# Detecta si el valor es una tabla de campos
|
||||||
def nested?
|
def nested?
|
||||||
value.is_a? Hash
|
value.is_a?(Hash) || (array? && value.first.is_a?(Hash))
|
||||||
end
|
end
|
||||||
|
|
||||||
# Un campo acepta valores abiertos si no es un array con múltiples
|
# Un campo acepta valores abiertos si no es un array con múltiples
|
||||||
|
@ -129,7 +131,18 @@ class Post
|
||||||
end
|
end
|
||||||
|
|
||||||
def help
|
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
|
end
|
||||||
|
|
||||||
# Obtiene los valores posibles para el campo de la plantilla
|
# Obtiene los valores posibles para el campo de la plantilla
|
||||||
|
|
|
@ -78,32 +78,12 @@
|
||||||
- @post.template_fields.each do |template|
|
- @post.template_fields.each do |template|
|
||||||
- next unless type = template.type
|
- next unless type = template.type
|
||||||
.form-group
|
.form-group
|
||||||
= label_tag "post_#{template}" do
|
= label_tag "post_#{template}", id: template do
|
||||||
- if template.private?
|
- if template.private?
|
||||||
= fa_icon 'lock', title: t('posts.private')
|
= fa_icon 'lock', title: t('posts.private')
|
||||||
= template.help.present? ? template.help : template.human
|
= template.help
|
||||||
- name = "post[#{template}]"
|
|
||||||
- value = @post.new? ? template.values : @post.get_front_matter(template)
|
- value = @post.new? ? template.values : @post.get_front_matter(template)
|
||||||
- case type
|
= render "posts/template_field/#{type}", template: template, name: template.key, value: value
|
||||||
- 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')
|
|
||||||
.invalid-feedback= t('posts.invalid')
|
.invalid-feedback= t('posts.invalid')
|
||||||
.form-group
|
.form-group
|
||||||
= submit_tag t('posts.save'), class: 'btn btn-success', id: 'submit-post'
|
= submit_tag t('posts.save'), class: 'btn btn-success', id: 'submit-post'
|
||||||
|
|
6
app/views/posts/template_field/_check_box.haml
Normal file
6
app/views/posts/template_field/_check_box.haml
Normal 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'
|
9
app/views/posts/template_field/_select.haml
Normal file
9
app/views/posts/template_field/_select.haml
Normal 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')
|
15
app/views/posts/template_field/_table.haml
Normal file
15
app/views/posts/template_field/_table.haml
Normal 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'
|
7
app/views/posts/template_field/_text.haml
Normal file
7
app/views/posts/template_field/_text.haml
Normal 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?
|
2
app/views/posts/template_field/_text_area.haml
Normal file
2
app/views/posts/template_field/_text_area.haml
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
= text_area_tag field_name_for_post_as_string(name), value,
|
||||||
|
class: 'form-control', required: template.required?
|
Loading…
Reference in a new issue