5
0
Fork 0
mirror of https://0xacab.org/sutty/sutty synced 2024-11-23 08:36:22 +00:00

refactor: agregar relaciones

This commit is contained in:
f 2023-10-26 16:29:58 -03:00
parent b81e38e9ea
commit 66472d4b6f
No known key found for this signature in database

View file

@ -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