5
0
Fork 0
mirror of https://0xacab.org/sutty/sutty synced 2024-11-26 10:36:21 +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 added_paths << post.path.value
# Recorrer todas las asociaciones y agregarse donde corresponda # Recorrer todas las asociaciones y agregarse donde corresponda
update_associations_forward(post) update_associations(post)
associated_posts_to_save.each do |associated_post| associated_posts_to_save.each do |associated_post|
next unless associated_post.save(validate: false) 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 added_paths << post.path.value
# Recorrer todas las asociaciones y agregarse donde corresponda # Recorrer todas las asociaciones y agregarse donde corresponda
update_associations_forward(post) update_associations(post)
associated_posts_to_save.each do |associated_post| associated_posts_to_save.each do |associated_post|
next unless associated_post.save(validate: false) 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) site.indexed_posts.where(post_id: post_ids).map(&:post)
end end
# Modificar las asociaciones. # Modificar las asociaciones en cascada
# def update_associations(post)
# Si el valor actual es una String, es un BelongsTo
#
#
def update_associations_forward(post)
association_attributes(post).each do |attribute| association_attributes(post).each do |attribute|
metadata = post[attribute] metadata = post[attribute]
@ -194,11 +190,10 @@ PostService = Struct.new(:site, :usuarie, :post, :params, keyword_init: true) do
inverse_attribute = post[attribute].inverse inverse_attribute = post[attribute].inverse
value_was = metadata.value_was.dup value_was = metadata.value_was.dup
value = metadata.value.dup value = metadata.value.dup
case metadata.type case metadata.type
when 'has_and_belongs_to_many' when 'has_and_belongs_to_many'
binding.pry
associated_posts(value_was - value).each do |remove_post| associated_posts(value_was - value).each do |remove_post|
remove_post[inverse_attribute].value.delete(post.uuid.value) 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 associated_posts_to_save << add_post
end end
when 'has_many' 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' 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' when 'locales'
end end
end end