2019-08-09 19:49:25 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# Se encargar de guardar cambios en sitios
|
|
|
|
# TODO: Implementar rollback en la configuración
|
|
|
|
# rubocop:disable Metrics/BlockLength
|
|
|
|
SiteService = Struct.new(:site, :usuarie, :params, keyword_init: true) do
|
|
|
|
# Crea un sitio, agrega un rol nuevo y guarda los cambios a la
|
|
|
|
# configuración en el repositorio git
|
|
|
|
def create
|
|
|
|
self.site = Site.new params
|
|
|
|
|
|
|
|
add_role temporal: false, rol: 'usuarie'
|
|
|
|
|
|
|
|
I18n.with_locale(usuarie.try(:lang) || I18n.default_locale) do
|
|
|
|
site.save &&
|
|
|
|
site.config.write &&
|
|
|
|
commit_config(action: :create)
|
|
|
|
end
|
|
|
|
|
2019-09-17 21:27:51 +00:00
|
|
|
add_licencias
|
|
|
|
|
2019-08-09 19:49:25 +00:00
|
|
|
site
|
|
|
|
end
|
|
|
|
|
|
|
|
# Actualiza el sitio y guarda los cambios en la configuración
|
|
|
|
def update
|
|
|
|
I18n.with_locale(usuarie.try(:lang) || I18n.default_locale) do
|
2019-08-29 17:54:19 +00:00
|
|
|
site.update(params) &&
|
2019-08-09 19:49:25 +00:00
|
|
|
site.config.write &&
|
|
|
|
commit_config(action: :update)
|
|
|
|
end
|
|
|
|
|
2019-09-17 23:26:59 +00:00
|
|
|
change_licencias
|
|
|
|
|
2019-08-09 19:49:25 +00:00
|
|
|
site
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# Guarda los cambios de la configuración en el repositorio git
|
|
|
|
def commit_config(action:)
|
|
|
|
site.repository
|
|
|
|
.commit(usuarie: usuarie,
|
|
|
|
file: site.config.path,
|
|
|
|
message: I18n.t("site_service.#{action}",
|
|
|
|
name: site.name))
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_role(temporal: true, rol: 'invitade')
|
|
|
|
site.roles << Rol.new(site: site, usuarie: usuarie,
|
|
|
|
temporal: temporal, rol: rol)
|
|
|
|
end
|
2019-09-17 21:27:51 +00:00
|
|
|
|
|
|
|
# Crea la licencia del sitio para cada locale disponible
|
|
|
|
def add_licencias
|
|
|
|
I18n.available_locales.each do |locale|
|
|
|
|
Mobility.with_locale(locale) do
|
2019-09-17 23:26:59 +00:00
|
|
|
add_licencia lang: locale
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_licencia(lang:)
|
|
|
|
params = ActionController::Parameters.new(
|
|
|
|
post: {
|
|
|
|
lang: lang,
|
|
|
|
title: site.licencia.name,
|
|
|
|
author: %w[Sutty],
|
|
|
|
permalink: "#{I18n.t('activerecord.models.licencia').downcase}/",
|
|
|
|
content: CommonMarker.render_html(site.licencia.deed)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
PostService.new(site: site, usuarie: usuarie, params: params).create
|
|
|
|
end
|
|
|
|
|
|
|
|
# Encuentra la licencia a partir de su enlace permanente y le cambia
|
|
|
|
# el contenido
|
|
|
|
#
|
|
|
|
# TODO: Crear un layout específico para licencias así es más certera
|
|
|
|
# la búsqueda.
|
|
|
|
def change_licencias
|
|
|
|
I18n.available_locales.each do |locale|
|
|
|
|
Mobility.with_locale(locale) do
|
|
|
|
permalink = "#{I18n.t('activerecord.models.licencia').downcase}/"
|
|
|
|
post = site.posts(lang: locale).find_by(permalink: permalink)
|
|
|
|
|
|
|
|
post ? change_licencia(post: post) : add_licencia(lang: locale)
|
2019-09-17 21:27:51 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-09-17 23:26:59 +00:00
|
|
|
|
|
|
|
def change_licencia(post:)
|
|
|
|
params = ActionController::Parameters.new(
|
|
|
|
post: {
|
|
|
|
title: site.licencia.name,
|
|
|
|
content: CommonMarker.render_html(site.licencia.deed)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
PostService.new(site: site, usuarie: usuarie, post: post,
|
|
|
|
params: params).update
|
|
|
|
end
|
2019-08-09 19:49:25 +00:00
|
|
|
end
|
|
|
|
# rubocop:enable Metrics/BlockLength
|