From ce299cc9d3906fbbe0c76e987b7f8a9279b1e927 Mon Sep 17 00:00:00 2001 From: f Date: Thu, 26 Oct 2023 12:24:41 -0300 Subject: [PATCH] feat: modificar relaciones en PostService (cherry picked from commit 3e88a98f5e9e1b31de8c058b7b5a41dfc4222f7c) --- app/services/post_service.rb | 47 ++++++++++++++++++++++++++++-------- 1 file changed, 37 insertions(+), 10 deletions(-) diff --git a/app/services/post_service.rb b/app/services/post_service.rb index dc04dda6..1d7e1a99 100644 --- a/app/services/post_service.rb +++ b/app/services/post_service.rb @@ -20,7 +20,7 @@ PostService = Struct.new(:site, :usuarie, :post, :params, keyword_init: true) do added_paths << post.path.value # Recorrer todas las asociaciones y agregarse donde corresponda - update_associations_forward(post) + update_associations(post) associated_posts_to_save.each do |associated_post| next unless associated_post.save(validate: false) @@ -65,7 +65,7 @@ PostService = Struct.new(:site, :usuarie, :post, :params, keyword_init: true) do added_paths << post.path.value # Recorrer todas las asociaciones y agregarse donde corresponda - update_associations_forward(post) + update_associations(post) associated_posts_to_save.each do |associated_post| next unless associated_post.save(validate: false) @@ -181,12 +181,8 @@ PostService = Struct.new(:site, :usuarie, :post, :params, keyword_init: true) do site.indexed_posts.where(post_id: post_ids).map(&:post) end - # Modificar las asociaciones. - # - # Si el valor actual es una String, es un BelongsTo - # - # - def update_associations_forward(post) + # Modificar las asociaciones en cascada + def update_associations(post) association_attributes(post).each do |attribute| metadata = post[attribute] @@ -194,11 +190,10 @@ PostService = Struct.new(:site, :usuarie, :post, :params, keyword_init: true) do inverse_attribute = post[attribute].inverse value_was = metadata.value_was.dup - value = metadata.value.dup + value = metadata.value.dup case metadata.type when 'has_and_belongs_to_many' - binding.pry associated_posts(value_was - value).each do |remove_post| remove_post[inverse_attribute].value.delete(post.uuid.value) @@ -212,7 +207,39 @@ PostService = Struct.new(:site, :usuarie, :post, :params, keyword_init: true) do associated_posts_to_save << add_post end when 'has_many' + associated_posts(value_was - value).each do |remove_post| + # @todo por qué no podemos usar nil para deshabilitar un valor? + remove_post[inverse_attribute].value = '' + + associated_posts_to_save << remove_post + end + + associated_posts(value - value_was).each do |add_post| + associated_posts(add_post[inverse_attribute].value_was).each do |remove_post| + remove_post[attribute].value.delete(add_post.uuid.value) + + associated_posts_to_save << remove_post + end + + add_post[inverse_attribute].value = post.uuid.value + + associated_posts_to_save << add_post + end when 'belongs_to' + if value_was.present? + associated_posts(value_was).each do |remove_post| + remove_post[inverse_attribute].value.delete(value_was) + + associated_posts_to_save << remove_post + end + end + + associated_posts(value).each do |add_post| + add_post[inverse_attribute].value << post.uuid.value + add_post[inverse_attribute].value.uniq! + + associated_posts_to_save << add_post + end when 'locales' end end