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

---
 .../university/organizations_controller.rb    |  5 ++++-
 app/models/university/organization.rb         | 18 ++++++++++++++++
 app/models/university/person.rb               |  1 -
 .../filters/admin/university/organizations.rb |  9 ++++++++
 .../university/organizations/index.html.erb   |  2 ++
 config/locales/en.yml                         |  1 +
 config/locales/fr.yml                         |  1 +
 db/schema.rb                                  | 21 ++-----------------
 8 files changed, 37 insertions(+), 21 deletions(-)
 create mode 100644 app/services/filters/admin/university/organizations.rb

diff --git a/app/controllers/admin/university/organizations_controller.rb b/app/controllers/admin/university/organizations_controller.rb
index 4a168b2be..328dd0b54 100644
--- a/app/controllers/admin/university/organizations_controller.rb
+++ b/app/controllers/admin/university/organizations_controller.rb
@@ -3,8 +3,11 @@ class Admin::University::OrganizationsController < Admin::University::Applicatio
                               through: :current_university,
                               through_association: :organizations
 
+  has_scope :for_search_term
+  has_scope :for_kind
+
   def index
-    @organizations = @organizations.ordered.page(params[:page])
+    @organizations = apply_scopes(@organizations).ordered.page(params[:page])
     breadcrumb
   end
 
diff --git a/app/models/university/organization.rb b/app/models/university/organization.rb
index 84a53aad2..9e8a37796 100644
--- a/app/models/university/organization.rb
+++ b/app/models/university/organization.rb
@@ -45,6 +45,24 @@ class University::Organization < ApplicationRecord
   has_one_attached_deletable :logo
 
   scope :ordered, -> { order(:name) }
+  scope :for_kind, -> (kind) { where(kind: kind) }
+  scope :for_search_term, -> (term) {
+    where("
+      unaccent(university_organizations.address) ILIKE unaccent(:term) OR
+      unaccent(university_organizations.city) ILIKE unaccent(:term) OR
+      unaccent(university_organizations.country) ILIKE unaccent(:term) OR
+      unaccent(university_organizations.description) ILIKE unaccent(:term) OR
+      unaccent(university_organizations.email) ILIKE unaccent(:term) OR
+      unaccent(university_organizations.long_name) ILIKE unaccent(:term) OR
+      unaccent(university_organizations.name) ILIKE unaccent(:term) OR
+      unaccent(university_organizations.nic) ILIKE unaccent(:term) OR
+      unaccent(university_organizations.phone) ILIKE unaccent(:term) OR
+      unaccent(university_organizations.siren) ILIKE unaccent(:term) OR
+      unaccent(university_organizations.text) ILIKE unaccent(:term) OR
+      unaccent(university_organizations.zipcode) ILIKE unaccent(:term) OR
+      unaccent(university_organizations.url) ILIKE unaccent(:term)
+    ", term: "%#{sanitize_sql_like(term)}%")
+  }
 
   validates_presence_of :name
 
diff --git a/app/models/university/person.rb b/app/models/university/person.rb
index df746435d..b2d74f3b2 100644
--- a/app/models/university/person.rb
+++ b/app/models/university/person.rb
@@ -117,7 +117,6 @@ class University::Person < ApplicationRecord
   scope :researchers,     -> { where(is_researcher: true) }
   scope :alumni,          -> { where(is_alumnus: true) }
   scope :for_role, -> (role) { where("is_#{role}": true) }
-
   scope :for_search_term, -> (term) {
     where("
       unaccent(concat(university_people.first_name, ' ', university_people.last_name)) ILIKE unaccent(:term) OR
diff --git a/app/services/filters/admin/university/organizations.rb b/app/services/filters/admin/university/organizations.rb
new file mode 100644
index 000000000..85b22cef3
--- /dev/null
+++ b/app/services/filters/admin/university/organizations.rb
@@ -0,0 +1,9 @@
+module Filters
+  class Admin::University::Organizations < Filters::Base
+    def initialize(user)
+      super
+      add_search
+      add :for_kind, ::University::Organization::kinds.keys.map { |r| { to_s: I18n.t("enums.university.organization.kind.#{r}"), id: r } }, I18n.t('filters.attributes.kind')
+    end
+  end
+end
diff --git a/app/views/admin/university/organizations/index.html.erb b/app/views/admin/university/organizations/index.html.erb
index b538dd5cd..6fc56c6f7 100644
--- a/app/views/admin/university/organizations/index.html.erb
+++ b/app/views/admin/university/organizations/index.html.erb
@@ -1,5 +1,7 @@
 <% content_for :title, "#{University::Organization.model_name.human(count: 2)} (#{@organizations.total_count})" %>
 
+<%= render 'filters', current_path: admin_university_organizations_path, filters: @filters if @filters.any?  %>
+
 <%= render 'admin/university/organizations/list', organizations: @organizations %>
 <%= paginate @organizations, theme: 'bootstrap-5' %>
 
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 49586fa67..14bb8300d 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -96,6 +96,7 @@ en:
   filters:
     attributes:
       date: Filter by Date
+      kind: Filter by Kind
       role: Filter by Role
     buttons:
       expand: Filter table
diff --git a/config/locales/fr.yml b/config/locales/fr.yml
index 2963ff758..657b393cb 100644
--- a/config/locales/fr.yml
+++ b/config/locales/fr.yml
@@ -96,6 +96,7 @@ fr:
   filters:
     attributes:
       date: Filtrer par Date
+      kind: Filtrer par Type
       role: Filtrer par Rôle
     buttons:
       expand: Filtrer le tableau
diff --git a/db/schema.rb b/db/schema.rb
index 1b22f72bb..c2f1cfe20 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -319,10 +319,10 @@ ActiveRecord::Schema.define(version: 2022_04_27_094234) do
     t.uuid "related_category_id"
     t.string "featured_image_alt"
     t.text "text"
-    t.text "description_short"
     t.string "breadcrumb_title"
     t.text "header_text"
     t.integer "kind"
+    t.text "description_short"
     t.string "bodyclass"
     t.uuid "language_id"
     t.index ["communication_website_id"], name: "index_communication_website_pages_on_communication_website_id"
@@ -502,23 +502,6 @@ ActiveRecord::Schema.define(version: 2022_04_27_094234) do
     t.index ["university_id"], name: "index_education_schools_on_university_id"
   end
 
-  create_table "external_organizations", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
-    t.string "title"
-    t.text "description"
-    t.string "address"
-    t.string "zipcode"
-    t.string "city"
-    t.string "country"
-    t.string "website"
-    t.string "phone"
-    t.string "mail"
-    t.boolean "active"
-    t.string "sirene"
-    t.integer "kind"
-    t.datetime "created_at", precision: 6, null: false
-    t.datetime "updated_at", precision: 6, null: false
-  end
-
   create_table "languages", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
     t.string "name"
     t.string "iso_code"
@@ -702,8 +685,8 @@ ActiveRecord::Schema.define(version: 2022_04_27_094234) do
     t.string "linkedin"
     t.boolean "is_alumnus", default: false
     t.text "description_short"
-    t.string "name"
     t.boolean "is_author"
+    t.string "name"
     t.index ["university_id"], name: "index_university_people_on_university_id"
     t.index ["user_id"], name: "index_university_people_on_user_id"
   end
-- 
GitLab