From c08cfb16376ba50acb8769d9b9b88cb199b03045 Mon Sep 17 00:00:00 2001 From: f Date: Sat, 7 Aug 2021 18:26:52 -0300 Subject: [PATCH] Actualizar el hostname si cambia el name --- app/models/deploy_local.rb | 6 +++++- app/models/site.rb | 9 ++++++--- test/models/site_test.rb | 4 ++-- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/app/models/deploy_local.rb b/app/models/deploy_local.rb index 3bfc7d9..be8de36 100644 --- a/app/models/deploy_local.rb +++ b/app/models/deploy_local.rb @@ -3,7 +3,7 @@ # Alojamiento local, genera el sitio como si corriéramos `jekyll build`. class DeployLocal < Deploy # Asegurarse que el hostname es el permitido. - before_save :default_hostname! + before_save :reset_hostname!, :default_hostname! # Realizamos la construcción del sitio usando Jekyll y un entorno # limpio para no pasarle secretos @@ -46,6 +46,10 @@ class DeployLocal < Deploy private + def reset_hostname! + self.hostname = nil + end + # Crea el directorio destino si no existe. def mkdir FileUtils.mkdir_p destination diff --git a/app/models/site.rb b/app/models/site.rb index 2fc1344..c935765 100644 --- a/app/models/site.rb +++ b/app/models/site.rb @@ -58,7 +58,7 @@ class Site < ApplicationRecord after_initialize :load_jekyll after_create :load_jekyll, :static_file_migration! # Cambiar el nombre del directorio - before_update :update_name! + before_update :update_name!, :update_deploy_local_hostname!, if: :name_changed? before_save :add_private_key_if_missing! # Guardar la configuración si hubo cambios after_save :sync_attributes_with_config! @@ -450,8 +450,6 @@ class Site < ApplicationRecord end def update_name! - return unless name_changed? - FileUtils.mv path_was, path reload_jekyll! end @@ -469,6 +467,11 @@ class Site < ApplicationRecord config.hostname = hostname end + # Si cambia el nombre queremos actualizarlo en el DeployLocal + def update_deploy_local_hostname! + deploy_local&.update hostname: name + end + # Migra los archivos a Sutty def static_file_migration! Site::StaticFileMigration.new(site: self).migrate! diff --git a/test/models/site_test.rb b/test/models/site_test.rb index e53fff0..481c9bb 100644 --- a/test/models/site_test.rb +++ b/test/models/site_test.rb @@ -93,9 +93,9 @@ class SiteTest < ActiveSupport::TestCase test 'tienen un hostname que puede cambiar' do assert_equal "#{site.name}.#{Site.domain}", site.hostname - site.name = name = SecureRandom.hex + site.update(name: (new_name = SecureRandom.hex)) - assert_equal "#{name}.#{Site.domain}", site.hostname + assert_equal "#{new_name}.#{Site.domain}", site.hostname end test 'se pueden traer los datos de una plantilla' do