mirror of
https://0xacab.org/sutty/sutty
synced 2024-11-22 17:46:22 +00:00
73 lines
1.8 KiB
Ruby
73 lines
1.8 KiB
Ruby
# 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
|
|
front_matter = {
|
|
tags: params[:post][:tags].split(',').map(&:strip),
|
|
categories: params[:post][:categories].split(',').map(&:strip),
|
|
title: params[:post][:title],
|
|
date: Time.parse(params[:post][:date])
|
|
}
|
|
|
|
post = Sutty::Models::Post.new(@site, @post, front_matter)
|
|
post.content = params[:post][:content]
|
|
|
|
if post.save
|
|
@post = post.post
|
|
flash[:success] = 'Artículo guardado con éxito'
|
|
# XXX por alguna razon el flash no sobrevive la redirección
|
|
haml :'posts/show'
|
|
else
|
|
flash[:warning] = post.errors
|
|
haml :'posts/edit'
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|