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

cargar campos desde la plantilla

This commit is contained in:
f 2018-05-11 17:00:45 -03:00
parent 265fd9fff5
commit 8d70037aee
No known key found for this signature in database
GPG key ID: F3FDAB97B5F9F7E7
2 changed files with 52 additions and 1 deletions

View file

@ -15,11 +15,15 @@ require 'jekyll/utils'
# datos y los sincroniza al momento de leer y de escribir el Document.
class Post
attr_accessor :content, :front_matter
attr_reader :post, :site, :errors, :old_post, :lang, :template
attr_reader :post, :site, :errors, :old_post, :lang, :template,
:template_fields
REJECT_FROM_DATA = %w[excerpt].freeze
# datos que no tienen que terminar en el front matter
REJECT_FROM_FRONT_MATTER = %w[date slug draft ext].freeze
# datos que no traemos del template
REJECT_FROM_TEMPLATE = %w[date draft categories layout title ext tags date slug].freeze
# Trabajar con posts. Si estamos creando uno nuevo, el **site** y
# el **front_matter** son necesarios, sino, **site** y **post**.
@ -210,6 +214,36 @@ class Post
@front_matter.dig(name.to_s)
end
def template_fields
@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?
case
when tt == 'string'
type = 'text'
when tt == 'text'
type = 'text_area'
when tt.is_a?(Array)
type = 'select'
when tt.is_a?(FalseClass) || tt.is_a?(TrueClass)
type = 'check_box'
end
type
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)
end
private
# Completa el front_matter a partir de las variables de otro

View file

@ -75,5 +75,22 @@
@post.get_front_matter(:lang).try(:dig, lang)),
{ class: 'form-control select2' }
%small.text-muted.form-text= t('posts.lang_help')
- @post.template_fields.each do |template|
- next unless type = @post.template_form_type_for(template)
.form-group
= label_tag "post_#{template}", template.humanize
- name = "post[#{template}]"
- value = @post.template_values_for(template)
- case type
- when 'text'
= text_field 'post', template,
value: value,
class: 'form-control'
- when 'text_area'
= text_area_tag name, value, class: 'form-control'
- when 'check_box'
= check_box_tag name, value: value, class: 'form-control'
- when 'select'
= select_tag name, value, { class: 'form-control' }
.form-group
= submit_tag t('posts.save'), class: 'btn btn-success'