2019-03-26 15:32:20 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-01-29 22:19:10 +00:00
|
|
|
# Controlador de sitios
|
2018-01-29 18:09:30 +00:00
|
|
|
class SitesController < ApplicationController
|
2018-09-28 15:27:25 +00:00
|
|
|
include Pundit
|
2019-07-03 23:25:23 +00:00
|
|
|
before_action :authenticate_usuarie!
|
2018-01-29 18:09:30 +00:00
|
|
|
|
2018-01-29 22:19:10 +00:00
|
|
|
# Ver un listado de sitios
|
2018-01-29 18:09:30 +00:00
|
|
|
def index
|
2018-09-28 15:27:25 +00:00
|
|
|
authorize Site
|
2019-07-08 16:40:41 +00:00
|
|
|
@sites = current_usuarie.sites
|
2018-01-29 22:19:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# No tenemos propiedades de un sitio aún, así que vamos al listado de
|
|
|
|
# artículos
|
|
|
|
def show
|
|
|
|
site = find_site
|
2019-07-12 23:40:44 +00:00
|
|
|
authorize site
|
2018-01-29 22:19:10 +00:00
|
|
|
|
|
|
|
redirect_to site_posts_path(site)
|
2018-01-29 18:09:30 +00:00
|
|
|
end
|
2018-02-20 17:47:11 +00:00
|
|
|
|
2019-07-12 23:40:44 +00:00
|
|
|
def new
|
|
|
|
@site = Site.new
|
|
|
|
authorize @site
|
2019-07-25 18:51:32 +00:00
|
|
|
|
|
|
|
@site.deploys.build type: 'DeployLocal'
|
|
|
|
@site.deploys.build type: 'DeployZip'
|
2019-07-12 23:40:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
|
|
|
@site = Site.new(site_params)
|
2019-07-31 20:55:34 +00:00
|
|
|
@site.roles << Rol.new(site: @site,
|
|
|
|
usuarie: current_usuarie,
|
|
|
|
temporal: false,
|
|
|
|
rol: 'usuarie')
|
|
|
|
|
|
|
|
# XXX: Necesitamos escribir la configuración después porque queremos
|
|
|
|
# registrar quién hizo los cambios en el repositorio
|
|
|
|
if @site.save && @site.config.write(current_usuarie)
|
2019-07-12 23:40:44 +00:00
|
|
|
redirect_to site_path(@site)
|
|
|
|
else
|
|
|
|
render 'new'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-07-13 00:20:36 +00:00
|
|
|
def edit
|
|
|
|
@site = find_site
|
|
|
|
authorize @site
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
@site = find_site
|
|
|
|
authorize @site
|
|
|
|
|
2019-07-31 20:55:34 +00:00
|
|
|
# XXX: Necesitamos escribir la configuración después porque queremos
|
|
|
|
# registrar quién hizo los cambios en el repositorio
|
|
|
|
if @site.update(site_params) && @site.config.write(current_usuarie)
|
2019-07-13 00:20:36 +00:00
|
|
|
redirect_to sites_path
|
|
|
|
else
|
|
|
|
render 'edit'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-02 22:07:06 +00:00
|
|
|
# Envía un archivo del directorio público de Jekyll
|
|
|
|
def send_public_file
|
2018-09-28 15:27:25 +00:00
|
|
|
authorize Site
|
2018-07-02 22:07:06 +00:00
|
|
|
@site = find_site
|
|
|
|
file = [params[:basename], params[:format]].join('.')
|
2019-07-03 23:25:23 +00:00
|
|
|
path = File.join(@site.path, 'public', params[:type], file)
|
|
|
|
path = Pathname.new path
|
2018-07-02 22:07:06 +00:00
|
|
|
|
2019-03-26 15:32:20 +00:00
|
|
|
# TODO: Verificar que no nos estén sacando archivos del sistema, como
|
2018-07-02 22:07:06 +00:00
|
|
|
# /etc/passwd
|
|
|
|
#
|
|
|
|
# En sí el matcher de la ruta no admite .. así que estaríamos semi
|
|
|
|
# cubiertas.
|
|
|
|
|
|
|
|
if path.exist?
|
|
|
|
send_file path, 'x-sendfile': true
|
|
|
|
else
|
|
|
|
render status: 404
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-02-20 17:47:11 +00:00
|
|
|
def enqueue
|
2019-07-26 01:11:01 +00:00
|
|
|
site = find_site
|
|
|
|
authorize site
|
2018-02-20 17:47:11 +00:00
|
|
|
|
2019-07-26 01:11:01 +00:00
|
|
|
# XXX: Convertir en una máquina de estados?
|
|
|
|
DeployWorker.perform_async site.id if site.enqueue!
|
2018-02-20 17:47:11 +00:00
|
|
|
|
2019-07-26 01:11:01 +00:00
|
|
|
redirect_to sites_path
|
2018-02-20 17:47:11 +00:00
|
|
|
end
|
2018-04-30 18:51:39 +00:00
|
|
|
|
|
|
|
def reorder_posts
|
|
|
|
@site = find_site
|
2018-09-28 15:27:25 +00:00
|
|
|
authorize @site
|
2018-04-30 18:51:39 +00:00
|
|
|
lang = params.require(:posts).require(:lang)
|
|
|
|
|
2019-07-03 23:25:23 +00:00
|
|
|
if params[:posts][:force].present?
|
|
|
|
result = @site.reorder_collection! lang
|
|
|
|
else
|
|
|
|
result = @site
|
|
|
|
.reorder_collection(lang, params.require(:posts).require(:order))
|
|
|
|
end
|
2018-05-02 17:23:45 +00:00
|
|
|
|
|
|
|
if result
|
2018-04-30 18:51:39 +00:00
|
|
|
flash[:info] = I18n.t('info.posts.reorder')
|
|
|
|
else
|
|
|
|
flash[:danger] = I18n.t('errors.posts.reorder')
|
|
|
|
end
|
|
|
|
|
|
|
|
redirect_to site_posts_path @site
|
|
|
|
end
|
2019-07-12 23:40:44 +00:00
|
|
|
|
2019-07-16 19:47:44 +00:00
|
|
|
def fetch
|
|
|
|
@site = find_site
|
|
|
|
authorize @site
|
|
|
|
|
|
|
|
@commits = @site.repository.commits
|
|
|
|
end
|
|
|
|
|
|
|
|
def merge
|
|
|
|
@site = find_site
|
|
|
|
authorize @site
|
|
|
|
|
|
|
|
if @site.repository.merge(current_usuarie)
|
|
|
|
flash[:success] = I18n.t('sites.fetch.merge.success')
|
|
|
|
else
|
|
|
|
flash[:error] = I18n.t('sites.fetch.merge.error')
|
|
|
|
end
|
|
|
|
|
|
|
|
redirect_to sites_path
|
|
|
|
end
|
|
|
|
|
2019-07-12 23:40:44 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def site_params
|
2019-07-25 18:51:32 +00:00
|
|
|
params.require(:site)
|
2019-07-31 20:55:34 +00:00
|
|
|
.permit(:name, :design_id, :licencia_id, :description, :title,
|
|
|
|
deploys_attributes: %i[type id _destroy])
|
2019-07-12 23:40:44 +00:00
|
|
|
end
|
2018-01-29 18:09:30 +00:00
|
|
|
end
|