poder buscar posts según un array de valores

This commit is contained in:
f 2020-07-22 16:22:25 -03:00
parent 8364259342
commit a078f2b87e

View file

@ -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