From ebbca76f5bbc15fd84d1f19b006ffe581a20a612 Mon Sep 17 00:00:00 2001 From: f Date: Tue, 9 Jun 2020 11:22:13 -0300 Subject: [PATCH] buscar y reemplazar #151 --- app/models/git_author.rb | 4 +++ app/models/site.rb | 3 +- app/models/site/find_and_replace.rb | 45 +++++++++++++++++++++++++++++ config/locales/en.yml | 1 + config/locales/es.yml | 1 + 5 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 app/models/git_author.rb create mode 100644 app/models/site/find_and_replace.rb diff --git a/app/models/git_author.rb b/app/models/git_author.rb new file mode 100644 index 00000000..fe7291ae --- /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 b9b134cc..3a0c13e5 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 00000000..2670159d --- /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 edc2b3db..aae7c861 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 e9d1345a..0223ef04 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'