From 8c6af96d2f8bc5efac27c43960661a6583fdb16d Mon Sep 17 00:00:00 2001 From: f Date: Thu, 21 Jun 2018 15:15:03 -0300 Subject: [PATCH] generar los parametros correctos --- app/controllers/posts_controller.rb | 6 +----- app/models/post.rb | 11 ++++++++--- app/models/post/template_field.rb | 2 +- app/views/posts/template_field/_table.haml | 2 +- 4 files changed, 11 insertions(+), 10 deletions(-) diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 5ffd710b..c26d3952 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -68,10 +68,6 @@ class PostsController < ApplicationController # Solo permitir cambiar estos atributos de cada articulo def post_params - default_post_params = [:title, :date, :content, :slug, :cover, - :layout, :permalink, :dir, - { lang: {} }, { tags: [] }, { categories: [] }] - - params.require(:post).permit(default_post_params + @post.template_params) + params.require(:post).permit(@post.template_params) end end diff --git a/app/models/post.rb b/app/models/post.rb index 12532259..c5b00cb8 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -23,7 +23,9 @@ class Post 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 - + DEFAULT_PARAMS = [:title, :date, :content, :slug, :cover, + :layout, :permalink, :dir, + { lang: {} }, { tags: [] }, { categories: [] }] # Trabajar con posts. Si estamos creando uno nuevo, el **site** y # el **front_matter** son necesarios, sino, **site** y **post**. @@ -230,10 +232,13 @@ class Post end.compact end - # devuelve las plantillas como strong params + # devuelve las plantillas como strong params, primero los valores + # simples, luego los arrays y al final los hashes def template_params - @template_params ||= template_fields.map do |k| + @template_params ||= (DEFAULT_PARAMS + template_fields.map do |k| k.to_param + end).sort_by do |s| + s.is_a?(Symbol) ? 0 : 1 end end diff --git a/app/models/post/template_field.rb b/app/models/post/template_field.rb index 254e4e16..1090646c 100644 --- a/app/models/post/template_field.rb +++ b/app/models/post/template_field.rb @@ -98,7 +98,7 @@ class Post def to_param if nested? { key.to_sym => {} } - elsif array? + elsif array? && multiple? { key.to_sym => [] } else key.to_sym diff --git a/app/views/posts/template_field/_table.haml b/app/views/posts/template_field/_table.haml index e192236d..5937d86b 100644 --- a/app/views/posts/template_field/_table.haml +++ b/app/views/posts/template_field/_table.haml @@ -17,7 +17,7 @@ -# Si el template es un array, quiere decir que esperamos un array de hashes. -# El valor vacĂ­o luego es reemplazado por un id temporal - if template.array? - - name = [template.key, '', nf.key] + - name = [template.key, '0', nf.key] - else - name = [template.key, nf.key] - if @post.new?