From 2a7de90ac9d68b8dc2d48854ae8dec014c2c45b9 Mon Sep 17 00:00:00 2001 From: Sarah Proctor Date: Mon, 9 Sep 2024 10:39:30 -0700 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=90=9B=20Fix=20"Sort=20by:=20Publishe?= =?UTF-8?q?d=20Date"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit fixes a bug in the ability to sort works by "Published date", "Upload date", and "Title" in the catalog search page and the collection show page. Ref: - https://github.com/scientist-softserv/adventist_knapsack/issues/185 --- app/indexers/concerns/hyku_indexing.rb | 32 +++++++++++++++++++++----- 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/app/indexers/concerns/hyku_indexing.rb b/app/indexers/concerns/hyku_indexing.rb index c0c0d8104..41a834a90 100644 --- a/app/indexers/concerns/hyku_indexing.rb +++ b/app/indexers/concerns/hyku_indexing.rb @@ -24,11 +24,20 @@ module HykuIndexing # TODO: Reinstate once valkyrie fileset work is complete - https://github.com/scientist-softserv/hykuup_knapsack/issues/34 solr_doc['all_text_tsimv'] = full_text(object.file_sets.first&.id) if object.kind_of?(ActiveFedora::Base) # rubocop:enable Style/ClassCheck + solr_doc['title_ssim'] = SortTitle.new(object.title.first).alphabetical + solr_doc['depositor_ssi'] = object.depositor + solr_doc['creator_ssim'] = object.creator&.first + if object.respond_to?(:date_created) + solr_doc[CatalogController.created_field] = + Array(object.date_created).first + end add_date(solr_doc) end end end + private + def full_text(file_set_id) return if !Flipflop.default_pdf_viewer? || file_set_id.blank? @@ -36,12 +45,23 @@ def full_text(file_set_id) end def add_date(solr_doc) - # The allowed date formats are either YYYY, YYYY-MM, or YYYY-MM-DD - # the date must be formatted as a 4 digit year in order to be sorted. - valid_date_formats = /\A(\d{4})(?:-\d{2}(?:-\d{2})?)?\z/ date_string = solr_doc['date_created_tesim']&.first - year = date_string&.match(valid_date_formats)&.captures&.first - solr_doc['date_tesi'] = year if year - solr_doc['date_ssi'] = year if year + return unless date_string + + date_string = pad_date_with_zero(date_string) if date_string.include?('-') + + # The allowed date formats are either YYYY, YYYY-MM, or YYYY-MM-DD + valid_date_formats = /\A(\d{4}(?:-\d{2}(?:-\d{2})?)?)\z/ + date = date_string&.match(valid_date_formats)&.captures&.first + + # If the date is not in the correct format, index the original date string + date ||= date_string + + solr_doc['date_tesi'] = date if date + solr_doc['date_ssi'] = date if date + end + + def pad_date_with_zero(date_string) + date_string.split('-').map { |d| d.rjust(2, '0') }.join('-') end end From 2846f44763b0c1c89e05afe89b18e7d776277074 Mon Sep 17 00:00:00 2001 From: Sarah Proctor Date: Mon, 9 Sep 2024 14:57:38 -0700 Subject: [PATCH 2/2] =?UTF-8?q?=E2=9C=85=20Adds=20a=20guard=20clause=20to?= =?UTF-8?q?=20fix=20failing=20tests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit adds a guard clause to the `SortTitle` model to fix failing tests due to a nil value being assigned to `@title` on initialization. Ref: - https://github.com/scientist-softserv/adventist_knapsack/issues/185 --- app/models/sort_title.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/models/sort_title.rb b/app/models/sort_title.rb index 55ff31337..acc79b939 100644 --- a/app/models/sort_title.rb +++ b/app/models/sort_title.rb @@ -6,6 +6,8 @@ def initialize(title) end def alphabetical + return if @title.blank? + title = @title.downcase title = title.gsub(/^an(?:[[:space:]])/, '') .gsub(/^a(?:[[:space:]])/, '')