From 5d7f65c3286d3ce731d6a23527895c76a9b7eeff Mon Sep 17 00:00:00 2001 From: Arnaud Levy <arnaud.levy@noesya.coop> Date: Mon, 18 Nov 2024 16:47:27 +0100 Subject: [PATCH] Facettes dans l'extranet (#2415) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * list * shorter * simpler * better * tweaks * clean add_scope taxonomy * fix search extranet document * fix scope taxonomy * fix when cohort.program.diploma is nil --------- Co-authored-by: Sébastien Gaya <sebastien.gaya@gmail.com> --- .../alumni/organizations_controller.rb | 4 +- .../extranet/alumni/persons_controller.rb | 5 +- app/models/communication/extranet/document.rb | 8 ++ .../communication/extranet/document/facets.rb | 2 + app/models/university/organization/facets.rb | 26 +++++- .../university/person/alumnus/facets.rb | 35 ++++++-- app/models/university/person/category.rb | 3 +- .../faceted_search/facets/taxonomy.rb | 83 +++++++++++++++++++ app/services/faceted_search/facets/text.rb | 15 ++++ .../extranet/alumni/cohorts/show.html.erb | 9 +- .../extranet/alumni/persons/show.html.erb | 6 +- .../facets/taxonomy/_all.html.erb | 20 +++++ .../facets/taxonomy/_selected.html.erb | 13 +++ 13 files changed, 212 insertions(+), 17 deletions(-) create mode 100644 app/services/faceted_search/facets/taxonomy.rb create mode 100644 app/services/faceted_search/facets/text.rb create mode 100644 app/views/faceted_search/facets/taxonomy/_all.html.erb create mode 100644 app/views/faceted_search/facets/taxonomy/_selected.html.erb diff --git a/app/controllers/extranet/alumni/organizations_controller.rb b/app/controllers/extranet/alumni/organizations_controller.rb index 19ecddd17..c13e2c361 100644 --- a/app/controllers/extranet/alumni/organizations_controller.rb +++ b/app/controllers/extranet/alumni/organizations_controller.rb @@ -2,7 +2,9 @@ class Extranet::Alumni::OrganizationsController < Extranet::Alumni::ApplicationC def index @facets = University::Organization::Facets.new params[:facets], { model: about&.university_person_alumni_organizations, - about: about + about: about, + language: current_language, + categories: current_university.organization_categories } @organizations = @facets.results .ordered(current_language) diff --git a/app/controllers/extranet/alumni/persons_controller.rb b/app/controllers/extranet/alumni/persons_controller.rb index 7b73844dd..2c37e2c32 100644 --- a/app/controllers/extranet/alumni/persons_controller.rb +++ b/app/controllers/extranet/alumni/persons_controller.rb @@ -3,13 +3,14 @@ class Extranet::Alumni::PersonsController < Extranet::Alumni::ApplicationControl @facets = University::Person::Alumnus::Facets.new params[:facets], { model: about&.university_person_alumni, about: about, - language: current_language + language: current_language, + categories: current_university.person_categories } + @count = @facets.results.count @people = @facets.results .ordered(current_language) .page(params[:page]) .per(72) - @count = @people.total_count breadcrumb end diff --git a/app/models/communication/extranet/document.rb b/app/models/communication/extranet/document.rb index 281c659ad..4979fea5f 100644 --- a/app/models/communication/extranet/document.rb +++ b/app/models/communication/extranet/document.rb @@ -36,4 +36,12 @@ class Communication::Extranet::Document < ApplicationRecord validates :category, :kind, presence: true + scope :for_search_term, -> (term, language) { + joins(:localizations) + .where(communication_extranet_document_localizations: { language_id: language.id }) + .where(" + unaccent(communication_extranet_document_localizations.name) ILIKE unaccent(:term) + ", term: "%#{sanitize_sql_like(term)}%") + } + end diff --git a/app/models/communication/extranet/document/facets.rb b/app/models/communication/extranet/document/facets.rb index 96d9ac46d..a80787692 100644 --- a/app/models/communication/extranet/document/facets.rb +++ b/app/models/communication/extranet/document/facets.rb @@ -1,4 +1,6 @@ class Communication::Extranet::Document::Facets < FacetedSearch::Facets + attr_reader :language + def initialize(params, extranet, language) super(params) diff --git a/app/models/university/organization/facets.rb b/app/models/university/organization/facets.rb index 1c6a3e806..e77716642 100644 --- a/app/models/university/organization/facets.rb +++ b/app/models/university/organization/facets.rb @@ -1,13 +1,31 @@ class University::Organization::Facets < FacetedSearch::Facets + attr_reader :language, :categories + def initialize(params, options) - super params + super params + @model = options[:model] + @about = options[:about] + @language = options[:language] + @categories = options[:categories] + add_name + add_taxonomies + end - @model = options[:model] - @about = options[:about] + protected + def add_name filter_with_text :name, { title: University::Organization::Localization.human_attribute_name('name') } - + end + + def add_taxonomies + categories.taxonomies.each do |taxonomy| + taxonomy_l10n = taxonomy.localization_for(language) + next if taxonomy_l10n.nil? + add_facet FacetedSearch::Facets::Taxonomy, taxonomy_l10n.slug, { + l10n: taxonomy_l10n + } + end end end diff --git a/app/models/university/person/alumnus/facets.rb b/app/models/university/person/alumnus/facets.rb index 3b1d123bf..124e79dd2 100644 --- a/app/models/university/person/alumnus/facets.rb +++ b/app/models/university/person/alumnus/facets.rb @@ -1,26 +1,51 @@ class University::Person::Alumnus::Facets < FacetedSearch::Facets + attr_reader :language, :categories + def initialize(params, options) - super params + super params + @model = options[:model] + @about = options[:about] + @language = options[:language] + @categories = options[:categories] + add_name + add_years + add_programs + add_taxonomies + end - @model = options[:model] - @about = options[:about] - @language = options[:language] + protected + def add_name filter_with_text :name, { title: University::Person::Localization.human_attribute_name('name') } + end + def add_years filter_with_list :diploma_years, { source: @about.academic_years.ordered, title: Education::AcademicYear.model_name.human(count: 2), habtm: true } + end + def add_programs + return if @about.is_a? Education::Program filter_with_checkboxes :diploma_programs, { source: @about.programs.ordered(@language), title: Education::Program.model_name.human(count: 2), display_method: Proc.new { |program| program.to_s_in(@language) }, habtm: true - } unless @about.is_a? Education::Program + } + end + + def add_taxonomies + categories.taxonomies.each do |taxonomy| + taxonomy_l10n = taxonomy.localization_for(language) + next if taxonomy_l10n.nil? + add_facet FacetedSearch::Facets::Taxonomy, taxonomy_l10n.slug, { + l10n: taxonomy_l10n + } + end end end diff --git a/app/models/university/person/category.rb b/app/models/university/person/category.rb index dbba705ee..a36bfad67 100644 --- a/app/models/university/person/category.rb +++ b/app/models/university/person/category.rb @@ -26,9 +26,10 @@ class University::Person::Category < ApplicationRecord include Localizable include WithUniversity - has_and_belongs_to_many :people, + has_and_belongs_to_many :university_people, class_name: 'University::Person', join_table: :university_people_categories + alias :people :university_people def dependencies localizations diff --git a/app/services/faceted_search/facets/taxonomy.rb b/app/services/faceted_search/facets/taxonomy.rb new file mode 100644 index 000000000..c413da0ae --- /dev/null +++ b/app/services/faceted_search/facets/taxonomy.rb @@ -0,0 +1,83 @@ +module FacetedSearch + class Facets::Taxonomy < Facets::List + attr_reader :l10n, :taxonomy, :language, :university + + def initialize(name, params, facets, options) + @name = name + @params = params + @facets = facets + @options = options + @l10n = options[:l10n] + @taxonomy = @l10n.about + @language = @l10n.language + @university = @taxonomy.university + end + + def title + l10n.to_s + end + + def values + descendants.ordered(language) + end + + # INNER JOIN "university_organizations_categories" "university_organizations_categories_01234567-89ab-cdef-0123-4567890abcde" + # ON "university_organizations_categories_01234567-89ab-cdef-0123-4567890abcde"."organization_id" = "university_organizations"."id" + # WHERE "university_organizations_categories_01234567-89ab-cdef-0123-4567890abcde"."category_id" IN (?) + def add_scope(scope) + return scope if params_array.blank? + + scope + .joins(" + INNER JOIN \"#{association_join_table_name}\" \"#{association_join_table_name_alias}\" + ON \"#{association_join_table_name_alias}\".\"#{join_table_foreign_key}\" = \"#{table_name}\".\"id\" + ") + .where( + "\"#{association_join_table_name_alias}\".\"#{join_table_association_foreign_key}\" IN (?)", + params_array + ) + end + + protected + + def categories_class + taxonomy.class + end + + # university_organizations + def table_name + @facets.model.klass.table_name + end + + def habtm_reflection + @association_reflection ||= @facets.model.klass._reflections[:categories].parent_reflection + end + + # organization_id + def join_table_foreign_key + @join_table_foreign_key ||= habtm_reflection.foreign_key + end + + # category_id + def join_table_association_foreign_key + @join_table_association_foreign_key ||= habtm_reflection.association_foreign_key + end + + # university_organizations_categories + def association_join_table_name + @association_join_table_name ||= habtm_reflection.join_table + end + + # university_organizations_categories_01234567-89ab-cdef-0123-4567890abcde + def association_join_table_name_alias + @association_join_table_name_alias ||= "#{association_join_table_name}_#{taxonomy.id}" + end + + def descendants + categories_class.where( + university: university, + id: taxonomy.descendants.pluck(:id) + ) + end + end +end \ No newline at end of file diff --git a/app/services/faceted_search/facets/text.rb b/app/services/faceted_search/facets/text.rb new file mode 100644 index 000000000..bad0c0bdb --- /dev/null +++ b/app/services/faceted_search/facets/text.rb @@ -0,0 +1,15 @@ +module FacetedSearch + class Facets::Text < Facets::Default + include ActiveRecord::Sanitization + + def placeholder + @options[:placeholder] + end + + def add_scope(scope) + return scope if params.blank? + language = facets.language + scope.for_search_term(params, language) + end + end +end \ No newline at end of file diff --git a/app/views/extranet/alumni/cohorts/show.html.erb b/app/views/extranet/alumni/cohorts/show.html.erb index 99270e2d4..8e88372f0 100644 --- a/app/views/extranet/alumni/cohorts/show.html.erb +++ b/app/views/extranet/alumni/cohorts/show.html.erb @@ -6,9 +6,14 @@ </p> <% end %> +<% +subtitle_parts = [] +subtitle_parts << @cohort.program.diploma.to_s_in(current_language) if @cohort.program.diploma.present? +subtitle_parts << @cohort.program.to_s_in(current_language) +%> + <p class="mb-5"> - <%= @cohort.program.diploma.to_s_in(current_language) %> - — <%= @cohort.program.to_s_in(current_language) %> + <%= subtitle_parts.join(' — ') %> </p> <%= render 'extranet/alumni/persons/list', people: @cohort.people %> diff --git a/app/views/extranet/alumni/persons/show.html.erb b/app/views/extranet/alumni/persons/show.html.erb index 39599b1dd..a4cd93caf 100644 --- a/app/views/extranet/alumni/persons/show.html.erb +++ b/app/views/extranet/alumni/persons/show.html.erb @@ -18,7 +18,7 @@ <% @person&.cohorts&.each do |cohort| %> <% program_l10n = cohort.program.best_localization_for(current_language) - diploma_l10n = cohort.program.diploma.best_localization_for(current_language) + diploma_l10n = cohort.program.diploma.best_localization_for(current_language) if cohort.program.diploma.present? school_l10n = cohort.school.best_localization_for(current_language) %> <li class="experiences__experience py-4 border-top"> @@ -27,7 +27,9 @@ <p class="mb-0"> <%= link_to [:alumni, cohort] do %> <b><%= program_l10n %></b><br> - <%= diploma_l10n %><br> + <% if diploma_l10n.present? %> + <%= diploma_l10n %><br> + <% end %> <%= cohort.year %> <% end %> </p> diff --git a/app/views/faceted_search/facets/taxonomy/_all.html.erb b/app/views/faceted_search/facets/taxonomy/_all.html.erb new file mode 100644 index 000000000..9b01cfde9 --- /dev/null +++ b/app/views/faceted_search/facets/taxonomy/_all.html.erb @@ -0,0 +1,20 @@ +<% anchor ||= '' %> +<% if facet.values.any? %> + <li> + <% unless facet.title.blank? %><b><%= facet.title %></b><% end %> + <ol class="faceted__facet__list list-unstyled"> + <% facet.values.each do |taxon| %> + <% + identifier = taxon.id + display_value = taxon.to_s_in(current_language) + %> + <li class="faceted__facet__list__value <%= 'faceted__facet__list__value--selected' if facet.value_selected?(identifier) %>"> + <%= render 'faceted_search/link', + display_value: display_value, + path: facet.facets.path_for(facet, identifier) + anchor, + searchable: facet.searchable %> + </li> + <% end %> + </ol> + </li> +<% end %> \ No newline at end of file diff --git a/app/views/faceted_search/facets/taxonomy/_selected.html.erb b/app/views/faceted_search/facets/taxonomy/_selected.html.erb new file mode 100644 index 000000000..46ddb10a2 --- /dev/null +++ b/app/views/faceted_search/facets/taxonomy/_selected.html.erb @@ -0,0 +1,13 @@ +<% anchor ||= '' %> +<% if facet.values.any? %> + <% facet.values.each do |taxon| %> + <% identifier = taxon.id %> + <% if facet.value_selected?(identifier) %> + <% display_value = taxon.to_s_in(current_language) %> + <%= render 'faceted_search/facets/facet-selected', + title: facet.title, + value: display_value, + path: facet.facets.path_for(facet, identifier) + anchor %> + <% end %> + <% end %> +<% end %> \ No newline at end of file -- GitLab