WIP: crear un articulo a traves de una plantilla

This commit is contained in:
f 2018-05-08 18:57:11 -03:00
parent cb87b049fb
commit cf93ba743d
No known key found for this signature in database
GPG key ID: F3FDAB97B5F9F7E7
7 changed files with 40 additions and 6 deletions

View file

@ -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

View file

@ -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

View file

@ -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] }

View file

@ -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

View file

@ -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'

View file

@ -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'

View file

@ -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'