5
0
Fork 0
mirror of https://0xacab.org/sutty/sutty synced 2024-07-03 10:56:09 +00:00

cargar los campos desde la plantilla

This commit is contained in:
f 2018-05-14 16:27:29 -03:00
parent 47e149b1c9
commit 17d6d01aeb
No known key found for this signature in database
GPG key ID: F3FDAB97B5F9F7E7
2 changed files with 60 additions and 8 deletions

View file

@ -180,7 +180,7 @@ class Post
# enviamos actualizaciones al front matter, debería traer el contenido
# del post sin cambios
def content
@content ||= @post.try(:content) || @template.try(:content)
@content ||= @post.try(:content) || template.try(:content)
end
# imita Model.update_attributes de ActiveRecord
@ -214,15 +214,22 @@ class Post
@front_matter.dig(name.to_s)
end
# Trae el template a partir del layout
def template_from_layout
@site.templates.find do |t|
t.get_front_matter('slug') == get_front_matter('layout')
end
end
def template_fields
@template_fields ||= @template.front_matter.reject do |key, _|
@template_fields ||= template.front_matter.reject do |key, _|
REJECT_FROM_TEMPLATE.include? key
end.keys
end
# Obtiene el tipo de campo para la plantilla
def template_form_type_for(field)
return if (tt = @template.get_front_matter(field)).nil?
return if (tt = template.get_front_matter(field)).nil?
case
when tt == 'string'
@ -238,20 +245,63 @@ class Post
type
end
def template_multiple_values?(field)
template.get_front_matter(field).is_a? Array
end
# Obtiene los valores posibles para el campo de la plantilla
def template_values_for(field)
return '' if %w[string text].include? @template.get_front_matter(field)
@template.get_front_matter(field)
return '' if %w[string text].include? template.get_front_matter(field)
# Para obtener los valores posibles, hay que procesar la string y
# convertirla a parametros
#
# XXX Prestar atención a no enviar metodos privados
value = template.get_front_matter(field)
# Si es una array de un solo elemento, es un indicador de que
# tenemos que rellenarla con los valores que indica
if value.is_a?(Array) && value.count == 1
values = value.first
else
values = value
end
# Procesar el valor
if values.is_a?(String)
value = values.split(':', 2).map do |v|
collection, attr = v.split('/', 2)
if collection == 'site'
# TODO puede ser peligroso permitir acceder a cualquier
# atributo de site? No estamos trayendo nada fuera de
# lo normal
@site.send(attr.to_sym)
else
@site.everything_of(attr, lang: collection)
end
end
value = value.last.zip value.first
end
# En última instancia, traer el valor por defecto
value
end
def template
@template ||= template_from_layout
end
private
# Completa el front_matter a partir de las variables de otro
def front_matter_from_template
return {} unless @template
return {} unless template
# Convertimos el slug en layout
ft = @template.front_matter.dup
ft = template.front_matter.dup
ft['layout'] = ft.delete('slug')
ft

View file

@ -91,6 +91,8 @@
- when 'check_box'
= check_box_tag name, value: value, class: 'form-control'
- when 'select'
= select_tag name, value, { class: 'form-control' }
= select_tag name, options_for_select(value, @post.get_front_matter(template)),
{ class: 'form-control select2',
multiple: @post.template_multiple_values?(template) }
.form-group
= submit_tag t('posts.save'), class: 'btn btn-success'