modernizar el reordenamiento

This commit is contained in:
f 2020-08-28 13:48:04 -03:00
parent f77a907753
commit ab3c2a8f42

View file

@ -58,25 +58,24 @@ PostService = Struct.new(:site, :usuarie, :post, :params, keyword_init: true) do
#
# { uuid => 2, uuid => 1, uuid => 0 }
def reorder
posts = site.posts(lang: locale)
reorder = params.require(:post).permit(reorder: {}).try(:[], :reorder)
modified = PostRelation.new(site: site)
reorder = params.require(:post).permit(reorder: {}).try(:[], :reorder).transform_values(&:to_i)
posts = site.posts(lang: locale).where(uuid: reorder.keys)
files = reorder.keys.map do |uuid|
post = posts.find(uuid, uuid: true)
order = reorder[uuid].to_i
files = posts.map do |post|
next unless post.attribute? :order
order = reorder[post.uuid.value]
next unless post
next unless post.attributes.include? :order
next if post.order.value == order
modified << post
post.order.value = order
post.path.absolute
end.compact
return if files.empty?
# TODO: Implementar transacciones!
modified.save_all(validate: false) &&
posts.save_all(validate: false) &&
commit(action: :reorder, file: files)
end