diff --git a/app/controllers/admin/communication/website/posts_controller.rb b/app/controllers/admin/communication/website/posts_controller.rb index aed1f3de8e8270e59428f58e1251b6b532a02b4b..2b33de83116442e0e881d787d76e811ad6627898 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 f4c52f96a99855b7ca9cce5a329dbcaec97a932b..37ea835aad3d3c19d2ea1e3a39f5d08872899d1e 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 49e506997ff691bb3a96ce5d2ce4fca561f11242..c24b805575eb5016d6607c76282fe09662793922 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 fdfbb5bab8aef23c5b33ac762ff2fce142d75c77..663bf2e000cbb3d821a31676304e6104bfb5ab24 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 b2d74f3b246caa05e0f5f8074cb1b78012f4eed1..cea1bad99553ecd60eea4b1ff0303bd79664fa8a 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 37140ba7b1d670cda457627f9b6feeccffbceb18..0f83e630a46a59a4431fdfeb88b3221d72932427 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 afa833c2a12c31746b5e4cd01ebcea47963d5b96..c5cfe1fe5e01c4889df85b0c58e55125bc8d49eb 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 04eaf065fc58d2a05e27e7122c92257b534aabec..3e0e5dfeef8d04e761b566f8184046eb46da34db 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 d76303d3dc56605a7a3196838e56824511d3abec..baa883f7b9c54d1c0c6f596fde610678cf3abeaa 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 b3a1a56740a09939a8ee0d7e4b53c4dcf6d2b52d..8e053d762e5c94fe350b622b3771c37405eee181 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 4c2238c74afe79c882e0eee50241274c4f0e37a9..baf3ddc0700ecdb16809352f74726028a153e7b2 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 14bb8300d6598cf0628f124ff26d027b135e6a7e..6d9128aca701db5f2b89b871a73482cc2116ec53 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 657b393cba75a98e3a563139d13a30892911a29b..ce749793162a5b13cb214707753688e744513c1f 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: