From 737416488b5ca8b87c90dcf1c6205fad279d71e8 Mon Sep 17 00:00:00 2001 From: f Date: Wed, 17 Feb 2021 18:46:00 -0300 Subject: [PATCH] =?UTF-8?q?garantizar=20que=20PostRelation=20siempre=20usa?= =?UTF-8?q?=20la=20misma=20colecci=C3=B3n/idioma?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/post_relation.rb | 11 ++++++----- app/models/site.rb | 5 +++-- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/app/models/post_relation.rb b/app/models/post_relation.rb index 7ea9aef6..850a83dc 100644 --- a/app/models/post_relation.rb +++ b/app/models/post_relation.rb @@ -4,10 +4,11 @@ # artículos como si estuviésemos usando ActiveRecord. class PostRelation < Array # No necesitamos cambiar el sitio - attr_reader :site + attr_reader :site, :lang - def initialize(site:) + def initialize(site:, lang:) @site = site + @lang = lang # Proseguimos la inicialización sin valores por defecto super() end @@ -15,7 +16,7 @@ class PostRelation < Array # Genera un artículo nuevo con los parámetros que le pasemos y lo suma # al array def build(**args) - args[:lang] ||= I18n.locale + args[:lang] = lang args[:document] ||= build_document(collection: args[:lang]) args[:layout] = build_layout(args[:layout]) @@ -94,7 +95,7 @@ class PostRelation < Array @where ||= {} @where[args.hash.to_s] ||= begin - PostRelation.new(site: site).concat(select do |post| + PostRelation.new(site: site, lang: lang).concat(select do |post| result = args.map do |attr, value| next unless post.attribute?(attr) @@ -123,7 +124,7 @@ class PostRelation < Array # @return [PostRelation] alias array_select select def select(&block) - PostRelation.new(site: site).concat array_select(&block) + PostRelation.new(site: site, lang: lang).concat array_select(&block) end # Intenta guardar todos y devuelve true si pudo diff --git a/app/models/site.rb b/app/models/site.rb index 6c930836..4ec0b9f9 100644 --- a/app/models/site.rb +++ b/app/models/site.rb @@ -225,6 +225,7 @@ class Site < ApplicationRecord # Traemos los posts del idioma actual por defecto lang ||= I18n.locale + lang = lang.to_sym # Crea un Struct dinámico con los valores de los locales, si # llegamos a pasar un idioma que no existe vamos a tener una @@ -233,7 +234,7 @@ class Site < ApplicationRecord return @posts[lang] unless @posts[lang].blank? - @posts[lang] = PostRelation.new site: self + @posts[lang] = PostRelation.new site: self, lang: lang # No fallar si no existe colección para este idioma # XXX: queremos fallar silenciosamente? @@ -250,7 +251,7 @@ class Site < ApplicationRecord # # @return PostRelation def docs - @docs ||= PostRelation.new(site: self).push(locales.flat_map do |locale| + @docs ||= PostRelation.new(site: self, lang: :docs).push(locales.flat_map do |locale| posts(lang: locale) end).flatten! end