From 7e7b7e74500245d8fbd3ed4385d8ba45a7f41a11 Mon Sep 17 00:00:00 2001 From: f Date: Mon, 25 Jun 2018 17:44:47 -0300 Subject: [PATCH] esconder el contenido si la ficha es solo metadatos #51 --- app/models/post.rb | 12 +++++++++++- app/views/posts/_form.haml | 13 +++++++------ 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/app/models/post.rb b/app/models/post.rb index 6dd80b65..f0571abc 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -22,7 +22,7 @@ class Post # 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 + REJECT_FROM_TEMPLATE = %w[has_content date draft categories layout title ext tags date slug].freeze DEFAULT_PARAMS = [:title, :date, :content, :slug, :cover, :layout, :permalink, :dir, { lang: {} }, { tags: [] }, { categories: [] }] @@ -185,6 +185,11 @@ class Post @content ||= @post.try(:content) || template.try(:content) end + # Determina si el post lleva contenido o es solo front_matter + def content? + template.fetch_front_matter('has_content', true) + end + # imita Model.update_attributes de ActiveRecord def update_attributes(attrs) # el cuerpo se maneja por separado @@ -225,6 +230,11 @@ class Post @front_matter.dig(name.to_s) end + # Como get_front_matter pero con un valor por defecto + def fetch_front_matter(name, default) + @front_matter.fetch(name, default) + end + # Trae el template a partir del layout def template_from_layout @site.templates.find do |t| diff --git a/app/views/posts/_form.haml b/app/views/posts/_form.haml index 8f7c0f32..d354cd9e 100644 --- a/app/views/posts/_form.haml +++ b/app/views/posts/_form.haml @@ -28,12 +28,13 @@ .form-group = label_tag 'post_title', t('posts.title') = text_field 'post', 'title', value: @post.title, class: field_class, required: true - .form-group{class: direction} - = render 'layouts/help', help: [ t('help.markdown.intro'), - t('help.distraction_free_html'), - t('help.preview_html') ] - = text_area_tag 'post[content]', @post.content, autofocus: true, - class: 'post-content' + - if @post.content? + .form-group{class: direction} + = render 'layouts/help', help: [ t('help.markdown.intro'), + t('help.distraction_free_html'), + t('help.preview_html') ] + = text_area_tag 'post[content]', @post.content, autofocus: true, + class: 'post-content' .form-group = label_tag 'post_date', t('posts.date') = date_field 'post', 'date', value: @post.date.try(:strftime, '%F'),