From 1fb53168e2e1c872037ef56dd041ab469e662cb3 Mon Sep 17 00:00:00 2001 From: f Date: Fri, 27 Nov 2020 20:54:29 -0300 Subject: [PATCH] =?UTF-8?q?los=20art=C3=ADculos=20filtrados=20tambi=C3=A9n?= =?UTF-8?q?=20tienen=20un=20sitio?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/post_relation.rb | 39 ++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/app/models/post_relation.rb b/app/models/post_relation.rb index acf9058a..7ea9aef6 100644 --- a/app/models/post_relation.rb +++ b/app/models/post_relation.rb @@ -92,27 +92,30 @@ class PostRelation < Array def where(**args) return self if args.empty? - PostRelation[*select do |post| - result = args.map do |attr, value| - next unless post.attribute?(attr) + @where ||= {} + @where[args.hash.to_s] ||= begin + PostRelation.new(site: site).concat(select do |post| + result = args.map do |attr, value| + next unless post.attribute?(attr) - attribute = post.public_send(attr) + attribute = post[attr] - # TODO: Si el valor del atributo también es un Array deberíamos - # cruzar ambas. - case value - when Array then value.include? attribute.value - else - case attribute.value - when Array then attribute.value.include? value - else attribute.value == value + # TODO: Si el valor del atributo también es un Array deberíamos + # cruzar ambas. + case value + when Array then value.include? attribute.value + else + case attribute.value + when Array then attribute.value.include? value + else attribute.value == value + end end - end - end.compact + end.compact - # Un Array vacío devuelve true para all? - !result.empty? && result.all? - end] + # Un Array vacío devuelve true para all? + result.present? && result.all? + end) + end end # Como Array#select devolviendo una relación @@ -120,7 +123,7 @@ class PostRelation < Array # @return [PostRelation] alias array_select select def select(&block) - PostRelation[*array_select(&block)] + PostRelation.new(site: site).concat array_select(&block) end # Intenta guardar todos y devuelve true si pudo