From 5c8d8aafdf2bd2dba86b98f3e91f57fe5ef8557d Mon Sep 17 00:00:00 2001 From: f Date: Thu, 2 Jan 2020 20:29:04 -0300 Subject: [PATCH] implementar uuid como metadata --- app/models/post.rb | 17 ++++++++++++++--- app/models/post_relation.rb | 10 +++++++--- app/services/post_service.rb | 10 +++++----- app/views/posts/attribute_ro/_uuid.haml | 3 +++ app/views/posts/attributes/_uuid.haml | 1 + app/views/posts/index.haml | 2 +- config/locales/en.yml | 2 ++ config/locales/es.yml | 2 ++ test/controllers/posts_controller_test.rb | 4 ++-- 9 files changed, 37 insertions(+), 14 deletions(-) create mode 100644 app/views/posts/attribute_ro/_uuid.haml create mode 100644 app/views/posts/attributes/_uuid.haml diff --git a/app/models/post.rb b/app/models/post.rb index 8b851adc..25beb7fa 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -11,7 +11,7 @@ class Post < OpenStruct DEFAULT_ATTRIBUTES = %i[site document layout].freeze # Otros atributos que no vienen en los metadatos PRIVATE_ATTRIBUTES = %i[path slug attributes errors].freeze - PUBLIC_ATTRIBUTES = %i[lang date].freeze + PUBLIC_ATTRIBUTES = %i[lang date uuid].freeze class << self # Obtiene el layout sin leer el Document @@ -63,18 +63,20 @@ class Post < OpenStruct load_slug! load_date! load_path! + load_uuid! # XXX: No usamos Post#read porque a esta altura todavía no sabemos # nada del Document document.read! if File.exist? document.path end + # TODO: Convertir a UUID? def id path.basename end - def sha1 - Digest::SHA1.hexdigest id + def updated_at + File.mtime(path.absolute) end @@ -142,8 +144,10 @@ class Post < OpenStruct { metadata.to_s => template.value } end.compact.inject(:merge) + # TODO: Convertir a Metadata? # Asegurarse que haya un layout yaml['layout'] = layout.name.to_s + yaml['uuid'] = uuid.value # Y que no se procese liquid yaml['liquid'] = false yaml['usuaries'] = usuaries.map(&:id).uniq @@ -335,6 +339,13 @@ class Post < OpenStruct required: true) end + def load_uuid! + self.uuid = MetadataUuid.new(document: document, site: site, + layout: layout, name: :uuid, + type: :uuid, post: self, + required: true) + end + # Ejecuta la acción de guardado en cada atributo def save_attributes! attributes.map do |attr| diff --git a/app/models/post_relation.rb b/app/models/post_relation.rb index 934b2c54..e262893d 100644 --- a/app/models/post_relation.rb +++ b/app/models/post_relation.rb @@ -56,10 +56,14 @@ class PostRelation < Array alias find_generic find - # Encontra un post por su id convertido a SHA1 - def find(id, sha1: false) + # Encontrar un post por su UUID + def find(id, uuid: false) find_generic do |p| - p.sha1 == (sha1 ? id : Digest::SHA1.hexdigest(id)) + if uuid + p.uuid.value == id + else + p.id == id + end end end diff --git a/app/services/post_service.rb b/app/services/post_service.rb index b97a3fdd..1745290f 100644 --- a/app/services/post_service.rb +++ b/app/services/post_service.rb @@ -40,18 +40,18 @@ PostService = Struct.new(:site, :usuarie, :post, :params, keyword_init: true) do end # Reordena todos los posts que soporten orden de acuerdo a un hash de - # ids y nuevas posiciones. La posición actual la da la posición en + # uuids y nuevas posiciones. La posición actual la da la posición en # el array. # - # { sha1 => 2, sha1 => 1, sha1 => 0 } + # { uuid => 2, uuid => 1, uuid => 0 } def reorder posts = site.posts(lang: lang) reorder = params.require(:post).permit(reorder: {}).try(:[], :reorder) modified = PostRelation.new(site: site) - files = reorder.keys.map do |id| - post = posts.find(id, sha1: true) - order = reorder[id].to_i + files = reorder.keys.map do |uuid| + post = posts.find(uuid, uuid: true) + order = reorder[uuid].to_i next unless post.attributes.include? :order next if post.order.value == order diff --git a/app/views/posts/attribute_ro/_uuid.haml b/app/views/posts/attribute_ro/_uuid.haml new file mode 100644 index 00000000..31dd8f0d --- /dev/null +++ b/app/views/posts/attribute_ro/_uuid.haml @@ -0,0 +1,3 @@ +%tr{ id: attribute } + %th= post_label_t(attribute, post: post) + %td= metadata.value diff --git a/app/views/posts/attributes/_uuid.haml b/app/views/posts/attributes/_uuid.haml new file mode 100644 index 00000000..0aab9802 --- /dev/null +++ b/app/views/posts/attributes/_uuid.haml @@ -0,0 +1 @@ +-# nada diff --git a/app/views/posts/index.haml b/app/views/posts/index.haml index eab39338..7e0ce3a9 100644 --- a/app/views/posts/index.haml +++ b/app/views/posts/index.haml @@ -48,7 +48,7 @@ .handle = image_tag 'arrows-alt-v.svg' -# Orden más alto es mayor prioridad - = hidden_field 'post[reorder]', post.sha1, + = hidden_field 'post[reorder]', post.uuid.value, value: @posts.length - i, class: 'reorder' %td %small diff --git a/config/locales/en.yml b/config/locales/en.yml index 84215d03..4e0d1c8d 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -348,6 +348,8 @@ en: required: label: ' (required)' feedback: 'This field cannot be empty!' + uuid: + label: 'Unique identifier' reorder: 'Reorder posts' sort: by: 'Sort by' diff --git a/config/locales/es.yml b/config/locales/es.yml index 2c71813e..3a11551a 100644 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -358,6 +358,8 @@ es: required: label: ' (requerido)' feedback: '¡Este campo no puede estar vacío!' + uuid: + label: 'Identificador único' reorder: 'Reordenar artículos' sort: by: 'Ordenar por' diff --git a/test/controllers/posts_controller_test.rb b/test/controllers/posts_controller_test.rb index 3075948b..6a25fe30 100644 --- a/test/controllers/posts_controller_test.rb +++ b/test/controllers/posts_controller_test.rb @@ -145,7 +145,7 @@ class PostsControllerTest < ActionDispatch::IntegrationTest test 'se pueden reordenar' do lang = I18n.available_locales.sample posts = @site.posts(lang: lang) - reorder = Hash[posts.map(&:sha1).shuffle.each_with_index.to_a] + reorder = Hash[posts.map { |p| p.uuid.value }.shuffle.each_with_index.to_a] post site_posts_reorder_url(@site), headers: @authorization, @@ -157,7 +157,7 @@ class PostsControllerTest < ActionDispatch::IntegrationTest @site.repository.rugged.head.target.message assert_equal reorder, Hash[@site.posts(lang: lang).map do |p| - [p.sha1, p.order.value] + [p.uuid.value, p.order.value] end] end end