sutty/app/services/site_service.rb

51 lines
1.4 KiB
Ruby
Raw Normal View History

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
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
site.update_attributes(params) &&
site.config.write &&
commit_config(action: :update)
end
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
end
# rubocop:enable Metrics/BlockLength