mirror of
https://0xacab.org/sutty/sutty
synced 2024-11-23 09:06:22 +00:00
refactor: agregar relaciones
This commit is contained in:
parent
b81e38e9ea
commit
66472d4b6f
1 changed files with 16 additions and 11 deletions
|
@ -199,10 +199,7 @@ PostService = Struct.new(:site, :usuarie, :post, :params, keyword_init: true) do
|
||||||
end
|
end
|
||||||
|
|
||||||
associated_posts(value - value_was).each do |add_post|
|
associated_posts(value - value_was).each do |add_post|
|
||||||
add_post[inverse_attribute].value << post.uuid.value
|
add_relation_to(add_post[inverse_attribute], post.uuid.value)
|
||||||
add_post[inverse_attribute].value.uniq!
|
|
||||||
|
|
||||||
associated_posts_to_save << add_post
|
|
||||||
end
|
end
|
||||||
when 'has_many'
|
when 'has_many'
|
||||||
associated_posts(value_was - value).each do |remove_post|
|
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)
|
remove_relation_from(remove_post[attribute], add_post.uuid.value)
|
||||||
end
|
end
|
||||||
|
|
||||||
add_post[inverse_attribute].value = post.uuid.value
|
add_relation_to(add_post[inverse_attribute], post.uuid.value)
|
||||||
|
|
||||||
associated_posts_to_save << add_post
|
|
||||||
end
|
end
|
||||||
when 'belongs_to'
|
when 'belongs_to'
|
||||||
if value_was.present?
|
if value_was.present?
|
||||||
|
@ -226,10 +221,7 @@ PostService = Struct.new(:site, :usuarie, :post, :params, keyword_init: true) do
|
||||||
end
|
end
|
||||||
|
|
||||||
associated_posts(value).each do |add_post|
|
associated_posts(value).each do |add_post|
|
||||||
add_post[inverse_attribute].value << post.uuid.value
|
add_relation_to(add_post[inverse_attribute], post.uuid.value)
|
||||||
add_post[inverse_attribute].value.uniq!
|
|
||||||
|
|
||||||
associated_posts_to_save << add_post
|
|
||||||
end
|
end
|
||||||
when 'locales'
|
when 'locales'
|
||||||
end
|
end
|
||||||
|
@ -246,4 +238,17 @@ PostService = Struct.new(:site, :usuarie, :post, :params, keyword_init: true) do
|
||||||
associated_posts_to_save << metadata.post
|
associated_posts_to_save << metadata.post
|
||||||
nil
|
nil
|
||||||
end
|
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
|
end
|
||||||
|
|
Loading…
Reference in a new issue