Skip to content

Commit

Permalink
Merge pull request #2316 from samvera/fixes-sort-by-actions
Browse files Browse the repository at this point in the history
🐛 Fix "Sort by: Published Date"
  • Loading branch information
sjproctor authored Sep 10, 2024
2 parents 02262aa + 2846f44 commit ca5f702
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
32 changes: 26 additions & 6 deletions app/indexers/concerns/hyku_indexing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,44 @@ 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?

SolrDocument.find(file_set_id)['all_text_tsimv']
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
2 changes: 2 additions & 0 deletions app/models/sort_title.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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:]])/, '')
Expand Down

0 comments on commit ca5f702

Please sign in to comment.