From 7b340be39b8caae995817947522560a68c631409 Mon Sep 17 00:00:00 2001 From: Alex Zotov Date: Wed, 28 Aug 2024 15:55:09 -0500 Subject: [PATCH] fixes bugs for fields 5xx and 8xx --- app/helpers/catalog_helper.rb | 54 +++++++++++++---- lib/traject/extract_3a_fields.rb | 69 ++++++++-------------- lib/traject/extract_author_addl_display.rb | 7 +-- spec/helpers/catalog_helpers_spec.rb | 38 +++++++++--- 4 files changed, 100 insertions(+), 68 deletions(-) diff --git a/app/helpers/catalog_helper.rb b/app/helpers/catalog_helper.rb index 1bf48140..f872e7a3 100644 --- a/app/helpers/catalog_helper.rb +++ b/app/helpers/catalog_helper.rb @@ -100,17 +100,8 @@ def author_additional_collapse_span(values) end def multilined_links_to_facet_author_addl(values) - ret_vals = values.map do |v| - line_pieces = v.split(' relator: ') - quer_disp = line_pieces[0] - relator = line_pieces.size == 2 ? line_pieces[1] : nil - - ret_line = link_to(quer_disp, ("/?f%5Bauthor_addl_ssim%5D%5B%5D=" + CGI.escape(quer_disp))).to_s - ret_line += ", #{relator}" if relator.present? - ret_line - end - return safe_join(ret_vals, tag.br) if ret_vals.present? - '' + ret_vals = values.map { |v| process_value(v) } + ret_vals.present? ? safe_join(ret_vals, tag.br) : '' end def multilined_links_to_facet_flexible(values, field_name) @@ -131,3 +122,44 @@ def build_title_search_links(record_values, record_formats) end end end + +private + +def process_value(value) + line_pieces = split_value(value) + rest_of_line, relator = process_line_pieces(line_pieces) + quer_disp, prefix = process_rest_of_line(rest_of_line) + + build_line(quer_disp, prefix, relator) +end + +def split_value(value) + value.split(' relator: ') +end + +def process_line_pieces(line_pieces) + rest_of_line = line_pieces[0].split(':') + relator = line_pieces.size == 2 ? content_tag(:span, ", " + line_pieces[1].strip) : nil + [rest_of_line, relator] +end + +def process_rest_of_line(rest_of_line) + if rest_of_line.size == 2 + quer_disp = rest_of_line[1].strip + prefix = content_tag(:span, rest_of_line[0].strip + ": ") + else + quer_disp = rest_of_line[0].strip + prefix = nil + end + [quer_disp, prefix] +end + +def build_line(quer_disp, prefix, relator) + ret_line = prefix ? prefix + content_tag(:a, quer_disp, href: query_href(quer_disp)) : content_tag(:a, quer_disp, href: query_href(quer_disp)) + ret_line += relator if relator.present? + ret_line +end + +def query_href(quer_disp) + "/?f%5Bauthor_addl_ssim%5D%5B%5D=#{CGI.escape(quer_disp)}" +end diff --git a/lib/traject/extract_3a_fields.rb b/lib/traject/extract_3a_fields.rb index 2e0d91a1..d1ce0c34 100644 --- a/lib/traject/extract_3a_fields.rb +++ b/lib/traject/extract_3a_fields.rb @@ -6,14 +6,12 @@ module Extract3aFields def extract_3a_fields(tag) lambda do |record, accumulator| record.each_by_tag(tag) do |field| - three_subfield = field['3'] a_subfield = field['a'] - next unless three_subfield - three_subfield += ':' unless three_subfield.end_with?(':') - note = three_subfield - note += " #{a_subfield}" if a_subfield - accumulator << note + note = extractor_subfield3(field) + note += note.empty? ? a_subfield.to_s : " #{a_subfield}" if a_subfield + + accumulator << note.strip unless note.strip.empty? end end end @@ -21,19 +19,14 @@ def extract_3a_fields(tag) def extract_541_subfields_with_colon lambda do |record, accumulator| record.each_by_tag('541') do |field| - three_subfield = field['3'] c_subfield = field['c'] a_subfield = field['a'] - next unless three_subfield - - three_subfield += ':' unless three_subfield.end_with?(':') + note = extractor_subfield3(field) + note += note.empty? ? a_subfield.to_s : " #{a_subfield}" if a_subfield + note += note.empty? ? c_subfield.to_s : " #{c_subfield}" if c_subfield - note = three_subfield - note += " #{c_subfield}" if c_subfield - note += " #{a_subfield}" if a_subfield - - accumulator << note + accumulator << note.strip unless note.strip.empty? end end end @@ -41,31 +34,16 @@ def extract_541_subfields_with_colon def extract_540_subfields lambda do |record, accumulator| record.each_by_tag('540') do |field| - three_subfield = field['3'] a_subfield = field['a'] f_subfield = field['f'] u_subfield = field['u'] - next unless three_subfield - three_subfield += ':' unless three_subfield.end_with?(':') - note = [three_subfield, a_subfield, f_subfield, u_subfield].compact.join(' ') - accumulator << note - end - end - end - - def extract_7xx_8xx_with_subfield3(tag) - lambda do |record, accumulator| - record.each_by_tag(tag) do |field| - three_subfield = field['3'] - next unless three_subfield - - three_subfield += ':' unless three_subfield.end_with?(':') + note = extractor_subfield3(field) + note += note.empty? ? a_subfield.to_s : " #{a_subfield}" if a_subfield + note += note.empty? ? f_subfield.to_s : " #{f_subfield}" if f_subfield + note += note.empty? ? u_subfield.to_s : " #{u_subfield}" if u_subfield - rest_of_field = field.map { |sf| sf.value.to_s unless sf.code == '3' }.compact.join(' ') - note = "#{three_subfield} #{rest_of_field}" - - accumulator << note + accumulator << note.strip unless note.strip.empty? end end end @@ -73,17 +51,20 @@ def extract_7xx_8xx_with_subfield3(tag) def extract_8xx_with_subfield3(atog) lambda do |record, accumulator| record.fields(['800', '810', '811']).each do |field| - subfield3 = field['3'] - - subfield3 += ':' if subfield3 && !subfield3.end_with?(':') + note = extractor_subfield3(field) subfields_to_extract = title_series_8xx_str(atog).split(':').find { |s| s.start_with?(field.tag) } - other_subfields = field.subfields.select { |sf| subfields_to_extract.include?(sf.code) } - .map(&:value) - .join(' ') - - full_string = subfield3 ? "#{subfield3} #{other_subfields}".strip : other_subfields.strip - accumulator << full_string unless full_string.empty? + subfields_to_extract = subfields_to_extract[3..-1] if subfields_to_extract + other_subfields = field.subfields.select { |sf| subfields_to_extract.include?(sf.code) && sf.code != '3' }.map(&:value).join(' ') + note += note.empty? ? other_subfields : " #{other_subfields}" if other_subfields.present? + accumulator << note.strip unless note.strip.empty? end end end end + +private + +def extractor_subfield3(field) + subfield3 = field['3'] + subfield3 ? subfield3.chomp(':') + ':' : '' +end diff --git a/lib/traject/extract_author_addl_display.rb b/lib/traject/extract_author_addl_display.rb index 4a581874..d92db5ee 100644 --- a/lib/traject/extract_author_addl_display.rb +++ b/lib/traject/extract_author_addl_display.rb @@ -29,11 +29,8 @@ def build_ret_strings(record, field_num, text_fields, relator_field, ret_values) end def gather_subfield_three(field) - subfield_three = field['3'] - return '' unless subfield_three - - subfield_three = subfield_three.end_with?(':') ? subfield_three : "#{subfield_three}:" - "#{subfield_three} " + subfield3 = field['3'] + subfield3 ? subfield3.chomp(':') + ':' : '' end def gather_relator_string(field, relator_field) diff --git a/spec/helpers/catalog_helpers_spec.rb b/spec/helpers/catalog_helpers_spec.rb index 060f307e..47ae679c 100644 --- a/spec/helpers/catalog_helpers_spec.rb +++ b/spec/helpers/catalog_helpers_spec.rb @@ -56,15 +56,25 @@ dupe[:value] = ["Tim Jenkins relator: editor."] dupe end + let(:value_with_prefix) do + dupe = SHOW_PAGE_VALUE.dup + dupe[:value] = ["Prefix: Tim Jenkins"] + dupe + end + let(:value_with_prefix_relator) do + dupe = SHOW_PAGE_VALUE.dup + dupe[:value] = ["Prefix: Tim Jenkins relator: editor."] + dupe + end let(:value_with_6_auth_addl) do dupe = SHOW_PAGE_VALUE.dup dupe[:value] = [ - "Tim Jenkins relator: editor.", - "Sally Jenkins", + "Prefix: Tim Jenkins relator: editor.", + "Prefix: Sally Jenkins", "Betsy Jenkins", "Sal Weitzman relator: ghost writer.", "Mike Birbiglia", - "Tim Conway relator: moral support." + "Tim Conway" ] dupe end @@ -77,19 +87,31 @@ it 'converts a single valued additional author with relator into a facet search hyperlink, leaving relator out of the link' do expect(helper.author_additional_format(value_with_relator)).to eq( - "Tim Jenkins, editor." + "Tim Jenkins, editor." + ) + end + + it 'converts a single valued additional author with prefix and relator into a facet search hyperlink, leaving relator and prefix out of the link' do + expect(helper.author_additional_format(value_with_prefix_relator)).to eq( + "Prefix: Tim Jenkins, editor." + ) + end + + it 'converts a single valued additional author with prefix into a facet search hyperlink, leaving prefix out of the link' do + expect(helper.author_additional_format(value_with_prefix)).to eq( + "Prefix: Tim Jenkins" ) end it 'converts a values array with more than 5 items into a new-lined list with a collapsible after 5' do # rubocop:disable RSpec/ExampleLength expect(helper.author_additional_format(value_with_6_auth_addl)).to eq( - "Tim Jenkins, editor.
" \ - "Sally Jenkins
" \ + "Prefix: Tim Jenkins, editor.
" \ + "Prefix: Sally Jenkins
" \ "Betsy Jenkins
" \ - "Sal Weitzman, ghost writer.
" \ + "Sal Weitzman, ghost writer.
" \ "Mike Birbiglia
" \ "" \ - "Tim Conway, moral support.
" \ + "Tim Conway
" \ "" )