5
0
Fork 0
mirror of https://0xacab.org/sutty/sutty synced 2024-11-26 10:26:22 +00:00

feat: modificar relaciones en PostService

(cherry picked from commit 3e88a98f5e)
This commit is contained in:
f 2023-10-26 12:24:41 -03:00
parent 232cda1379
commit ce299cc9d3
No known key found for this signature in database

View file

@ -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]
@ -198,7 +194,6 @@ PostService = Struct.new(:site, :usuarie, :post, :params, keyword_init: true) do
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