From e1038cd404b31286cf3a41d6f21bb34bf1f95307 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Gaya?= <sebastien.gaya@gmail.com> Date: Thu, 28 Apr 2022 19:12:43 +0200 Subject: [PATCH] scopes --- app/models/communication/website/post.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/app/models/communication/website/post.rb b/app/models/communication/website/post.rb index 1612921ec..5a6f0d1e1 100644 --- a/app/models/communication/website/post.rb +++ b/app/models/communication/website/post.rb @@ -69,8 +69,11 @@ class Communication::Website::Post < ApplicationRecord scope :ordered, -> { order(published_at: :desc, created_at: :desc) } scope :recent, -> { order(published_at: :desc).limit(5) } scope :for_author, -> (author_id) { where(author_id: author_id) } - scope :for_category, -> (category_id) { } # TODO Sebou - scope :for_pinned, -> (pinned) { where(pinned: pinned == 'true') } # TODO Sebou + scope :for_category, -> (category_id) { + descendant_ids = Communication::Website::Category.find_by(id: category_id)&.descendants&.map(&:id) || [] + joins(:categories).where(communication_website_categories: { id: [category_id, descendant_ids].flatten }).distinct + } + scope :for_pinned, -> (pinned) { where(pinned: pinned == 'true') } scope :for_search_term, -> (term) { where(" unaccent(communication_website_posts.description) ILIKE unaccent(:term) OR -- GitLab