From ec65fdadab80a90c01b53c383256b41d88afc2b2 Mon Sep 17 00:00:00 2001
From: pabois <pierreandre.boissinot@noesya.coop>
Date: Thu, 28 Apr 2022 16:08:20 +0200
Subject: [PATCH] wip #279

---
 .../admin/communication/extranets_controller.rb    |  4 ++++
 .../communication/website/authors_controller.rb    |  4 +++-
 .../communication/website/posts_controller.rb      |  4 +++-
 .../admin/communication/websites_controller.rb     |  5 ++++-
 app/models/communication/extranet.rb               |  8 ++++++++
 app/models/communication/website.rb                | 14 ++++++++++----
 app/models/communication/website/post.rb           |  9 ++++++++-
 app/models/concerns/with_abouts.rb                 |  2 ++
 .../filters/admin/communication/extranets.rb       |  9 +++++++++
 .../filters/admin/communication/website/authors.rb |  8 ++++++++
 .../filters/admin/communication/website/posts.rb   |  8 ++++++++
 .../filters/admin/communication/websites.rb        |  9 +++++++++
 .../admin/communication/extranets/index.html.erb   |  6 +++++-
 .../communication/website/authors/index.html.erb   |  2 ++
 .../communication/website/posts/index.html.erb     |  2 ++
 .../admin/communication/websites/index.html.erb    |  7 ++++++-
 16 files changed, 91 insertions(+), 10 deletions(-)
 create mode 100644 app/services/filters/admin/communication/extranets.rb
 create mode 100644 app/services/filters/admin/communication/website/authors.rb
 create mode 100644 app/services/filters/admin/communication/website/posts.rb
 create mode 100644 app/services/filters/admin/communication/websites.rb

diff --git a/app/controllers/admin/communication/extranets_controller.rb b/app/controllers/admin/communication/extranets_controller.rb
index 6cd178308..2d11a6318 100644
--- a/app/controllers/admin/communication/extranets_controller.rb
+++ b/app/controllers/admin/communication/extranets_controller.rb
@@ -3,7 +3,11 @@ class Admin::Communication::ExtranetsController < Admin::Communication::Applicat
                               through: :current_university,
                               through_association: :communication_extranets
 
+  has_scope :for_search_term
+  has_scope :for_about_type
+
   def index
+    @extranets = apply_scopes(@extranets).ordered.page(params[:page])
     breadcrumb
   end
 
diff --git a/app/controllers/admin/communication/website/authors_controller.rb b/app/controllers/admin/communication/website/authors_controller.rb
index e4b694f4b..934b4faf2 100644
--- a/app/controllers/admin/communication/website/authors_controller.rb
+++ b/app/controllers/admin/communication/website/authors_controller.rb
@@ -1,7 +1,9 @@
 class Admin::Communication::Website::AuthorsController < Admin::Communication::Website::ApplicationController
 
+  has_scope :for_search_term
+  
   def index
-    @authors =  @website.authors.accessible_by(current_ability)
+    @authors =  apply_scopes(@website.authors.accessible_by(current_ability))
                                 .ordered
                                 .page(params[:page])
     breadcrumb
diff --git a/app/controllers/admin/communication/website/posts_controller.rb b/app/controllers/admin/communication/website/posts_controller.rb
index ef6456510..aed1f3de8 100644
--- a/app/controllers/admin/communication/website/posts_controller.rb
+++ b/app/controllers/admin/communication/website/posts_controller.rb
@@ -1,8 +1,10 @@
 class Admin::Communication::Website::PostsController < Admin::Communication::Website::ApplicationController
   load_and_authorize_resource class: Communication::Website::Post, through: :website
 
+  has_scope :for_search_term
+  
   def index
-    @posts = @posts.ordered.page params[:page]
+    @posts = apply_scopes(@posts).ordered.page params[:page]
     breadcrumb
   end
 
diff --git a/app/controllers/admin/communication/websites_controller.rb b/app/controllers/admin/communication/websites_controller.rb
index 97df27604..aa95abda1 100644
--- a/app/controllers/admin/communication/websites_controller.rb
+++ b/app/controllers/admin/communication/websites_controller.rb
@@ -3,8 +3,11 @@ class Admin::Communication::WebsitesController < Admin::Communication::Applicati
                               through: :current_university,
                               through_association: :communication_websites
 
+  has_scope :for_search_term
+  has_scope :for_about_type
+
   def index
-    @websites = @websites.ordered
+    @websites = apply_scopes(@websites).ordered.page(params[:page])
     breadcrumb
     add_breadcrumb Communication::Website.model_name.human(count: 2), admin_communication_websites_path
   end
diff --git a/app/models/communication/extranet.rb b/app/models/communication/extranet.rb
index f04134752..968f35b92 100644
--- a/app/models/communication/extranet.rb
+++ b/app/models/communication/extranet.rb
@@ -28,6 +28,14 @@ class Communication::Extranet < ApplicationRecord
 
   has_one_attached_deletable :logo
 
+  scope :ordered, -> { order(:name) }
+  scope :for_search_term, -> (term) {
+    where("
+      unaccent(communication_extranets.domain) ILIKE unaccent(:term) OR
+      unaccent(communication_extranets.name) ILIKE unaccent(:term)
+    ", term: "%#{sanitize_sql_like(term)}%")
+  }
+
   def self.with_host(host)
     find_by domain: host
   end
diff --git a/app/models/communication/website.rb b/app/models/communication/website.rb
index 339043d19..595984586 100644
--- a/app/models/communication/website.rb
+++ b/app/models/communication/website.rb
@@ -36,6 +36,11 @@ class Communication::Website < ApplicationRecord
   include WithProgramCategories
   include WithSpecialPages
 
+  enum git_provider: {
+    github: 0,
+    gitlab: 1
+  }
+
   has_and_belongs_to_many :languages,
                           class_name: 'Language',
                           join_table: 'communication_websites_languages',
@@ -43,10 +48,11 @@ class Communication::Website < ApplicationRecord
                           association_foreign_key: 'language_id'
 
   scope :ordered, -> { order(:name) }
-
-  enum git_provider: {
-    github: 0,
-    gitlab: 1
+  scope :for_search_term, -> (term) {
+    where("
+      unaccent(communication_websites.name) ILIKE unaccent(:term) OR
+      unaccent(communication_websites.url) ILIKE unaccent(:term)
+    ", term: "%#{sanitize_sql_like(term)}%")
   }
 
   def to_s
diff --git a/app/models/communication/website/post.rb b/app/models/communication/website/post.rb
index 14cda1e87..ace44b44c 100644
--- a/app/models/communication/website/post.rb
+++ b/app/models/communication/website/post.rb
@@ -68,7 +68,14 @@ 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_search_term, -> (term) {
+    where("
+      unaccent(communication_website_posts.description) ILIKE unaccent(:term) OR
+      unaccent(communication_website_posts.description_short) ILIKE unaccent(:term) OR
+      unaccent(communication_website_posts.text) ILIKE unaccent(:term) OR
+      unaccent(communication_website_posts.title) ILIKE unaccent(:term)
+    ", term: "%#{sanitize_sql_like(term)}%")
+  }
 
   def path
     # used in menu_item#static_target
diff --git a/app/models/concerns/with_abouts.rb b/app/models/concerns/with_abouts.rb
index 55d5b9248..c2cbb413b 100644
--- a/app/models/concerns/with_abouts.rb
+++ b/app/models/concerns/with_abouts.rb
@@ -6,6 +6,8 @@ module WithAbouts
                 polymorphic: true,
                 optional: true
 
+    scope :for_about_type, -> (type) { where(about_type: type) }
+
     def self.about_types
       [
         nil,
diff --git a/app/services/filters/admin/communication/extranets.rb b/app/services/filters/admin/communication/extranets.rb
new file mode 100644
index 000000000..059f47780
--- /dev/null
+++ b/app/services/filters/admin/communication/extranets.rb
@@ -0,0 +1,9 @@
+module Filters
+  class Admin::Communication::Extranets < Filters::Base
+    def initialize(user)
+      super
+      add_search
+      add :for_about_type, ::Communication::Extranet::about_types.compact.map { |r| { to_s: I18n.t("activerecord.attributes.communication/extranet.about_#{r}"), id: r } }, I18n.t('filters.attributes.kind')
+    end
+  end
+end
diff --git a/app/services/filters/admin/communication/website/authors.rb b/app/services/filters/admin/communication/website/authors.rb
new file mode 100644
index 000000000..959f212bc
--- /dev/null
+++ b/app/services/filters/admin/communication/website/authors.rb
@@ -0,0 +1,8 @@
+module Filters
+  class Admin::Communication::Website::Authors < Filters::Base
+    def initialize(user)
+      super
+      add_search
+    end
+  end
+end
diff --git a/app/services/filters/admin/communication/website/posts.rb b/app/services/filters/admin/communication/website/posts.rb
new file mode 100644
index 000000000..afa833c2a
--- /dev/null
+++ b/app/services/filters/admin/communication/website/posts.rb
@@ -0,0 +1,8 @@
+module Filters
+  class Admin::Communication::Website::Posts < Filters::Base
+    def initialize(user)
+      super
+      add_search
+    end
+  end
+end
diff --git a/app/services/filters/admin/communication/websites.rb b/app/services/filters/admin/communication/websites.rb
new file mode 100644
index 000000000..4f43797b3
--- /dev/null
+++ b/app/services/filters/admin/communication/websites.rb
@@ -0,0 +1,9 @@
+module Filters
+  class Admin::Communication::Websites < Filters::Base
+    def initialize(user)
+      super
+      add_search
+      add :for_about_type, ::Communication::Website::about_types.compact.map { |r| { to_s: I18n.t("activerecord.attributes.communication/website.about_#{r}"), id: r } }, I18n.t('filters.attributes.kind')
+    end
+  end
+end
diff --git a/app/views/admin/communication/extranets/index.html.erb b/app/views/admin/communication/extranets/index.html.erb
index 5a83cd771..8b7e80ea0 100644
--- a/app/views/admin/communication/extranets/index.html.erb
+++ b/app/views/admin/communication/extranets/index.html.erb
@@ -1,4 +1,6 @@
-<% content_for :title, Communication::Extranet.model_name.human(count: 2) %>
+<% content_for :title, "#{Communication::Extranet.model_name.human(count: 2)} (#{@extranets.total_count})" %>
+
+<%= render 'filters', current_path: admin_communication_extranets_path, filters: @filters if @filters.any?  %>
 
 <table class="<%= table_classes %>">
   <thead>
@@ -29,6 +31,8 @@
   </tbody>
 </table>
 
+<%= paginate @extranets, theme: 'bootstrap-5' %>
+
 <% content_for :action_bar_right do %>
   <%= create_link Communication::Extranet %>
 <% end %>
diff --git a/app/views/admin/communication/website/authors/index.html.erb b/app/views/admin/communication/website/authors/index.html.erb
index 40776b6c8..109407dc5 100644
--- a/app/views/admin/communication/website/authors/index.html.erb
+++ b/app/views/admin/communication/website/authors/index.html.erb
@@ -1,6 +1,8 @@
 <% content_for :title, "#{t('communication.authors', count: 2)} (#{@authors.total_count})" %>
 
 <%= render 'admin/communication/websites/sidebar' do %>
+  <%= render 'filters', current_path: admin_communication_website_authors_path, filters: @filters if @filters.any?  %>
+
   <div class="card">
     <%= render 'admin/communication/website/authors/list', authors: @authors %>
     <% if @authors.total_pages > 1 %>
diff --git a/app/views/admin/communication/website/posts/index.html.erb b/app/views/admin/communication/website/posts/index.html.erb
index 7b70ee606..4f0118150 100644
--- a/app/views/admin/communication/website/posts/index.html.erb
+++ b/app/views/admin/communication/website/posts/index.html.erb
@@ -1,6 +1,8 @@
 <% content_for :title, "#{Communication::Website::Post.model_name.human(count: 2)} (#{@posts.total_count})" %>
 
 <%= render 'admin/communication/websites/sidebar' do %>
+  <%= render 'filters', current_path: admin_communication_website_posts_path, filters: @filters if @filters.any?  %>
+
   <div class="card">
     <%= form_tag publish_admin_communication_website_posts_path do %>
       <input type="hidden" name="ids[]" value="">
diff --git a/app/views/admin/communication/websites/index.html.erb b/app/views/admin/communication/websites/index.html.erb
index a4b8e7879..40daabbc0 100644
--- a/app/views/admin/communication/websites/index.html.erb
+++ b/app/views/admin/communication/websites/index.html.erb
@@ -1,4 +1,6 @@
-<% content_for :title, Communication::Website.model_name.human(count: 2) %>
+<% content_for :title, "#{Communication::Website.model_name.human(count: 2)} (#{@websites.total_count})" %>
+
+<%= render 'filters', current_path: admin_communication_websites_path, filters: @filters if @filters.any?  %>
 
 <table class="<%= table_classes %>">
   <thead>
@@ -28,6 +30,9 @@
   </tbody>
 </table>
 
+<%= paginate @websites, theme: 'bootstrap-5' %>
+
+
 <% content_for :action_bar_right do %>
   <%= create_link Communication::Website %>
 <% end %>
-- 
GitLab