• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pulibrary / orangelight / 4c391e0e-519a-40cb-8ad3-354445f4ce03

12 Aug 2025 08:47PM UTC coverage: 85.348% (-10.0%) from 95.335%
4c391e0e-519a-40cb-8ad3-354445f4ce03

push

circleci

web-flow
[#5143] Use access restriction note as Aeon ItemInfo1 if available (#5173)

With this commit, if a user visits a record with an access
restrictions note and presses the Reading Room Request
button, they will get to an Aeon form with the 'Restrictions'
field pre-filled with the restriction note.

If the record does not have an access restrictions note,
the field will be pre-filled with 'Reading Room Access Only',
as it has been previously.

Closes #5143

5493 of 6436 relevant lines covered (85.35%)

251.82 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

45.36
/app/models/concerns/blacklight/marc/document_extension.rb
1
# frozen_string_literal: true
2

3
require 'marc'
1✔
4
require 'openurl/context_object'
1✔
5

6
# rubocop:disable Metrics/ModuleLength
7
module Blacklight
1✔
8
  module Marc
1✔
9
    module DocumentExtension
1✔
10
      include Blacklight::Marc::DocumentExport
1✔
11
      include OpenURL
1✔
12

13
      # Prepend our overloaded method to bypass bug in Blacklight
14
      # See https://stackoverflow.com/questions/5944278/overriding-method-by-another-defined-in-module
15
      prepend Blacklight::Marc::Document::MarcExportOverride
1✔
16

17
      class UnsupportedMarcFormatType < RuntimeError; end
1✔
18

19
      def self.extended(document)
1✔
20
        Blacklight::Solr::Document::MarcExport.register_export_formats(document)
719✔
21
      end
22

23
      # Accesses the MARC::Record constructed from data retrieved over the HTTP
24
      # @return [MARC::Record]
25
      def to_marc
1✔
26
        @_ruby_marc_obj ||= load_marc
×
27
      end
28

29
      # These are registered by default
30
      # @see Blacklight::Solr::Document::MarcExport.register_export_formats
31

32
      # Generate the string-serialized XML from the remote MARC record
33
      # @see Blacklight::Solr::Document::MarcExport#export_as_marcxml
34
      # @return [String]
35
      def export_as_marcxml
1✔
36
        return '' unless to_marc
×
37
        super
×
38
      end
39

40
      # @see Blacklight::Solr::Document::MarcExport#export_as_marc
41
      # @return [String]
42
      def export_as_marc
1✔
43
        return '' unless to_marc
×
44
        super
×
45
      end
46

47
      # @see Blacklight::Solr::Document::MarcExport#export_as_openurl_ctx_kev
48
      # @return [String]
49
      def export_as_openurl_ctx_kev(format = nil)
1✔
50
        ctx = to_ctx(format)
193✔
51
        # send back the encoded string
52
        ctx.kev
193✔
53
      end
54

55
      # Generate the refworks citation format from the remote MARC record
56
      # @see Blacklight::Solr::Document::MarcExport#export_as_refworks_marc_txt
57
      # @return [String]
58
      def export_as_refworks_marc_txt
1✔
59
        return '' unless to_marc
×
60
        super
×
61
      end
62

63
      # These are not registered by default, but still provided as public methods
64

65
      # @see Blacklight::Solr::Document::MarcExport#export_as_apa_citation_txt
66
      # @return [String]
67
      def export_as_apa_citation_txt
1✔
68
        return '' unless to_marc
×
69
        super
×
70
      end
71

72
      # @see Blacklight::Solr::Document::MarcExport#export_as_mla_citation_txt
73
      # @return [String]
74
      def export_as_mla_citation_txt
1✔
75
        return '' unless to_marc
×
76
        super
×
77
      end
78

79
      # @see Blacklight::Solr::Document::MarcExport#export_as_chicago_citation_txt
80
      # @return [String]
81
      def export_as_chicago_citation_txt
1✔
82
        return '' unless to_marc
×
83
        super
×
84
      end
85

86
      # @see Blacklight::Solr::Document::MarcExport#export_as_endnote
87
      # @return [String]
88
      def export_as_endnote
1✔
89
        return '' unless to_marc
×
90
        super
×
91
      end
92

93
      # return openurl ctx object
94
      def to_ctx(format = nil)
1✔
95
        @_ctx || build_ctx(format)
486✔
96
      end
97

98
      # We allow the user to retry in very specific scenarios.
99
      def can_retry?
1✔
100
        @can_retry
×
101
      end
102

103
        protected
1✔
104

105
          def build_ctx(format = nil)
1✔
106
            format ||= first('format')&.downcase
486✔
107
            unless format.nil?
486✔
108
              format = format.is_a?(Array) ? format[0].downcase.strip : format.downcase.strip
474✔
109
            end
110
            if format == 'book'
486✔
111
              BookCtxBuilder.new(document: self, format:).build
314✔
112
            elsif /journal/i.match?(format) # checking using include because institutions may use formats like Journal or Journal/Magazine
172✔
113
              JournalCtxBuilder.new(document: self, format:).build
82✔
114
            else
115
              CtxBuilder.new(document: self, format:).build
90✔
116
            end
117
          end
118

119
          # Retrieves the bib. ID from the Solr Document
120
          # @return [String]
121
          def marc_source
1✔
122
            @_marc_source ||= fetch(_marc_source_field)
×
123
          end
124

125
          # Retrieve the MARC 21 bitstream over the HTTP
126
          # @return [MARC::Record]
127
          def marc_record_from_marc21
1✔
128
            return if marc_source.blank?
×
129
            MARC::Record.new_from_marc marc_source
×
130
          end
131

132
          # Retrieve the MARC JSON over the HTTP
133
          # @return [MARC::Record]
134
          def marc_record_from_json
1✔
135
            return if marc_source.blank?
×
136

137
            begin
138
              marc_json = JSON.parse(marc_source)
×
139
            rescue JSON::ParserError => json_error
140
              Rails.logger.error "#{self.class}: Failed to parse the MARC JSON: #{json_error}"
×
141
              return
×
142
            end
143
            MARC::Record.new_from_hash marc_json
×
144
          end
145

146
          # Construct a MARC::Record using MARC record data retrieved over the HTTP
147
          # @return [MARC::Record]
148
          def load_marc
1✔
149
            marc_format = _marc_format_type.to_s
×
150

151
            case marc_format
×
152
            when 'marcxml'
153
              marc_record_from_marcxml
×
154
            when 'marc21'
155
              marc_record_from_marc21
×
156
            when 'json'
157
              marc_record_from_json
×
158
            else
159
              raise UnsupportedMarcFormatType, "Only marcxml, marc21, and json are supported, this documents format is #{_marc_format_type} and the current extension parameters are #{self.class.extension_parameters.inspect}"
×
160
            end
161
          rescue StandardError => e
162
            Rails.logger.error("Blacklight failed to parse MARC record. Exception was: #{e}")
×
163
            nil
×
164
          end
165

166
          # Construct a MARC::Record using MARC-XML data retrieved over the HTTP
167
          # @return [MARC::Record]
168
          def marc_record_from_marcxml
1✔
169
            id = fetch(_marc_source_field)
×
170

171
            response = Faraday.get("#{Requests.config['bibdata_base']}/bibliographic/#{id}")
×
172
            @can_retry = response.status == 429
×
173
            response_stream = StringIO.new(response.body)
×
174
            marc_reader = ::MARC::XMLReader.new(response_stream)
×
175
            marc_records = marc_reader.to_a
×
176
            marc_records.first
×
177
          end
178

179
          def _marc_helper
1✔
180
            @_marc_helper ||= Blacklight::Marc::Document.new fetch(_marc_source_field), _marc_format_type
×
181
          end
182

183
          def _marc_source_field
1✔
184
            self.class.extension_parameters[:marc_source_field]
×
185
          end
186

187
          def _marc_format_type
1✔
188
            # TODO: Raise if not present
189
            self.class.extension_parameters[:marc_format_type]
×
190
          end
191

192
          # Overwites the get_author_list(record) method from the module Blacklight::Solr::Document::MarcExport
193
          def get_author_list(record)
1✔
194
            author_list = []
×
195
            authors_primary = record.find { |f| f.tag == '100' }
×
196
            begin
197
              author_primary = authors_primary.find { |s| s.code == 'a' }.value unless authors_primary.nil?
×
198
            rescue StandardError
199
              ''
×
200
            end
201
            author_list.push(clean_end_punctuation(author_primary)) unless author_primary.nil?
×
202
            authors_secondary = record.find_all { |f| f.tag == '700' }
×
203
            authors_secondary&.each do |l|
×
204
              unless l.find { |s| s.code == 'a' }.nil?
×
205
                author_list.push(clean_end_punctuation(l.find { |s| s.code == 'a' }.value)) unless l.find { |s| s.code == 'a' }.value.nil?
×
206
              end
207
            end
208
            author_list.uniq!
×
209
            author_list
×
210
          end
211
    end
212
    # rubocop:enable Metrics/ModuleLength
213
  end
214
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc