diff --git a/lib/models/subject_item.rb b/lib/models/subject_item.rb index 76d134c..3a6061a 100644 --- a/lib/models/subject_item.rb +++ b/lib/models/subject_item.rb @@ -1,7 +1,8 @@ class SubjectItem attr_reader :browse_doc def self.for(browse_doc:, exact_match:) - if browse_doc.key?("broader") || browse_doc.key?("narrower") || browse_doc.key?("see_also") + if ["broader", "narrower", "see_also", "see_instead"] + .any? { |xref_kind| browse_doc.key?(xref_kind) } SubjectItemWithCrossReferences.new(browse_doc: browse_doc, exact_match: exact_match) else SubjectItem.new(browse_doc: browse_doc, exact_match: exact_match) @@ -66,11 +67,13 @@ def has_cross_references? end def cross_references - # see_instead because this still needs to be changed in solr - broader = BroaderTerms.new(@browse_doc["broader"]) - narrower = NarrowerTerms.new(@browse_doc["narrower"]) - see_also = SeeAlsoTerms.new(@browse_doc["see_also"]) - OpenStruct.new(broader: broader, narrower: narrower, see_also: see_also) + xrefs = {} + [:broader, :narrower, :see_also, :see_instead].each do |xref| + # get the terms class from the xref name (example: BroaderTerms) + terms_klass = Module.const_get "#{xref.to_s.split("_").collect(&:capitalize).join}Terms" + xrefs[xref] = terms_klass.new(@browse_doc[xref.to_s]) + end + OpenStruct.new(**xrefs) end end @@ -148,6 +151,20 @@ def summary_text_open end end +class SeeInsteadTerms < SubjectItemCrossReferences + def text + "See instead" + end + + def summary_text_closed + summary_text(true, "see instead") + end + + def summary_text_open + summary_text(false, "see instead") + end +end + class SubjectItemCrossReference attr_reader :subject, :count def initialize(subject) diff --git a/spec/fixtures/subject_results.json b/spec/fixtures/subject_results.json index f997fae..f524899 100644 --- a/spec/fixtures/subject_results.json +++ b/spec/fixtures/subject_results.json @@ -34,6 +34,14 @@ "browse_field":"subject", "json":"{\"id\":\"http://id.loc.gov/authorities/subjects/sh85000887\",\"subject\":\"Adivasis\",\"narrower\":{},\"broader\":{},\"see_also\":{\"http://id.loc.gov/authorities/subjects/sh85090174\":{\"id\":\"http://id.loc.gov/authorities/subjects/sh85090174\",\"label\":\"Indigenous peoples\",\"count\":441,\"match_text\":\"indigenous peoples\",\"json_class\":\"AuthorityBrowse::GenericXRef\"}},\"incoming_see_also\":null}", "_version_":1757646983890206728, - "date_of_index":"2023-02-12T00:00:00Z"} - ] + "date_of_index":"2023-02-12T00:00:00Z"}, + { + "id":"illegal aliens\u001fsubject", + "loc_id":"http://id.loc.gov/authorities/subjects/sh85003553", + "browse_field":"subject", + "term":"Illegal aliens", + "count":0, + "date_of_index":"2024-05-22T00:00:00Z", + "see_instead":["Undocumented immigrants||250"], + "_version_":1799776250073448465}] }} diff --git a/spec/models/subject_item_spec.rb b/spec/models/subject_item_spec.rb index d4f80f0..c2f874c 100644 --- a/spec/models/subject_item_spec.rb +++ b/spec/models/subject_item_spec.rb @@ -208,4 +208,36 @@ expect(see_also.first.url).to include("subject?query=") end end + context "#cross_references.see_instead" do + before(:each) do + @params = { + browse_doc: @items[2], + exact_match: false + } + end + subject do + described_class.new(**@params) + end + let :see_instead do + subject.cross_references.see_instead.leading + end + it "has at least one cross reference of 'see_instead'" do + expect(see_instead).not_to be_nil + end + it "has a false 'heading_link?'" do + expect(see_instead.first.heading_link?).to eq(false) + end + it "has a subject_display" do + expect(see_instead.first.subject_display).to eq("Undocumented immigrants") + end + it "has a count" do + expect(see_instead.first.count).to eq("250") + end + it "displays records" do + expect(see_instead.first.record_text).to eq("250 records") + end + it "has a url that's a subject query" do + expect(see_instead.first.url).to include("subject?query=") + end + end end