From 6d59d79e2a086bc653565cb93c9ef6406a9b910c Mon Sep 17 00:00:00 2001 From: f Date: Wed, 22 Mar 2023 14:26:06 -0300 Subject: [PATCH] =?UTF-8?q?fix:=20establecer=20relaci=C3=B3n=20bidireccion?= =?UTF-8?q?al=20#10491?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/metadata_locales.rb | 36 +++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/app/models/metadata_locales.rb b/app/models/metadata_locales.rb index 702e1902..2e9080c9 100644 --- a/app/models/metadata_locales.rb +++ b/app/models/metadata_locales.rb @@ -7,9 +7,43 @@ class MetadataLocales < MetadataHasAndBelongsToMany # @return { lang: { title: uuid } } def values @values ||= site.locales.map do |locale| - [locale, site.posts(lang: locale).map do |post| + [locale, posts.where(lang: locale).map do |post| [title(post), post.uuid.value] end.to_h] end.to_h end + + # Siempre hay una relaciĆ³n inversa + # + # @return [True] + def inverse? + true + end + + # El campo inverso se llama igual en el otro post + # + # @return [Symbol] + def inverse + :locales + end + + private + + # Obtiene todos los locales distintos a este post + # + # @return [Array] + def other_locales + site.locales.reject do |locale| + locale == post.lang.value.to_sym + end + end + + # Obtiene todos los posts de los otros locales con el mismo layout + # + # @return [PostRelation] + def posts + other_locales.map do |locale| + site.posts(lang: locale).where(layout: post.layout.value) + end.reduce(&:concat) + end end