5
0
Fork 0
mirror of https://0xacab.org/sutty/sutty synced 2024-07-03 23:55:46 +00:00

modificar articulos

This commit is contained in:
f 2018-02-02 19:20:31 -03:00
parent 851da77ce7
commit 807e27bbf6
No known key found for this signature in database
GPG key ID: F3FDAB97B5F9F7E7
6 changed files with 98 additions and 24 deletions

View file

@ -17,18 +17,27 @@ class PostsController < ApplicationController
def update def update
p = post_params p = post_params
@site = find_site
@post = find_post(@site)
# crear un array a partir de una cadena separada por comas # crear un array a partir de una cadena separada por comas
[:tags,:categories].each do |comma| [:tags,:categories].each do |comma|
p[comma] = p.dig(comma).split(',').map(&:strip) p[comma] = p.dig(comma).split(',').map(&:strip)
end end
binding.pry @post.update_attributes(p)
if @post.save
redirect_to site_post_path(@site, @post)
else
render 'posts/edit'
end
end end
private private
# Solo permitir cambiar estos atributos de cada articulo
def post_params def post_params
params.require(:post).permit(:title, :date, :tags, :categories, :content) params.require(:post).permit(:title, :date, :tags,
:categories, :content, :slug)
end end
end end

View file

@ -12,7 +12,10 @@ class Post
# Trabajar con posts. Si estamos creando uno nuevo, el **site** y # Trabajar con posts. Si estamos creando uno nuevo, el **site** y
# el **front_matter** son necesarios, sino, **site** y **post**. # el **front_matter** son necesarios, sino, **site** y **post**.
# XXX chequear que se den las condiciones # XXX chequear que se den las condiciones
def initialize(site:, post:, front_matter: {}) def initialize(site:, post: nil, front_matter: {})
raise ArgumentError, I18n.t('posts.errors.site') unless site.is_a?(Site)
raise ArgumentError, I18n.t('posts.errors.post') unless post.is_a?(Jekyll::Document)
@site = site @site = site
@post = post @post = post
# los errores tienen que ser un hash para que # los errores tienen que ser un hash para que
@ -20,7 +23,8 @@ class Post
@errors = {} @errors = {}
@front_matter = front_matter @front_matter = front_matter
new_post! unless @post # Crea un post nuevo si no especificamos el post
new_post unless @post
load_data! unless new? load_data! unless new?
end end
@ -61,7 +65,7 @@ class Post
alias :category :categories alias :category :categories
def path def path
@post.try :path || File.join(@site.path, '_posts', basename_from_front_matter) basename_changed? ? File.join(@site.path, '_posts', basename_from_front_matter) : @post.try(:path)
end end
def id def id
@ -79,23 +83,39 @@ class Post
end end
def content def content
@post.content @content ||= @post.content
end
# imita Model.update_attributes de ActiveRecord
def update_attributes(attrs)
attrs.each_pair do |k,i|
# el contenido no es metadata
if k == 'content'
@content = i
else
set_metadata k.to_sym, i
end
end
end end
private private
def new_post def new_post
opts = { site: @site, collection: @site.posts } opts = { site: @site, collection: @site.jekyll.posts }
@post = ::Jekyll::Document.new(path, opts) @post = Jekyll::Document.new(path, opts)
@site.posts.docs << @post @site.jekyll.posts.docs << @post
@site.posts.docs.sort! @site.jekyll.posts.docs.sort!
@post @post
end end
# Obtiene metadatos # Obtiene metadatos
def get_metadata(name) def get_metadata(name)
new? ? @front_matter.dig(name) : @post.data[name] @front_matter.key?(name) ? @front_matter.dig(name) : @post.data[name]
end
def set_metadata(name, value)
@front_matter[name] = value
end end
# Cambiar el nombre del archivo si cambió el título o la fecha. # Cambiar el nombre del archivo si cambió el título o la fecha.
@ -105,7 +125,7 @@ class Post
return true unless basename_changed? return true unless basename_changed?
if File.exist? path if File.exist? path
@errors << 'El archivo destino ya existe' add_error path: I18n.t('posts.errors.path')
return return
end end
@ -114,7 +134,7 @@ class Post
end end
def replace_post!(path) def replace_post!(path)
@site.posts.docs.delete @post @site.jekyll.posts.docs.delete @post
new_post new_post
end end
@ -122,18 +142,22 @@ class Post
# Obtiene el nombre del archivo a partir de los datos que le # Obtiene el nombre del archivo a partir de los datos que le
# pasemos # pasemos
def basename_from_front_matter def basename_from_front_matter
date = @front_matter[:date].strftime('%F') _date = @front_matter[:date]
title = ::Jekyll::Utils.slugify(@front_matter[:title]) date = _date.respond_to?(:strftime) ? _date.strftime('%F') : _date
ext = @post.try :ext || 'markdown' title = get_metadata 'slug' || Jekyll::Utils.slugify(@front_matter[:title])
ext = get_metadata 'ext' || '.markdown'
"#{date}-#{title}.#{ext}" "#{date}-#{title}#{ext}"
end end
# Toma los datos del front matter local y los mueve a los datos # Toma los datos del front matter local y los mueve a los datos
# que van a ir al post. Si hay símbolos se convierten a cadenas, # que van a ir al post. Si hay símbolos se convierten a cadenas,
# porque Jekyll trabaja con cadenas. # porque Jekyll trabaja con cadenas. Se excluyen otros datos que no
# van en el frontmatter
def merge_data_with_front_matter! def merge_data_with_front_matter!
@data.merge! Hash[@front_matter.map { |k, v| [k.to_s, v] }] @data.merge! Hash[@front_matter.map do |k, v|
[k.to_s, v] unless REJECT_FROM_DATA.include? k
end.compact]
end end
# Carga una copia de los datos del post original excluyendo datos # Carga una copia de los datos del post original excluyendo datos
@ -169,11 +193,23 @@ class Post
return true if r.zero? return true if r.zero?
@errors << 'No se pudo escribir el archivo' add_error file: I18n.t('posts.errors.file')
false false
end end
def full_content def full_content
"#{@data.to_yaml}---\n\n#{@content}" "#{@data.to_yaml}---\n\n#{@content}"
end end
def add_error(hash)
hash.each_pair do |k,i|
if @errors.key?(k)
@errors[k] = [@errors[k], i]
else
@errors[k] = i
end
end
@errors
end
end end

View file

@ -3,9 +3,11 @@
class Site class Site
attr_accessor :jekyll attr_accessor :jekyll
attr_reader :path
def initialize(jekyll:) def initialize(jekyll:, path: nil)
@jekyll = jekyll @jekyll = jekyll
@path = path || @jekyll.config['source']
end end
# Obtener el nombre del sitio # Obtener el nombre del sitio
@ -24,12 +26,16 @@ class Site
def posts def posts
return @posts if @posts return @posts if @posts
@jekyll.read if @jekyll.posts.docs.empty? if @jekyll.posts.docs.empty?
@jekyll.read
# Queremos saber cuantas veces releemos los articulos
Rails.logger.info 'Leyendo articulos'
end
# Los convertimos a una clase intermedia capaz de acceder a sus # Los convertimos a una clase intermedia capaz de acceder a sus
# datos y modificarlos # datos y modificarlos
@posts = @jekyll.posts.docs.map do |post| @posts = @jekyll.posts.docs.map do |post|
Post.new(site: @jekyll, post: post) Post.new(site: self, post: post)
end end
end end
@ -69,7 +75,7 @@ class Site
config[unneeded] = [] if config.key? unneeded config[unneeded] = [] if config.key? unneeded
end end
Site.new(jekyll: ::Jekyll::Site.new(config)) Site.new(jekyll: ::Jekyll::Site.new(config), path: j)
end end
end.compact end.compact
end end

View file

@ -1,3 +1,9 @@
- unless @post.errors.empty?
.alert.alert-danger
%ul
- @post.errors.each do |error|
%li= error
= form_tag site_post_path(@site, @post), method: :patch, class: 'form' do = form_tag site_post_path(@site, @post), method: :patch, class: 'form' do
.form-group .form-group
= submit_tag t('posts.save'), class: 'btn btn-success' = submit_tag t('posts.save'), class: 'btn btn-success'
@ -22,3 +28,8 @@
= text_field 'post', 'tags', value: @post.tags.join(', '), = text_field 'post', 'tags', value: @post.tags.join(', '),
class: 'form-control' class: 'form-control'
%small.text-muted.form-text= t('posts.tags_help') %small.text-muted.form-text= t('posts.tags_help')
.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')

View file

@ -17,3 +17,10 @@ en:
tags_help: 'Comma separated!' tags_help: 'Comma separated!'
categories: 'Categories' categories: 'Categories'
categories_help: 'Comma separated!' categories_help: 'Comma separated!'
slug: 'Slug'
slug_help: 'This is the name of the article on the URL, ie. /title/. You can leave it empty.'
errors:
site: 'Argument `site` must be of class Site'
post: 'Argument `post` must be of class Post'
path: 'File already exist'
file: "Couldn't write the file"

View file

@ -15,3 +15,8 @@ es:
title: 'Título' title: 'Título'
categories: 'Categorías' categories: 'Categorías'
categories_help: '¡Separadas por comas!' categories_help: '¡Separadas por comas!'
slug: 'Nombre la URL'
slug_help: 'Esto es el nombre del artículo en la URL, por ejemplo /título/. Puedes dejarlo vacío.'
errors:
path: 'El archivo destino ya existe'
file: 'No se pudo escribir el archivo'