sutty/app/models/site/static_file_migration.rb

53 lines
1.3 KiB
Ruby
Raw Normal View History

2019-11-14 17:27:24 +00:00
# frozen_string_literal: true
class Site
# Obtiene todos los archivos relacionados en artículos del sitio y los
# sube a Sutty.
2019-11-14 17:27:24 +00:00
class StaticFileMigration
# Tipos de metadatos que contienen archivos
STATIC_TYPES = %i[file image].freeze
2019-11-14 17:27:24 +00:00
attr_reader :site
def initialize(site:)
@site = site
end
def migrate!
modified = site.docs.map do |doc|
next unless STATIC_TYPES.map do |field|
next unless doc.attribute? field
next unless doc[field].path?
next unless doc[field].static_file
2019-11-14 17:27:24 +00:00
true
end.any?
2019-11-14 17:27:24 +00:00
log.write "#{doc.path.relative};no se pudo guardar\n" unless doc.save(validate: false)
2019-11-14 17:27:24 +00:00
doc.path.absolute
end.compact
2019-11-14 17:27:24 +00:00
log.close
return if modified.empty?
2019-11-14 17:27:24 +00:00
# TODO: Hacer la migración desde el servicio de creación de sitios?
site.repository.commit(file: modified,
message: I18n.t('sites.static_file_migration'),
usuarie: author)
end
private
def author
2020-06-16 22:10:54 +00:00
@author ||= GitAuthor.new email: "sutty@#{Site.domain}",
name: 'Sutty'
2019-11-14 17:27:24 +00:00
end
def log
@log ||= File.open(File.join(site.path, 'migration.csv'), 'w')
2019-11-14 17:27:24 +00:00
end
end
end