buscar y reemplazar #151
This commit is contained in:
parent
e27fde6306
commit
ebbca76f5b
5 changed files with 53 additions and 1 deletions
4
app/models/git_author.rb
Normal file
4
app/models/git_author.rb
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
# Une autore de commits
|
||||||
|
GitAuthor = Struct.new :email, :name, keyword_init: true
|
|
@ -5,6 +5,7 @@
|
||||||
class Site < ApplicationRecord
|
class Site < ApplicationRecord
|
||||||
include FriendlyId
|
include FriendlyId
|
||||||
include Site::Forms
|
include Site::Forms
|
||||||
|
include Site::FindAndReplace
|
||||||
|
|
||||||
# TODO: Hacer que los diferentes tipos de deploy se auto registren
|
# TODO: Hacer que los diferentes tipos de deploy se auto registren
|
||||||
# @see app/services/site_service.rb
|
# @see app/services/site_service.rb
|
||||||
|
@ -52,7 +53,7 @@ class Site < ApplicationRecord
|
||||||
accepts_nested_attributes_for :deploys, allow_destroy: true
|
accepts_nested_attributes_for :deploys, allow_destroy: true
|
||||||
|
|
||||||
# El sitio en Jekyll
|
# El sitio en Jekyll
|
||||||
attr_accessor :jekyll
|
attr_reader :jekyll
|
||||||
|
|
||||||
# No permitir HTML en estos atributos
|
# No permitir HTML en estos atributos
|
||||||
def title=(title)
|
def title=(title)
|
||||||
|
|
45
app/models/site/find_and_replace.rb
Normal file
45
app/models/site/find_and_replace.rb
Normal file
|
@ -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
|
|
@ -256,6 +256,7 @@ en:
|
||||||
maximum: 'Maximum building time'
|
maximum: 'Maximum building time'
|
||||||
sites:
|
sites:
|
||||||
static_file_migration: 'File migration'
|
static_file_migration: 'File migration'
|
||||||
|
find_and_replace: 'Search and replace'
|
||||||
index:
|
index:
|
||||||
title: 'Sites'
|
title: 'Sites'
|
||||||
pull: 'Upgrade'
|
pull: 'Upgrade'
|
||||||
|
|
|
@ -261,6 +261,7 @@ es:
|
||||||
maximum: 'Tiempo máximo de generación'
|
maximum: 'Tiempo máximo de generación'
|
||||||
sites:
|
sites:
|
||||||
static_file_migration: 'Migración de archivos'
|
static_file_migration: 'Migración de archivos'
|
||||||
|
find_and_replace: 'Búsqueda y reemplazo'
|
||||||
index:
|
index:
|
||||||
title: 'Sitios'
|
title: 'Sitios'
|
||||||
pull: 'Actualizar'
|
pull: 'Actualizar'
|
||||||
|
|
Loading…
Reference in a new issue