diff --git a/app/lib/concerns/auto_publish_concern.rb b/app/lib/concerns/auto_publish_concern.rb new file mode 100644 index 00000000..19160f02 --- /dev/null +++ b/app/lib/concerns/auto_publish_concern.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + + +module AutoPublishConcern + extend ActiveSupport::Concern + + included do + def auto_publish! + DeployJob.perform_later(site) if site.auto_publish? + end + end +end diff --git a/app/services/post_service.rb b/app/services/post_service.rb index 4631a9a4..49471d7c 100644 --- a/app/services/post_service.rb +++ b/app/services/post_service.rb @@ -3,6 +3,8 @@ # Este servicio se encarga de crear artículos y guardarlos en git, # asignándoselos a une usuarie PostService = Struct.new(:site, :usuarie, :post, :params, keyword_init: true) do + include AutoPublishConcern + # Crea un artículo nuevo # # @return Post @@ -22,6 +24,7 @@ PostService = Struct.new(:site, :usuarie, :post, :params, keyword_init: true) do # Devolver el post aunque no se haya salvado para poder rescatar los # errores + auto_publish! post end @@ -35,6 +38,7 @@ PostService = Struct.new(:site, :usuarie, :post, :params, keyword_init: true) do params[:draft] = true commit(action: :created, add: [post.path.absolute]) if post.update(anon_post_params) + auto_publish! post end @@ -56,6 +60,7 @@ PostService = Struct.new(:site, :usuarie, :post, :params, keyword_init: true) do # Devolver el post aunque no se haya salvado para poder rescatar los # errores + auto_publish! post end diff --git a/app/services/site_service.rb b/app/services/site_service.rb index 36868c51..68b2a1b4 100644 --- a/app/services/site_service.rb +++ b/app/services/site_service.rb @@ -3,6 +3,8 @@ # Se encargar de guardar cambios en sitios # TODO: Implementar rollback en la configuración SiteService = Struct.new(:site, :usuarie, :params, keyword_init: true) do + include AutoPublishConcern + def deploy site.enqueue! DeployJob.perform_later site @@ -36,10 +38,14 @@ SiteService = Struct.new(:site, :usuarie, :params, keyword_init: true) do add_licencias && add_code_of_conduct && add_privacy_policy && - site.index_posts! && deploy end + if site.persisted? + site.index_posts! + auto_publish! + end + site end diff --git a/db/migrate/20230822165038_add_autopublish_to_sites.rb b/db/migrate/20230822165038_add_autopublish_to_sites.rb new file mode 100644 index 00000000..2f89e9d2 --- /dev/null +++ b/db/migrate/20230822165038_add_autopublish_to_sites.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +# Agrega posibilidad de autopublicación del sitio +class AddAutopublishToSites < ActiveRecord::Migration[6.1] + def change + add_column :sites, :auto_publish, :boolean, default: false + end +end