From a078f2b87e093882ab7549ab8c5a16486c2e85f1 Mon Sep 17 00:00:00 2001 From: f Date: Wed, 22 Jul 2020 16:22:25 -0300 Subject: [PATCH] =?UTF-8?q?poder=20buscar=20posts=20seg=C3=BAn=20un=20arra?= =?UTF-8?q?y=20de=20valores?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/post_relation.rb | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/app/models/post_relation.rb b/app/models/post_relation.rb index efc970b..0546cd7 100644 --- a/app/models/post_relation.rb +++ b/app/models/post_relation.rb @@ -84,16 +84,29 @@ class PostRelation < Array # Encuentra todos los Post que cumplan las condiciones # - # @param [Hash] + # @param [Hash] Mapa de atributo => valor. Valor puede ser un Array + # de valores # @return [PostRelation] def where(**args) return self if args.empty? PostRelation[*select do |post| args.map do |attr, value| - post.attribute?(attr) && - post.public_send(attr).value == value - end.all? + next unless post.attribute?(attr) + + attribute = post.public_send(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 + end + end + end.compact.all? end] end