diff --git a/app/models/git_author.rb b/app/models/git_author.rb new file mode 100644 index 0000000..fe7291a --- /dev/null +++ b/app/models/git_author.rb @@ -0,0 +1,4 @@ +# frozen_string_literal: true + +# Une autore de commits +GitAuthor = Struct.new :email, :name, keyword_init: true diff --git a/app/models/site.rb b/app/models/site.rb index b9b134c..3a0c13e 100644 --- a/app/models/site.rb +++ b/app/models/site.rb @@ -5,6 +5,7 @@ class Site < ApplicationRecord include FriendlyId include Site::Forms + include Site::FindAndReplace # TODO: Hacer que los diferentes tipos de deploy se auto registren # @see app/services/site_service.rb @@ -52,7 +53,7 @@ class Site < ApplicationRecord accepts_nested_attributes_for :deploys, allow_destroy: true # El sitio en Jekyll - attr_accessor :jekyll + attr_reader :jekyll # No permitir HTML en estos atributos def title=(title) diff --git a/app/models/site/find_and_replace.rb b/app/models/site/find_and_replace.rb new file mode 100644 index 0000000..2670159 --- /dev/null +++ b/app/models/site/find_and_replace.rb @@ -0,0 +1,45 @@ +# frozen_string_literal: true + +class Site + # Busca y reemplaza metadatos + module FindAndReplace + # Realiza la búsqueda y reemplazo. + # + # @param [String,Symbol] :field El campo donde buscar + # @param [Any] :search El valor a buscar + # @param [Any] :replace El valor de reemplazo + def find_and_replace(field:, search:, replace:) + modified = [] + field = field.to_sym + + docs.each do |doc| + next unless doc.attribute? field + + case doc[field].value + when Array + doc[field].value.map! do |x| + x == search ? replace : x + end + when Hash + doc[field].value.transform_values! do |x| + x == search ? replace : x + end + when NilClass + # nothing + else + doc[field].value = replace if doc[field].value == search + end + + modified << doc.path.absolute if doc.save(validate: false) + end + + return if modified.empty? + + author = GitAuthor.new email: "sutty@#{Site.domain}", name: 'Sutty' + + repository.commit(file: modified, + message: I18n.t('sites.find_and_replace'), + usuarie: author) + end + end +end diff --git a/config/locales/en.yml b/config/locales/en.yml index edc2b3d..aae7c86 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -256,6 +256,7 @@ en: maximum: 'Maximum building time' sites: static_file_migration: 'File migration' + find_and_replace: 'Search and replace' index: title: 'Sites' pull: 'Upgrade' diff --git a/config/locales/es.yml b/config/locales/es.yml index e9d1345..0223ef0 100644 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -261,6 +261,7 @@ es: maximum: 'Tiempo máximo de generación' sites: static_file_migration: 'Migración de archivos' + find_and_replace: 'Búsqueda y reemplazo' index: title: 'Sitios' pull: 'Actualizar'