From ab3c2a8f42f6d617f6133b25f15624d4914a738f Mon Sep 17 00:00:00 2001 From: f Date: Fri, 28 Aug 2020 13:48:04 -0300 Subject: [PATCH] modernizar el reordenamiento --- app/services/post_service.rb | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/app/services/post_service.rb b/app/services/post_service.rb index 16607a4a..5ee1f2fd 100644 --- a/app/services/post_service.rb +++ b/app/services/post_service.rb @@ -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