From cf93ba743df8e23ee71a8b323254146720005b80 Mon Sep 17 00:00:00 2001 From: f Date: Tue, 8 May 2018 18:57:11 -0300 Subject: [PATCH] WIP: crear un articulo a traves de una plantilla --- app/controllers/application_controller.rb | 2 +- app/controllers/posts_controller.rb | 6 +++++- app/models/post.rb | 16 ++++++++++++---- app/models/site.rb | 5 +++++ app/views/posts/index.haml | 11 +++++++++++ config/locales/en.yml | 5 +++++ config/routes.rb | 1 + 7 files changed, 40 insertions(+), 6 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 0aca486c..28b009af 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -39,7 +39,7 @@ class ApplicationController < ActionController::Base end def find_template(site) - id = params[:template_id] || params[:id] + id = params[:template_id] || params[:template] site.templates.find do |t| t.id == id end diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 610e6565..7787bab7 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -23,7 +23,11 @@ class PostsController < ApplicationController def new @site = find_site @lang = find_lang(@site) - @post = Post.new(site: @site, front_matter: { date: Time.now }, lang: @lang) + @template = find_template(@site) + @post = Post.new(site: @site, + front_matter: { date: Time.now }, + lang: @lang, + template: @template) end def create diff --git a/app/models/post.rb b/app/models/post.rb index 55a6f37d..b32e0341 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -15,7 +15,7 @@ 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 + attr_reader :post, :site, :errors, :old_post, :lang, :template REJECT_FROM_DATA = %w[excerpt].freeze # datos que no tienen que terminar en el front matter @@ -24,7 +24,7 @@ class Post # Trabajar con posts. Si estamos creando uno nuevo, el **site** y # el **front_matter** son necesarios, sino, **site** y **post**. # XXX chequear que se den las condiciones - def initialize(site:, post: nil, front_matter: {}, lang: nil) + def initialize(site:, post: nil, front_matter: {}, lang: nil, template: nil) unless site.is_a?(Site) raise ArgumentError, I18n.t('errors.argument_error', argument: :site, class: Site) @@ -38,6 +38,7 @@ class Post @site = site @post = post + @template = template # los errores tienen que ser un hash para que # ActiveModel pueda traer los errores normalmente @errors = {} @@ -52,7 +53,7 @@ class Post # sincronizar los datos del document if new? - @front_matter = {} + @front_matter = front_matter_from_template update_attributes front_matter else load_front_matter! @@ -175,7 +176,7 @@ class Post # enviamos actualizaciones al front matter, debería traer el contenido # del post sin cambios def content - @content ||= @post.try :content + @content ||= @post.try(:content) || @template.try(:content) end # imita Model.update_attributes de ActiveRecord @@ -211,6 +212,13 @@ class Post private + # Completa el front_matter a partir de las variables de otro + def front_matter_from_template + return {} unless @template + + @template.front_matter + end + # Genera un post nuevo y lo agrega a la colección del sitio. def new_post opts = { site: @site.jekyll, collection: @site.jekyll.collections[@collection] } diff --git a/app/models/site.rb b/app/models/site.rb index 71a82598..a58d776f 100644 --- a/app/models/site.rb +++ b/app/models/site.rb @@ -101,6 +101,11 @@ class Site @posts ||= posts_for('posts') end + # Obtiene todas las plantillas de artículos + def templates + @templates ||= posts_for('templates') + end + # Obtiene todos los posts de una colección determinada # # Devuelve nil si la colección no existe diff --git a/app/views/posts/index.haml b/app/views/posts/index.haml index 6c99649e..5b88d24d 100644 --- a/app/views/posts/index.haml +++ b/app/views/posts/index.haml @@ -12,6 +12,17 @@ .btn-group = link_to t('posts.new'), new_site_post_path(@site, lang: @lang), class: 'btn btn-success' + %button.btn.btn-success.dropdown-toggle.dropdown-toggle-split{data: { toggle: 'split' }, + aria: { haspopup: 'true', expanded: 'false' }} + %span.sr-only= t('posts.dropdown') + .dropdown-menu + - @site.templates.each do |template| + = link_to template.id.camelize, + new_site_post_path(@site, lang: @lang, template: template.id), + class: 'dropdown-item' + .dropdown-divider + = link_to t('templates.index'), site_templates_path(@site), + class: 'dropdown-item' - @site.translations.each do |l| = link_to t("i18n.#{l}"), site_posts_path(@site, category: @category, lang: l), class: 'btn btn-info' diff --git a/config/locales/en.yml b/config/locales/en.yml index ac7f40d3..232f4fb8 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -122,6 +122,10 @@ en: build_log: 'Read log' footer: powered_by: 'is developed by' + templates: + index: 'Templates' + edit: 'Edit' + save: 'Save' i18n: index: 'Translations' edit: 'Edit texts and translations' @@ -145,6 +149,7 @@ en: date: 'date' order: 'Order' new: 'New post' + dropdown: 'Toggle dropdown' index: 'Posts' edit: 'Edit' save: 'Save' diff --git a/config/routes.rb b/config/routes.rb index 0d74a191..1924dd7f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -9,6 +9,7 @@ Rails.application.routes.draw do resources :sites, only: [ :index, :show ] do resources :posts + resources :templates get 'i18n', to: 'i18n#index' get 'i18n/edit', to: 'i18n#edit'