From 8d70037aee94fb2f744f4ded1f99787e356fdc82 Mon Sep 17 00:00:00 2001 From: f Date: Fri, 11 May 2018 17:00:45 -0300 Subject: [PATCH] cargar campos desde la plantilla --- app/models/post.rb | 36 +++++++++++++++++++++++++++++++++++- app/views/posts/_form.haml | 17 +++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/app/models/post.rb b/app/models/post.rb index f26139c6..9b6b8439 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -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 diff --git a/app/views/posts/_form.haml b/app/views/posts/_form.haml index 9cd442bd..e81dcd8f 100644 --- a/app/views/posts/_form.haml +++ b/app/views/posts/_form.haml @@ -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'