From 3cd483bb7934b314259540358d2f46888428cfb9 Mon Sep 17 00:00:00 2001 From: f Date: Fri, 2 Feb 2024 12:41:44 -0300 Subject: [PATCH 1/8] feat: poder obtener la ubicacion a partir de la key --- .../active_storage/service/jekyll_service.rb | 18 +++++++++++++++++- app/models/metadata_file.rb | 2 +- app/models/site/index.rb | 4 ++++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/app/lib/active_storage/service/jekyll_service.rb b/app/lib/active_storage/service/jekyll_service.rb index b33ccf4d..584bf06c 100644 --- a/app/lib/active_storage/service/jekyll_service.rb +++ b/app/lib/active_storage/service/jekyll_service.rb @@ -15,6 +15,14 @@ module ActiveStorage end end + # Obtiene Blob#key desde la ubicación, la inversa de path_for + # + # @param :path [String,Pathname] + # @return [String] + def key_from_path(path) + Pathname.new(path).dirname.basename.to_s + end + # Solo copiamos el archivo si no existe # # @param :key [String] @@ -87,7 +95,15 @@ module ActiveStorage # @param :key [String] # @return [String] def path_for(key) - File.join root, folder_for(key), filename_for(key) + File.join absolute_path_for(key), filename_for(key) + end + + # La ruta absoluta del directorio donde se almacena el archivo + # + # @param :key [String] + # @return [String] + def absolute_path_for(key) + File.join root, folder_for(key) end # @return [Site] diff --git a/app/models/metadata_file.rb b/app/models/metadata_file.rb index 3ac89c9b..e5b2f1ce 100644 --- a/app/models/metadata_file.rb +++ b/app/models/metadata_file.rb @@ -88,7 +88,7 @@ class MetadataFile < MetadataTemplate # # @return [String] def key_from_path - @key_from_path ||= pathname.dirname.basename.to_s + @key_from_path ||= site.blob_service.key_from_path(path: pathname) end def path? diff --git a/app/models/site/index.rb b/app/models/site/index.rb index cfa4030a..b691a855 100644 --- a/app/models/site/index.rb +++ b/app/models/site/index.rb @@ -14,6 +14,10 @@ class Site DELETED_STATUSES = %i[deleted].freeze LOCALE_FROM_PATH = /\A_/.freeze + def blob_service + @blob_service ||= ActiveStorage::Service::JekyllService.build_for_site(site: self) + end + def index_posts! Site.transaction do docs.each(&:index!) From 8e75a188c80c11ca579abdc9f620ecebe3b8982b Mon Sep 17 00:00:00 2001 From: f Date: Fri, 2 Feb 2024 12:41:59 -0300 Subject: [PATCH 2/8] feat: eliminar el archivo si se elimina el blob --- app/lib/active_storage/service/jekyll_service.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/app/lib/active_storage/service/jekyll_service.rb b/app/lib/active_storage/service/jekyll_service.rb index 584bf06c..8ad46032 100644 --- a/app/lib/active_storage/service/jekyll_service.rb +++ b/app/lib/active_storage/service/jekyll_service.rb @@ -23,6 +23,14 @@ module ActiveStorage Pathname.new(path).dirname.basename.to_s end + # Elimina el archivo + # + # @param :key [String] + # @return [nil] + def delete(key) + FileUtils.rm_f(path_for(key)) + end + # Solo copiamos el archivo si no existe # # @param :key [String] From 003d8fa303fa4d30c921ff520663521ad6ce148e Mon Sep 17 00:00:00 2001 From: f Date: Fri, 2 Feb 2024 12:43:30 -0300 Subject: [PATCH 3/8] fix: incluir archivos renombrados aunque en las pruebas cuando es renombrado siempre viene como deleted y luego added --- app/models/site/index.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/site/index.rb b/app/models/site/index.rb index b691a855..9844c822 100644 --- a/app/models/site/index.rb +++ b/app/models/site/index.rb @@ -10,7 +10,7 @@ class Site included do has_many :indexed_posts, dependent: :destroy - MODIFIED_STATUSES = %i[added modified].freeze + MODIFIED_STATUSES = %i[added modified renamed].freeze DELETED_STATUSES = %i[deleted].freeze LOCALE_FROM_PATH = /\A_/.freeze From 4872ddd227044d48efa0a2a8127ac3cbc14cdf92 Mon Sep 17 00:00:00 2001 From: f Date: Fri, 2 Feb 2024 12:44:38 -0300 Subject: [PATCH 4/8] refactor: no es necesario buscar en un array --- app/models/site/index.rb | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/app/models/site/index.rb b/app/models/site/index.rb index 9844c822..71c442c1 100644 --- a/app/models/site/index.rb +++ b/app/models/site/index.rb @@ -11,7 +11,6 @@ class Site has_many :indexed_posts, dependent: :destroy MODIFIED_STATUSES = %i[added modified renamed].freeze - DELETED_STATUSES = %i[deleted].freeze LOCALE_FROM_PATH = /\A_/.freeze def blob_service @@ -71,7 +70,7 @@ class Site # Obtiene todos los archivos a reindexar # - # @return [Array] + # @return [Array] def indexable_posts @indexable_posts ||= diff_with_head.each_delta.select do |delta| @@ -84,9 +83,7 @@ class Site # Elimina los artículos eliminados o que cambiaron de ubicación # del índice def remove_deleted_posts! - indexable_posts.select do |delta| - DELETED_STATUSES.include? delta.status - end.each do |delta| + indexable_posts.select(&:deleted?).each do |delta| locale, path = locale_and_path_from(delta.old_file[:path]) indexed_posts.destroy_by(locale: locale, path: path).tap do |destroyed_posts| From 32d68ee19469c236a53a78128e131d08ffdbebc2 Mon Sep 17 00:00:00 2001 From: f Date: Fri, 2 Feb 2024 12:45:07 -0300 Subject: [PATCH 5/8] feat: reindexar los archivos #15093 --- app/models/site/index.rb | 49 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/app/models/site/index.rb b/app/models/site/index.rb index 71c442c1..d66c5272 100644 --- a/app/models/site/index.rb +++ b/app/models/site/index.rb @@ -80,6 +80,16 @@ class Site end end + # Encuentra todos los archivos estáticos a reindexar + # + # @return [Array] + def indexable_static_files + @indexable_static_files ||= + diff_with_head.each_delta.select do |delta| + delta.old_file[:path].start_with? 'public/' + end + end + # Elimina los artículos eliminados o que cambiaron de ubicación # del índice def remove_deleted_posts! @@ -94,6 +104,45 @@ class Site end end + # Elimina de ActiveStorage los archivos borrados, renombrados o + # modificados, para poder recalcular su contenido y nueva + # ubicación. + # + # Los renombrados o modificados se vuelven a crear en + # reindex_modified_static_files! + def remove_deleted_or_renamed_static_files! + indexable_static_files.select do |delta| + delta.deleted? || delta.modified? || delta.renamed? + end.each do |delta| + key = blob_service.key_from_path(delta.old_file[:path]) + + static_files.blobs.find_by(service_name: name, key: key).tap do |blob| + next unless blob + + transaction do + static_files.where(blob_id: blob.id).destroy_all + blob.destroy + end + end + end + end + + # Reindexa los archivos estáticos modificados + def reindex_modified_static_files! + indexable_static_files.select do |delta| + MODIFIED_STATUSES.include?(delta.status) || delta.renamed? + end.each do |delta| + transaction do + path = blob_service.absolute_path_for(delta.new_file[:path].sub(%r{\Apublic/}, '')) + key = blob_service.key_from_path(path) + pathname = Pathname.new(path) + blob = ActiveStorage::Blob.create_after_unfurling!(key: key, io: pathname.open, filename: pathname.basename, service_name: name) + + ActiveStorage::Attachment.create!(name: 'static_files', record: self, blob: blob) + end + end + end + # Reindexa artículos que cambiaron de ubicación, se agregaron # o fueron modificados def reindex_modified_posts! From 9837aa72aad94cbb80cca203ff0486d1d1e4aae1 Mon Sep 17 00:00:00 2001 From: f Date: Fri, 2 Feb 2024 14:07:09 -0300 Subject: [PATCH 6/8] fix: solo cargar archivos en public/ --- app/models/site/index.rb | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/app/models/site/index.rb b/app/models/site/index.rb index d66c5272..9d7ca08f 100644 --- a/app/models/site/index.rb +++ b/app/models/site/index.rb @@ -80,13 +80,18 @@ class Site end end - # Encuentra todos los archivos estáticos a reindexar + # Encuentra todos los archivos estáticos a reindexar, si fueron + # borrados necesitamos saber la ubicación anterior, si son nuevos, + # la nueva. # # @return [Array] def indexable_static_files @indexable_static_files ||= diff_with_head.each_delta.select do |delta| - delta.old_file[:path].start_with? 'public/' + ( + delta.status == :deleted && + delta.old_file[:path].start_with?('public/') + ) || delta.new_file[:path].start_with?('public/') end end From e3a4130ed9712806665e2f43a5593fa65722e820 Mon Sep 17 00:00:00 2001 From: f Date: Fri, 2 Feb 2024 14:15:13 -0300 Subject: [PATCH 7/8] chore: deshabilitar yuta --- .rubocop.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.rubocop.yml b/.rubocop.yml index 9e60da6c..a8f37ace 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -4,3 +4,5 @@ AllCops: Style/AsciiComments: Enabled: false +Style/MultilineBlockChain: + Enabled: false From 9f5df8050bd69e8049291b3ab5994956eef5469b Mon Sep 17 00:00:00 2001 From: f Date: Fri, 2 Feb 2024 14:15:30 -0300 Subject: [PATCH 8/8] fix: actualizar el ruby de rubocop --- .rubocop.yml | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index a8f37ace..487320c0 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,7 +1,6 @@ AllCops: - TargetRubyVersion: '2.7' - NewCops: enable - + TargetRubyVersion: "3.1" + NewCops: "enable" Style/AsciiComments: Enabled: false Style/MultilineBlockChain: