From 0dc9d1d157a8482851ee5c06706aefd2c1d30041 Mon Sep 17 00:00:00 2001 From: f Date: Mon, 23 Jul 2018 16:48:34 -0300 Subject: [PATCH] poder ocultar campos por defecto --- app/models/post.rb | 9 ++++- app/views/posts/_form.haml | 82 ++++++++++++++++++++------------------ 2 files changed, 51 insertions(+), 40 deletions(-) diff --git a/app/models/post.rb b/app/models/post.rb index aed3be03..e1449c7a 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[has_content date draft categories layout title ext tags date slug].freeze + REJECT_FROM_TEMPLATE = %w[draft categories layout title ext tags date slug].freeze DEFAULT_PARAMS = [:title, :date, :content, :slug, :cover, :layout, :permalink, :dir, { lang: {} }, { tags: [] }, { categories: [] }] @@ -187,7 +187,11 @@ class Post # Determina si el post lleva contenido o es solo front_matter def content? - template && template.fetch_front_matter('has_content', true) + has_field? :content + end + + def has_field?(field) + template && template.fetch_front_matter("has_#{field.to_s}", true) end # imita Model.update_attributes de ActiveRecord @@ -300,6 +304,7 @@ class Post return [] unless template @template_fields ||= template.front_matter.map do |key, contents| next if REJECT_FROM_TEMPLATE.include? key + next if key.start_with? 'has_' Post::TemplateField.new(self, key, contents) end.compact diff --git a/app/views/posts/_form.haml b/app/views/posts/_form.haml index 71fcb87f..352b92ba 100644 --- a/app/views/posts/_form.haml +++ b/app/views/posts/_form.haml @@ -35,44 +35,50 @@ 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'), - class: 'form-control' - %small.text-muted.form-text= t('posts.date_help') - .form-group - = render 'layouts/help', help: t('help.autocomplete_html') - = label_tag 'post_categories', t('posts.categories') - = select_tag 'post[categories][]', - options_for_select(@site.categories(lang: @lang), @post.categories), - { class: 'form-control select2', multiple: 'multiple', - data: { tags: true, - placeholder: t('posts.select.placeholder'), - 'allow-clear': true } } - .form-group - = label_tag 'post_tags', t('posts.tags') - = select_tag 'post[tags][]', - options_for_select(@site.tags(lang: @lang), @post.tags), - { class: 'form-control select2', multiple: 'multiple', - data: { tags: true, - placeholder: t('posts.select.placeholder'), - 'allow-clear': true } } - .form-group - = label_tag 'post_slug', t('posts.slug') - = text_field 'post', 'slug', value: @post.slug, - class: 'form-control' - %small.text-muted.form-text= t('posts.slug_help') - .form-group - = label_tag 'post_permalink', t('posts.permalink') - = text_field 'post', 'permalink', value: @post.get_front_matter('permalink'), - class: 'form-control' - %small.text-muted.form-text= t('posts.permalink_help') - .form-group - = label_tag 'post_layout', t('posts.layout') - = select_tag 'post[layout]', - options_for_select(@site.layouts, @post.get_front_matter('layout')), - { class: 'form-control select2' } - %small.text-muted.form-text= t('posts.layout_help') + - if @post.has_field? :date + .form-group + = label_tag 'post_date', t('posts.date') + = date_field 'post', 'date', value: @post.date.try(:strftime, '%F'), + class: 'form-control' + %small.text-muted.form-text= t('posts.date_help') + = render 'layouts/help', help: t('help.autocomplete_html') + - if @post.has_field? :categories + .form-group + = label_tag 'post_categories', t('posts.categories') + = select_tag 'post[categories][]', + options_for_select(@site.categories(lang: @lang), @post.categories), + { class: 'form-control select2', multiple: 'multiple', + data: { tags: true, + placeholder: t('posts.select.placeholder'), + 'allow-clear': true } } + - if @post.has_field? :tags + .form-group + = label_tag 'post_tags', t('posts.tags') + = select_tag 'post[tags][]', + options_for_select(@site.tags(lang: @lang), @post.tags), + { class: 'form-control select2', multiple: 'multiple', + data: { tags: true, + placeholder: t('posts.select.placeholder'), + 'allow-clear': true } } + - if @post.has_field? :slug + .form-group + = label_tag 'post_slug', t('posts.slug') + = text_field 'post', 'slug', value: @post.slug, + class: 'form-control' + %small.text-muted.form-text= t('posts.slug_help') + - if @post.has_field? :permalink + .form-group + = label_tag 'post_permalink', t('posts.permalink') + = text_field 'post', 'permalink', value: @post.get_front_matter('permalink'), + class: 'form-control' + %small.text-muted.form-text= t('posts.permalink_help') + - if @post.has_field? :layout + .form-group + = label_tag 'post_layout', t('posts.layout') + = select_tag 'post[layout]', + options_for_select(@site.layouts, @post.get_front_matter('layout')), + { class: 'form-control select2' } + %small.text-muted.form-text= t('posts.layout_help') - if @site.i18n? - @site.translations.each do |lang| - next if lang == @lang