5
0
Fork 0
mirror of https://0xacab.org/sutty/sutty synced 2024-10-01 22:46:59 +00:00
panel/lib/sutty/post.rb

65 lines
1.4 KiB
Ruby
Raw Normal View History

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
post = Sutty::Models::Post.new(@site, @post)
post.content = params[:post][:content]
if post.save
flash[:success] = 'Artículo guardado con éxito'
redirect to("/sites/#{@site.name}/posts/#{@post.basename}")
else
flash[:error] = 'Hubo un error al guardar el artículo'
redirect back
end
end
end
end
end
end