5
0
Fork 0
mirror of https://0xacab.org/sutty/sutty synced 2024-11-24 15:56:20 +00:00
panel/app/models/site/find_and_replace.rb
f 6ff0a36b44 BREAKING CHANGE: Site::Repository#commit
poder añadir y eliminar archivos en el mismo commit
2023-07-03 13:23:54 -03:00

45 lines
1.2 KiB
Ruby

# 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(add: modified,
message: I18n.t('sites.find_and_replace'),
usuarie: author)
end
end
end