From 064e23af1b2a61a96abfb1661fd6cd658cebfd0e Mon Sep 17 00:00:00 2001 From: pabois <pierreandre.boissinot@noesya.coop> Date: Thu, 28 Apr 2022 16:42:34 +0200 Subject: [PATCH] filters --- .../admin/communication/website/posts_controller.rb | 10 +++++++++- app/controllers/admin/education/schools_controller.rb | 1 + app/controllers/admin/education/teachers_controller.rb | 1 + app/models/education/school.rb | 3 ++- app/models/university/person.rb | 1 + app/models/university/person/teacher.rb | 1 + .../filters/admin/communication/website/posts.rb | 7 +++++-- app/services/filters/admin/education/schools.rb | 1 + app/services/filters/admin/education/teachers.rb | 1 + app/services/filters/base.rb | 5 +++-- app/views/admin/application/_filters.html.erb | 6 +++++- config/locales/en.yml | 1 + config/locales/fr.yml | 1 + 13 files changed, 32 insertions(+), 7 deletions(-) diff --git a/app/controllers/admin/communication/website/posts_controller.rb b/app/controllers/admin/communication/website/posts_controller.rb index aed1f3de8..2b33de831 100644 --- a/app/controllers/admin/communication/website/posts_controller.rb +++ b/app/controllers/admin/communication/website/posts_controller.rb @@ -1,8 +1,12 @@ class Admin::Communication::Website::PostsController < Admin::Communication::Website::ApplicationController + skip_before_action :load_filters + load_and_authorize_resource class: Communication::Website::Post, through: :website + before_action :load_filters, only: :index + has_scope :for_search_term - + def index @posts = apply_scopes(@posts).ordered.page params[:page] breadcrumb @@ -80,4 +84,8 @@ class Admin::Communication::Website::PostsController < Admin::Communication::Web ) .merge(university_id: current_university.id) end + + def load_filters + @filters = ::Filters::Admin::Communication::Website::Posts.new(current_user, @website).list + end end diff --git a/app/controllers/admin/education/schools_controller.rb b/app/controllers/admin/education/schools_controller.rb index f4c52f96a..37ea835aa 100644 --- a/app/controllers/admin/education/schools_controller.rb +++ b/app/controllers/admin/education/schools_controller.rb @@ -4,6 +4,7 @@ class Admin::Education::SchoolsController < Admin::Education::ApplicationControl through_association: :education_schools has_scope :for_search_term + has_scope :for_program def index @schools = apply_scopes(@schools).ordered.page(params[:page]) diff --git a/app/controllers/admin/education/teachers_controller.rb b/app/controllers/admin/education/teachers_controller.rb index 49e506997..c24b80557 100644 --- a/app/controllers/admin/education/teachers_controller.rb +++ b/app/controllers/admin/education/teachers_controller.rb @@ -2,6 +2,7 @@ class Admin::Education::TeachersController < Admin::Education::ApplicationContro before_action :load_teacher, only: [:show, :edit, :update] has_scope :for_search_term + has_scope :for_program def index @teachers = apply_scopes(current_university.people.teachers.accessible_by(current_ability)).ordered.page(params[:page]) diff --git a/app/models/education/school.rb b/app/models/education/school.rb index fdfbb5bab..663bf2e00 100644 --- a/app/models/education/school.rb +++ b/app/models/education/school.rb @@ -91,6 +91,7 @@ class Education::School < ApplicationRecord validates :name, :address, :city, :zipcode, :country, presence: true scope :ordered, -> { order(:name) } + scope :for_program, -> (program_id) { } # TODO @Sebou scope :for_search_term, -> (term) { where(" unaccent(education_schools.address) ILIKE unaccent(:term) OR @@ -98,7 +99,7 @@ class Education::School < ApplicationRecord unaccent(education_schools.country) ILIKE unaccent(:term) OR unaccent(education_schools.name) ILIKE unaccent(:term) OR unaccent(education_schools.phone) ILIKE unaccent(:term) OR - unaccent(education_schools.zipcode) ILIKE unaccent(:term) + unaccent(education_schools.zipcode) ILIKE unaccent(:term) ", term: "%#{sanitize_sql_like(term)}%") } diff --git a/app/models/university/person.rb b/app/models/university/person.rb index b2d74f3b2..cea1bad99 100644 --- a/app/models/university/person.rb +++ b/app/models/university/person.rb @@ -117,6 +117,7 @@ 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_program, -> (program_id) { } # TODO @Sebou scope :for_search_term, -> (term) { where(" unaccent(concat(university_people.first_name, ' ', university_people.last_name)) ILIKE unaccent(:term) OR diff --git a/app/models/university/person/teacher.rb b/app/models/university/person/teacher.rb index 37140ba7b..0f83e630a 100644 --- a/app/models/university/person/teacher.rb +++ b/app/models/university/person/teacher.rb @@ -38,6 +38,7 @@ # fk_rails_da35e70d61 (university_id => universities.id) # class University::Person::Teacher < University::Person + def self.polymorphic_name 'University::Person::Teacher' end diff --git a/app/services/filters/admin/communication/website/posts.rb b/app/services/filters/admin/communication/website/posts.rb index afa833c2a..c5cfe1fe5 100644 --- a/app/services/filters/admin/communication/website/posts.rb +++ b/app/services/filters/admin/communication/website/posts.rb @@ -1,8 +1,11 @@ module Filters class Admin::Communication::Website::Posts < Filters::Base - def initialize(user) - super + def initialize(user, website) + super(user) 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 + end end end diff --git a/app/services/filters/admin/education/schools.rb b/app/services/filters/admin/education/schools.rb index 04eaf065f..3e0e5dfee 100644 --- a/app/services/filters/admin/education/schools.rb +++ b/app/services/filters/admin/education/schools.rb @@ -3,6 +3,7 @@ module Filters def initialize(user) super add_search + add :for_program, user.university.education_programs, I18n.t('filters.attributes.element', element: I18n.t('activerecord.models.education/program.one').downcase), false, true end end end diff --git a/app/services/filters/admin/education/teachers.rb b/app/services/filters/admin/education/teachers.rb index d76303d3d..baa883f7b 100644 --- a/app/services/filters/admin/education/teachers.rb +++ b/app/services/filters/admin/education/teachers.rb @@ -3,6 +3,7 @@ module Filters def initialize(user) super add_search + add :for_program, user.university.education_programs, I18n.t('filters.attributes.element', element: I18n.t('activerecord.models.education/program.one').downcase), false, true end end end diff --git a/app/services/filters/base.rb b/app/services/filters/base.rb index b3a1a5674..8e053d762 100644 --- a/app/services/filters/base.rb +++ b/app/services/filters/base.rb @@ -9,12 +9,13 @@ module Filters protected - def add(scope_name, choices, label, multiple = false) + def add(scope_name, choices, label, multiple = false, tree = false) @list << { scope_name: scope_name, choices: choices, label: label, - multiple: multiple + multiple: multiple, + tree: tree } end diff --git a/app/views/admin/application/_filters.html.erb b/app/views/admin/application/_filters.html.erb index 4c2238c74..baf3ddc07 100644 --- a/app/views/admin/application/_filters.html.erb +++ b/app/views/admin/application/_filters.html.erb @@ -25,7 +25,11 @@ filters.each { |filter| should_be_open = true if params.has_key?(filter[:scope_n <% if filter[:scope_name] == :for_search_term %> <%= text_field_tag filter[:scope_name], params[filter[:scope_name]], placeholder: filter[:label], class: 'form-control mb-1 filter' %> <% else %> - <% choices = filter[:choices].map { |elmt| elmt.is_a?(String) ? [elmt, elmt] : [elmt.is_a?(Hash) ? elmt[:to_s] : elmt.to_s, elmt[:id]] } %> + <% if filter[:tree] %> + <% choices = collection_tree(filter[:choices]).map { |elmt| [elmt[:label].html_safe, elmt[:id]] } %> + <% else %> + <% choices = filter[:choices].map { |elmt| elmt.is_a?(String) ? [elmt, elmt] : [elmt.is_a?(Hash) ? elmt[:to_s] : elmt.to_s, elmt[:id]] } %> + <% end %> <% field_name = filter[:multiple] ? "#{filter[:scope_name]}[]" : filter[:scope_name] %> <%= select_tag field_name, options_for_select(choices, params[filter[:scope_name]]), include_blank: filter[:label], class: 'form-select mb-1 filter' %> <% end %> diff --git a/config/locales/en.yml b/config/locales/en.yml index 14bb8300d..6d9128aca 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -96,6 +96,7 @@ en: filters: attributes: date: Filter by Date + element: Filter by %{element} kind: Filter by Kind role: Filter by Role buttons: diff --git a/config/locales/fr.yml b/config/locales/fr.yml index 657b393cb..ce7497931 100644 --- a/config/locales/fr.yml +++ b/config/locales/fr.yml @@ -96,6 +96,7 @@ fr: filters: attributes: date: Filtrer par Date + element: Filtrer par %{element} kind: Filtrer par Type role: Filtrer par Rôle buttons: -- GitLab