Skip to content

Commit

Permalink
🐛 Fix "Sort by: Published Date"
Browse files Browse the repository at this point in the history
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:
- notch8/adventist_knapsack#185
  • Loading branch information
sjproctor committed Sep 9, 2024
1 parent aafef75 commit 2a7de90
Showing 1 changed file with 26 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

0 comments on commit 2a7de90

Please sign in to comment.