5
0
Fork 0
mirror of https://0xacab.org/sutty/sutty synced 2024-07-03 12:56:07 +00:00
panel/app/controllers/sites_controller.rb

122 lines
2.5 KiB
Ruby

# frozen_string_literal: true
# Controlador de sitios
class SitesController < ApplicationController
include Pundit
rescue_from Pundit::NilPolicyError, with: :page_not_found
before_action :authenticate_usuarie!
# Ver un listado de sitios
def index
authorize Site
@sites = current_usuarie.sites.order(:title)
fresh_when @sites
end
# No tenemos propiedades de un sitio aún, así que vamos al listado de
# artículos
def show
authorize site
redirect_to site_posts_path(site)
end
def new
@site = Site.new
authorize @site
@site.deploys.build type: 'DeployLocal'
end
def create
service = SiteService.new(usuarie: current_usuarie,
params: site_params)
if (@site = service.create).persisted?
redirect_to site_posts_path(@site)
else
render 'new'
end
end
def edit
authorize site
SiteService.new(site: site).build_deploys
end
def update
authorize site
service = SiteService.new(site: site, params: site_params,
usuarie: current_usuarie)
if service.update.valid?
redirect_to site_posts_path(site)
else
render 'edit'
end
end
def enqueue
authorize site
# XXX: Convertir en una máquina de estados?
DeployJob.perform_async site.id if site.enqueue!
redirect_to site_posts_path(site)
end
def reorder_posts
authorize site
lang = params.require(:posts).require(:lang)
result = if params[:posts][:force].present?
site.reorder_collection! lang
else
site
.reorder_collection(lang, params.require(:posts).require(:order))
end
if result
flash[:info] = I18n.t('info.posts.reorder')
else
flash[:danger] = I18n.t('errors.posts.reorder')
end
redirect_to site_posts_path(site)
end
def fetch
authorize site
@commits = site.repository.commits
end
def merge
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
private
def site
@site ||= find_site
end
def site_params
params.require(:site)
.permit(:name, :design_id, :licencia_id, :description, :title,
:colaboracion_anonima, :contact, :acepta_invitades,
deploys_attributes: %i[type id _destroy])
end
end