implementar uuid como metadata

This commit is contained in:
f 2020-01-02 20:29:04 -03:00
parent 0437444247
commit 5c8d8aafdf
No known key found for this signature in database
GPG key ID: 2AE5A13E321F953D
9 changed files with 37 additions and 14 deletions

View file

@ -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|

View file

@ -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

View file

@ -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

View file

@ -0,0 +1,3 @@
%tr{ id: attribute }
%th= post_label_t(attribute, post: post)
%td= metadata.value

View file

@ -0,0 +1 @@
-# nada

View file

@ -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

View file

@ -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'

View file

@ -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'

View file

@ -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