mirror of
https://0xacab.org/sutty/sutty
synced 2024-11-22 14:36:21 +00:00
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
|
||||
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)
|
||||
|
|
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'
|
||||
sites:
|
||||
static_file_migration: 'File migration'
|
||||
find_and_replace: 'Search and replace'
|
||||
index:
|
||||
title: 'Sites'
|
||||
pull: 'Upgrade'
|
||||
|
|
|
@ -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'
|
||||
|
|
Loading…
Reference in a new issue