2017-10-05 19:42:32 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'sinatra/base'
|
|
|
|
require 'sinatra/namespace'
|
|
|
|
require 'sinatra/reloader' if ENV['RACK_ENV'] == 'development'
|
|
|
|
require 'sinatra_warden'
|
|
|
|
require_relative 'login'
|
|
|
|
require_relative '../sutty'
|
|
|
|
|
|
|
|
module Sutty
|
|
|
|
# El gestor de posts
|
|
|
|
class Post < Sinatra::Base
|
|
|
|
use Sutty::Login
|
|
|
|
register Sinatra::Warden
|
|
|
|
register Sinatra::Namespace
|
|
|
|
|
|
|
|
set :root, Sutty.root
|
|
|
|
|
|
|
|
configure :development do
|
|
|
|
register Sinatra::Reloader
|
|
|
|
end
|
|
|
|
|
|
|
|
namespace '/sites/:name/posts' do
|
|
|
|
before do
|
|
|
|
authorize! '/login' if ENV['RACK_ENV'] == 'production'
|
|
|
|
@site = Sutty.find(params['name'])
|
|
|
|
@site.read if @site.posts.docs.empty?
|
|
|
|
end
|
|
|
|
|
|
|
|
get do
|
|
|
|
haml :'posts/index'
|
|
|
|
end
|
|
|
|
|
|
|
|
namespace '/:basename' do
|
|
|
|
before do
|
|
|
|
@post = @site.posts.docs.select do |d|
|
|
|
|
d.basename == params['basename']
|
|
|
|
end.first
|
|
|
|
end
|
|
|
|
|
|
|
|
get do
|
|
|
|
haml :'posts/show'
|
|
|
|
end
|
|
|
|
|
|
|
|
get '/edit' do
|
|
|
|
haml :'posts/edit'
|
|
|
|
end
|
|
|
|
|
|
|
|
post do
|
2017-10-09 22:29:31 +00:00
|
|
|
front_matter = {
|
|
|
|
tags: params[:post][:tags].split(',').map(&:strip),
|
|
|
|
categories: params[:post][:categories].split(',').map(&:strip),
|
|
|
|
title: params[:post][:title],
|
2017-10-10 14:52:27 +00:00
|
|
|
date: Time.parse(params[:post][:date])
|
2017-10-09 22:29:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
post = Sutty::Models::Post.new(@site, @post, front_matter)
|
2017-10-05 19:42:32 +00:00
|
|
|
post.content = params[:post][:content]
|
|
|
|
|
|
|
|
if post.save
|
2017-10-10 14:52:27 +00:00
|
|
|
@post = post.post
|
2017-10-05 19:42:32 +00:00
|
|
|
flash[:success] = 'Artículo guardado con éxito'
|
2017-10-10 14:52:27 +00:00
|
|
|
# XXX por alguna razon el flash no sobrevive la redirección
|
|
|
|
haml :'posts/show'
|
2017-10-05 19:42:32 +00:00
|
|
|
else
|
2017-10-10 14:52:27 +00:00
|
|
|
flash[:warning] = post.errors
|
|
|
|
haml :'posts/edit'
|
2017-10-05 19:42:32 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|