diff --git a/app/services/post_service.rb b/app/services/post_service.rb index c82953e3..83d30a50 100644 --- a/app/services/post_service.rb +++ b/app/services/post_service.rb @@ -199,10 +199,7 @@ PostService = Struct.new(:site, :usuarie, :post, :params, keyword_init: true) do end associated_posts(value - value_was).each do |add_post| - add_post[inverse_attribute].value << post.uuid.value - add_post[inverse_attribute].value.uniq! - - associated_posts_to_save << add_post + add_relation_to(add_post[inverse_attribute], post.uuid.value) end when 'has_many' associated_posts(value_was - value).each do |remove_post| @@ -214,9 +211,7 @@ PostService = Struct.new(:site, :usuarie, :post, :params, keyword_init: true) do remove_relation_from(remove_post[attribute], add_post.uuid.value) end - add_post[inverse_attribute].value = post.uuid.value - - associated_posts_to_save << add_post + add_relation_to(add_post[inverse_attribute], post.uuid.value) end when 'belongs_to' if value_was.present? @@ -226,10 +221,7 @@ PostService = Struct.new(:site, :usuarie, :post, :params, keyword_init: true) do 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 + add_relation_to(add_post[inverse_attribute], post.uuid.value) end when 'locales' end @@ -246,4 +238,17 @@ PostService = Struct.new(:site, :usuarie, :post, :params, keyword_init: true) do associated_posts_to_save << metadata.post nil end + + # @todo El validador ya debería eliminar valores duplicados + def add_relation_to(metadata, value) + case metadata.value + when Array + metadata.value << value + metadata.uniq! + when String then metadata.value = value + end + + associated_posts_to_save << metadata.post + nil + end end