From cc38b449a2e168f808d252275bcc5455fec74853 Mon Sep 17 00:00:00 2001
From: pabois <pierreandre.boissinot@noesya.coop>
Date: Thu, 28 Apr 2022 16:56:59 +0200
Subject: [PATCH] filtres

---
 .../admin/communication/website/posts_controller.rb            | 3 +++
 app/models/communication/website/post.rb                       | 3 +++
 app/services/filters/admin/communication/website/posts.rb      | 2 +-
 config/locales/communication/en.yml                            | 2 ++
 config/locales/communication/fr.yml                            | 2 ++
 5 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/app/controllers/admin/communication/website/posts_controller.rb b/app/controllers/admin/communication/website/posts_controller.rb
index 2b33de831..1bd2a9f59 100644
--- a/app/controllers/admin/communication/website/posts_controller.rb
+++ b/app/controllers/admin/communication/website/posts_controller.rb
@@ -6,6 +6,9 @@ class Admin::Communication::Website::PostsController < Admin::Communication::Web
   before_action :load_filters, only: :index
 
   has_scope :for_search_term
+  has_scope :for_author
+  has_scope :for_category
+  has_scope :for_pinned
 
   def index
     @posts = apply_scopes(@posts).ordered.page params[:page]
diff --git a/app/models/communication/website/post.rb b/app/models/communication/website/post.rb
index ace44b44c..1612921ec 100644
--- a/app/models/communication/website/post.rb
+++ b/app/models/communication/website/post.rb
@@ -68,6 +68,9 @@ class Communication::Website::Post < ApplicationRecord
   scope :published, -> { where(published: true) }
   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_search_term, -> (term) {
     where("
       unaccent(communication_website_posts.description) ILIKE unaccent(:term) OR
diff --git a/app/services/filters/admin/communication/website/posts.rb b/app/services/filters/admin/communication/website/posts.rb
index c5cfe1fe5..30559c526 100644
--- a/app/services/filters/admin/communication/website/posts.rb
+++ b/app/services/filters/admin/communication/website/posts.rb
@@ -5,7 +5,7 @@ module Filters
       add_search
       add :for_author, website.authors.ordered, I18n.t('filters.attributes.element', element: I18n.t('activerecord.attributes.communication/website/post.author').downcase)
       add :for_category, website.categories, I18n.t('filters.attributes.element', element: I18n.t('activerecord.models.communication/website/category.one').downcase), false, true
-
+      add :for_pinned, [{ to_s: I18n.t('true'), id: 'true' }, { to_s: I18n.t('false'), id: 'false' }], I18n.t('filters.attributes.element', element: I18n.t('admin.communication.website.post.pinned_status').downcase)
     end
   end
 end
diff --git a/config/locales/communication/en.yml b/config/locales/communication/en.yml
index abc8d0242..aab9134ea 100644
--- a/config/locales/communication/en.yml
+++ b/config/locales/communication/en.yml
@@ -202,6 +202,8 @@ en:
           delete_special_page_notice: Can't delete this page
           is_special_page: "This is a page with a special behaviour:"
           structure: Structure
+        post:
+          pinned_status: Pinned status
   communication:
     authors:
       one: Author
diff --git a/config/locales/communication/fr.yml b/config/locales/communication/fr.yml
index 95e7e7433..a6b2b3291 100644
--- a/config/locales/communication/fr.yml
+++ b/config/locales/communication/fr.yml
@@ -204,6 +204,8 @@ fr:
           delete_special_page_notice: Impossible de supprimer cette page
           is_special_page: "Ceci est une page avec un comportement spécial :"
           structure: Arborescence
+        post:
+          pinned_status: Mise en avant
   communication:
     authors:
       one: Auteur·rice
-- 
GitLab