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

pulibrary / orangelight / 420b8cf6-a128-4696-ba36-9f8360eedfd6

23 Oct 2025 12:32AM UTC coverage: 95.318% (+0.04%) from 95.275%
420b8cf6-a128-4696-ba36-9f8360eedfd6

Pull #5254

circleci

sandbergja
Refactor: move Requestable creation out of Requests::Form

Prior to this commit, one of the Form class' many
responsibilities was to create many of the
Requestable instances needed by the Requests system.
It created four different kinds of Requestable, and
it was hard to trace the logic throughout the Form
class for each one of those four paths.

This commit takes a different approach: introducing
a factory class named RequestablesList, which
determines what type of Requestable to create,
then delegates the creation to one of four smaller
factories: AlmaItemsFactory, ScsbItemsFactory,
NoItemsFactory, and TooManyItemsFactory.
Pull Request #5254: Refactor: move Requestable creation out of Requests::Form

158 of 159 new or added lines in 7 files covered. (99.37%)

1 existing line in 1 file now uncovered.

6148 of 6450 relevant lines covered (95.32%)

1499.2 hits per line

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

97.96
/app/models/requests/form.rb
1
# frozen_string_literal: true
2
require 'faraday'
3✔
3

4
module Requests
3✔
5
  # This class is responsible for assembling the data to display the Requests form
6
  class Form
3✔
7
    attr_reader :system_id, :mfhd, :patron, :requestable, :requestable_unrouted, :holdings, :location, :location_code, :pick_ups
3✔
8
    alias default_pick_ups pick_ups
3✔
9
    delegate :eligible_for_library_services?, to: :patron
3✔
10
    delegate :items, :too_many_items?, to: :requestables_list
3✔
11

12
    include Requests::Bibdata
3✔
13
    include Requests::Scsb
3✔
14

15
    # @option opts [String] :system_id A bib record id or a special collection ID value
16
    # @option opts [Fixnum] :mfhd alma holding id
17
    # @option opts [Patron] :patron current Patron object
18
    def initialize(system_id:, mfhd:, patron: nil)
3✔
19
      @system_id = system_id
285✔
20
      @holdings = JSON.parse(doc[:holdings_1display] || '{}')
285✔
21
      # scsb items are the only ones that come in without a MFHD parameter from the catalog now
22
      # set it for them, because they only ever have one location
23
      @mfhd = mfhd || @holdings.keys.first
285✔
24
      @patron = patron
285✔
25
      @location_code = @holdings[@mfhd]["location_code"] if @holdings[@mfhd].present?
285✔
26
      @location = load_bibdata_location
285✔
27
      @pick_ups = build_pick_ups
285✔
28
      @requestable_unrouted = requestables_list.to_a
285✔
29
      @requestable = route_requests(@requestable_unrouted)
285✔
30
    end
31

32
    delegate :user, to: :patron
3✔
33

34
    def requestable?
3✔
35
      requestable.size.positive?
83✔
36
    end
37

38
    def first_filtered_requestable
3✔
39
      requestable&.first
89✔
40
    end
41

42
    # Does this request object have any available copies?
43
    def any_loanable_copies?
3✔
44
      requestable_unrouted.any? do |requestable|
2,267✔
45
        !(requestable.charged? || (requestable.aeon? || !requestable.circulates? || requestable.partner_holding? || requestable.on_reserve?))
24,754✔
46
      end
47
    end
48

49
    # returns an array of Requests::Requestable objects that can respond to #services with an array of the relevant services
50
    def route_requests(requestable_items)
3✔
51
      requestable_items.map do |requestable|
285✔
52
        Requests::Router.new(requestable:, any_loanable: any_loanable_copies?, patron:).routed_request
2,263✔
53
      end
54
    end
55

56
    def serial?
3✔
57
      doc[:format].present? && doc[:format].include?('Journal')
1✔
58
    end
59

60
    # returns basic metadata for hidden fields on the request form via solr_doc values
61
    # Fields to return all keys are arrays
62
    ## Add more fields here as needed
63
    def hidden_field_metadata
3✔
64
      {
65
        title: doc["title_citation_display"],
82✔
66
        author: doc["author_citation_display"],
67
        isbn: doc["isbn_s"]&.values_at(0),
68
        date: doc["pub_date_display"]
69
      }
70
    end
71

72
    def ill_eligible?
3✔
UNCOV
73
      requestable.any? { |r| r.services.include? 'ill' }
×
74
    end
75

76
    def ctx
3✔
77
      @ctx ||= Requests::SolrOpenUrlContext.new(solr_doc: doc).ctx
559✔
78
    end
79

80
    def doc
3✔
81
      @doc ||= SolrDocument.new(solr_doc(system_id))
1,039✔
82
    end
83

84
    private
3✔
85

86
      def load_bibdata_location
3✔
87
        return if location_code.blank?
285✔
88
        location = get_location_data(location_code)
275✔
89
        location_object = Location.new location
275✔
90
        location[:delivery_locations] = location_object.sort_pick_ups if location_object.delivery_locations.present?
275✔
91
        location
275✔
92
      end
93

94
      def requestables_list
3✔
95
        @requestables_list ||= RequestablesList.new(document: doc, holdings:, mfhd:, location:, patron:)
291✔
96
      end
97
  end
98
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