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

pulibrary / orangelight / f4578fe2-2744-4dff-8d1a-1d809b5bf11a

pending completion
f4578fe2-2744-4dff-8d1a-1d809b5bf11a

Pull #3544

circleci

Max Kadel
Restore file to orig state
Pull Request #3544: Refactor stub for availability for tests

5438 of 5710 relevant lines covered (95.24%)

1445.21 hits per line

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

88.52
/app/helpers/requests/application_helper.rb
1
# frozen_string_literal: true
2
# rubocop:disable Metrics/ModuleLength
3
module Requests
3✔
4
  module ApplicationHelper
3✔
5
    def format_email(email)
3✔
6
      email&.downcase
176✔
7
    end
8

9
    def format_label(key)
3✔
10
      label = key.to_s
×
11
      human_label = label.tr('_', ' ')
×
12
      formatted = human_label.split.map(&:capitalize).join(' ')
×
13
      formatted
×
14
    end
15

16
    def error_key_format(key)
3✔
17
      keys_to_ignore = ['items']
×
18
      format_label(key) unless keys_to_ignore.include? key.to_s
×
19
    end
20

21
    # array of error_keys
22
    def guest_user_error?(error_keys)
3✔
23
      user_errors = [:email, :user_name, :barcode]
×
24
      error_keys.any? { |item| user_errors.include? item }
×
25
    end
26

27
    def show_pick_up_service_options(requestable, mfhd_id)
3✔
28
      if requestable.on_shelf?
131✔
29
        display_on_shelf(requestable, mfhd_id)
52✔
30
      else
31
        display_requestable_list(requestable)
77✔
32
      end
33
    end
34

35
    def show_service_options(requestable, _mfhd_id)
3✔
36
      if requestable.no_services?
4✔
37
        content_tag(:div, "#{requestable.title} #{enum_copy_display(requestable.item)} #{sanitize(I18n.t('requests.no_services.brief_msg'))}", class: 'sr-only') +
2✔
38
          content_tag(:div, sanitize(I18n.t("requests.no_services.brief_msg")), class: 'service-item', aria: { hidden: true })
39
      elsif requestable.charged? && !requestable.aeon? && !requestable.ask_me?
2✔
40
        render partial: 'checked_out_options', locals: { requestable: }
1✔
41
      else
42
        display_requestable_list(requestable)
1✔
43
      end
44
    end
45

46
    def hidden_service_options(requestable, fill_in: false)
3✔
47
      return hidden_service_options_fill_in(requestable) if fill_in
116✔
48
      hidden = output_request_input(requestable)
102✔
49
      return hidden if hidden.present?
102✔
50

51
      if requestable.services.include? 'recap'
53✔
52
        recap_print_only_input requestable
40✔
53
      else
54
        request_input(requestable.services.first)
13✔
55
      end
56
    end
57

58
    def output_request_input(requestable)
3✔
59
      output = ""
102✔
60
      ['annex', 'pres', 'ppl', 'lewis', 'paging', 'on_order', 'trace', 'on_shelf'].each do |type|
102✔
61
        next unless requestable.services.include?(type)
753✔
62
        output = request_input(type)
49✔
63
        break
49✔
64
      end
65
      output
102✔
66
    end
67

68
    # only requestable services that support "user-supplied volume info"
69
    def hidden_service_options_fill_in(requestable)
3✔
70
      if requestable.annex?
14✔
71
        request_input('annex')
2✔
72
      elsif requestable.services.include? 'recap_no_items'
12✔
73
        request_input('recap_no_items')
1✔
74
      else
75
        request_input('paging')
11✔
76
      end
77
    end
78

79
    def recap_print_only_input(requestable)
3✔
80
      # id = requestable.item? ? requestable.item['id'] : requestable.holding['id']
81
      content_tag(:fieldset, class: 'recap--print', id: "recap_group_#{requestable.preferred_request_id}") do
40✔
82
        concat hidden_field_tag "requestable[][type]", "", value: 'recap'
40✔
83
      end
84
    end
85

86
    # rubocop:disable Style/NumericPredicate
87
    def enum_copy_display(item)
3✔
88
      return "" if item.blank?
163✔
89
      [item.description, item.copy_value].join(" ").strip
160✔
90
    end
91
    # rubocop:enable Style/NumericPredicate
92

93
    def request_input(type)
3✔
94
      hidden_field_tag "requestable[][type]", "", value: type
82✔
95
    end
96

97
    def gfa_lookup(lib_code)
3✔
98
      if lib_code == "firestone"
2✔
99
        "PA"
×
100
      else
101
        lib = Requests::BibdataService.delivery_locations.select { |_key, hash| hash["library"]["code"] == lib_code }
48✔
102
        lib.keys.first.to_s
2✔
103
      end
104
    end
105

106
    def pick_up_classlist(requestable, collapse)
3✔
107
      class_list = "collapse request--print"
133✔
108
      class_list += " show" if !requestable.digitize? && !collapse
133✔
109
      class_list
133✔
110
    end
111

112
    # move this to requestable object
113
    # Default pick-ups should be available
114
    def pick_up_choices(requestable, default_pick_ups, collapse = false)
3✔
115
      content_tag(:div, id: "fields-print__#{requestable.preferred_request_id}", class: pick_up_classlist(requestable, collapse)) do
121✔
116
        preferred_request_content_tag(requestable, requestable.pick_up_locations || default_pick_ups)
121✔
117
      end
118
    end
119

120
    def preferred_request_content_tag(requestable, default_pick_ups)
3✔
121
      (show_pick_up_service_options(requestable, nil) || "") +
128✔
122
        content_tag(:div, id: "fields-print__#{requestable.preferred_request_id}_card", class: "card card-body bg-light") do
123
          locs = pick_up_locations(requestable, default_pick_ups)
126✔
124
          # temporary changes issue 438
125
          name = 'requestable[][pick_up]'
126✔
126
          id = "requestable__pick_up_#{requestable.preferred_request_id}"
126✔
127
          if locs.size > 1
126✔
128
            select_tag name.to_s, options_for_select(locs.map { |loc| [loc[:label], { 'pick_up' => loc[:gfa_pickup], 'pick_up_location_code' => loc[:pick_up_location_code] }.to_json] }), prompt: I18n.t("requests.default.pick_up_placeholder"), id: id
550✔
129
          else
130
            single_pickup(requestable.charged?, name, id, locs[0])
67✔
131
          end
132
        end
133
    end
134

135
    def available_pick_ups(requestable, default_pick_ups)
3✔
136
      idx = (default_pick_ups.pluck(:label)).index(requestable.location["library"]["label"]) # || 0
116✔
137
      if idx.present?
116✔
138
        [default_pick_ups[idx]]
52✔
139
      elsif requestable.recap? || requestable.annex?
64✔
140
        locations = requestable.pick_up_locations || default_pick_ups
61✔
141
        # open libraries
142
        pick_ups = locations.select { |loc| ['PJ', 'PA', 'PL', 'PK', 'PM', 'QX', 'PW', 'PN', 'QA', 'QT', 'QC'].include?(loc[:gfa_pickup]) }
756✔
143
        pick_ups << default_pick_ups[0] if pick_ups.empty?
61✔
144
        pick_ups
61✔
145
      else
146
        [default_pick_ups[0]]
3✔
147
      end
148
      # return
149
      # temporary only deliver to holding library or firestone
150
      # locs = []
151
      # if requestable.services.include? 'trace'
152
      #   locs = default_pick_ups
153
      # elsif requestable.pick_up_locations.nil?
154
      #   locs = default_pick_ups
155
      # else
156
      #   requestable.pick_up_locations.each do |location|
157
      #     locs << { label: location[:label], gfa_pickup: location[:gfa_pickup], staff_only: location[:staff_only] }
158
      #   end
159
      # end
160
      # locs
161
    end
162

163
    # rubocop:disable Rails/OutputSafety
164
    def hidden_fields_mfhd(mfhd)
3✔
165
      hidden = ""
85✔
166
      return hidden if mfhd.nil?
85✔
167
      hidden += hidden_field_tag "mfhd[][call_number]", "", value: (mfhd['call_number']).to_s unless mfhd["call_number"].nil?
84✔
168
      hidden += hidden_field_tag "mfhd[][location]", "", value: (mfhd['location']).to_s unless mfhd["location"].nil?
84✔
169
      hidden += hidden_field_tag "mfhd[][library]", "", value: (mfhd['library']).to_s
84✔
170
      hidden.html_safe
84✔
171
    end
172
    # rubocop:enable Rails/OutputSafety
173

174
    def hidden_fields_item(requestable)
3✔
175
      request_id = requestable.preferred_request_id
181✔
176
      hidden = hidden_field_tag "requestable[][bibid]", "", value: requestable.bib[:id].to_s, id: "requestable_bibid_#{request_id}"
181✔
177
      hidden += hidden_field_tag "requestable[][mfhd]", "", value: requestable.holding.keys[0].to_s, id: "requestable_mfhd_#{request_id}"
181✔
178
      hidden += hidden_field_tag "requestable[][call_number]", "", value: (requestable.holding.first[1]['call_number']).to_s, id: "requestable_call_number_#{request_id}" unless requestable.holding.first[1]["call_number"].nil?
181✔
179
      hidden += hidden_field_tag "requestable[][location_code]", "", value: requestable.item_location_code.to_s, id: "requestable_location_#{request_id}"
181✔
180
      hidden += if requestable.item?
181✔
181
                  hidden_fields_for_item(item: requestable.item, preferred_request_id: requestable.preferred_request_id)
165✔
182
                else
183
                  hidden_field_tag("requestable[][item_id]", "", value: requestable.preferred_request_id, id: "requestable_item_id_#{requestable.preferred_request_id}")
16✔
184
                end
185
      hidden += hidden_fields_for_scsb(item: requestable.item) if requestable.partner_holding?
181✔
186
      hidden
181✔
187
    end
188

189
    def hidden_fields_holding(requestable)
3✔
190
      hidden = hidden_field_tag "requestable[][mfhd]", "", value: requestable.holding.keys[0].to_s, id: "requestable_mfhd_#{requestable.holding.keys[0]}"
×
191
      hidden += hidden_field_tag "requestable[][call_number]", "", value: (requestable.holding.first[1]['call_number']).to_s, id: "requestable_call_number_#{requestable.holding.keys[0]}" unless requestable.holding.first[1]["call_number"].nil?
×
192
      hidden += hidden_field_tag "requestable[][location_code]", "", value: (requestable.holding.first[1]['location_code']).to_s, id: "requestable_location_code_#{requestable.holding.keys[0]}"
×
193
      hidden += hidden_field_tag "requestable[][location]", "", value: (requestable.holding.first[1]['location']).to_s, id: "requestable_location_#{requestable.holding.keys[0]}"
×
194
      sanitize(hidden, tags: input)
×
195
    end
196

197
    def isbn_string(array_of_isbns)
3✔
198
      array_of_isbns.join(',')
1✔
199
    end
200

201
    def suppress_login(request)
3✔
202
      request.only_aeon?
90✔
203
    end
204

205
    def item_checkbox(requestable, single_item_form)
3✔
206
      disabled = !requestable.will_submit_via_form?
158✔
207
      check_box_tag "requestable[][selected]", true, check_box_selected(requestable, disabled, single_item_form), class: 'request--select', disabled:, aria: { labelledby: "title enum_#{requestable.preferred_request_id}" }, id: "requestable_selected_#{requestable.preferred_request_id}"
158✔
208
    end
209

210
    ## If any requestable items have a temp location assume everything at the holding is in a temp loc?
211
    def current_location_label(mfhd_label, requestable_list)
3✔
212
      location_label = requestable_list.first.location['label'].blank? ? "" : "- #{requestable_list.first.location['label']}"
166✔
213
      label = if requestable_list.first.temp_loc?.present? && !requestable_list.first.in_resource_sharing?
166✔
214
                "#{requestable_list.first.location['library']['label']}#{location_label}"
8✔
215
              else
216
                mfhd_label
158✔
217
              end
218
      "#{label} #{requestable_list.first.call_number}"
166✔
219
    end
220

221
    def check_box_selected(requestable, disabled, single_item_form)
3✔
222
      if single_item_form
158✔
223
        if requestable.charged? || requestable.services.empty?
45✔
224
          false
7✔
225
        else
226
          !disabled
38✔
227
        end
228
      else
229
        false
113✔
230
      end
231
    end
232

233
    def submit_button_disabled(requestable_list)
3✔
234
      # temporary chane issue 438 guest can no longer check out materials
235
      return true if @user.blank? || @user.guest
63✔
236
      return unsubmittable? requestable_list unless requestable_list.size == 1
61✔
237
      # temporary changes issue 438 do not disable the button for circulating items
238
      # requestable_list.first.services.empty? || requestable_list.first.on_reserve? || (requestable_list.first.services.include? 'on_shelf') || requestable_list.first.ask_me?
239
      requestable_list.first.services.empty? || requestable_list.first.on_reserve?
50✔
240
    end
241

242
    def unsubmittable?(requestable_list)
3✔
243
      !requestable_list.any? { |requestable| (requestable.services | submitable_services).present? }
22✔
244
    end
245

246
    def submitable_services
3✔
247
      ['on_shelf', 'in_process', 'on_order', 'annex', 'recap', 'recap_edd', 'paging', 'recap_no_items', 'ppl', 'lewis']
11✔
248
    end
249

250
    def submit_message(requestable_list)
3✔
251
      single_item = "Request this Item"
59✔
252
      multi_item = "Request Selected Items"
59✔
253
      no_item = "No Items Available"
59✔
254
      return multi_item unless requestable_list.size == 1
59✔
255
      if requestable_list.first.services.empty?
48✔
256
        no_item
×
257
      elsif requestable_list.first.charged?
48✔
258
        return multi_item if requestable_list.first.annex? || requestable_list.first.pageable_loc?
5✔
259
        single_item # no_item
5✔
260
      else
261
        submit_message_for_requestable_items(requestable_list)
43✔
262
      end
263
    end
264

265
    def submit_message_for_requestable_items(requestable_list)
3✔
266
      single_item = "Request this Item"
43✔
267
      multi_item = "Request Selected Items"
43✔
268
      trace = "Trace this item"
43✔
269
      if requestable_list.first.annex? || requestable_list.first.pageable_loc?
43✔
270
        multi_item
3✔
271
      elsif requestable_list.first.traceable?
40✔
272
        trace
×
273
      else
274
        single_item
40✔
275
      end
276
    end
277

278
    # only show the table sort if there are enough items
279
    # to make it worthwhile
280
    def show_tablesorter(requestable_list)
3✔
281
      return "tablesorter" if table_sorter_present?(requestable_list)
83✔
282
      ""
75✔
283
    end
284

285
    def table_sorter_present?(requestable_list)
3✔
286
      requestable_list.size > 5
125✔
287
    end
288

289
    def display_label
3✔
290
      {
384✔
291
        author: "Author/Artist",
292
        title: "Title",
293
        date: "Published/Created",
294
        id: "Bibliographic ID",
295
        mfhd: "Holding ID (mfhd)"
296
      }.with_indifferent_access
297
    end
298

299
    # def display_language
300
    #   {
301
    #     language: "Language:"
302
    #   }.with_indifferent_access
303
    # end
304

305
    def display_status(requestable)
3✔
306
      content_tag(:span, requestable.item['status']) unless requestable.item.nil?
×
307
    end
308

309
    def system_status_label(requestable)
3✔
310
      return "" if requestable.item.blank?
×
311
      content_tag(:div, requestable.item[:status], class: 'system-status')
×
312
    end
313

314
    def display_urls(requestable)
3✔
315
      content_tag :ol do
×
316
        requestable.urls.each do |key, value|
×
317
          unless key == 'iiif_manifest_paths'
×
318
            value.reverse!
×
319
            concat content_tag(:li, link_to(value.join(": "), key), class: 'link')
×
320
          end
321
        end
322
      end
323
    end
324

325
    private
3✔
326

327
      def display_requestable_list(requestable)
3✔
328
        return if requestable.no_services?
130✔
329
        content_tag(:ul, class: "service-list") do
123✔
330
          if requestable.ill_eligible?
123✔
331
            concat content_tag(:li, sanitize(I18n.t("requests.ill.brief_msg")), class: "service-item")
1✔
332
          else
333
            # there are no instances where more than one actual service is available to an item, so we are going to take the first service that is not edd
334
            filtered_services = if requestable.services.size == 1 && requestable.services.first.include?("edd")
122✔
335
                                  requestable.services
1✔
336
                                else
337
                                  requestable.services.reject { |service_name| service_name.include?("edd") }
341✔
338
                                end
339
            brief_msg = I18n.t("requests.#{filtered_services.first}.brief_msg")
122✔
340
            concat content_tag(:li, sanitize(brief_msg), class: "service-item")
122✔
341
          end
342
        end
343
      end
344

345
      def display_on_shelf(requestable, _mfhd_id)
3✔
346
        content_tag(:div) do
52✔
347
          display_requestable_list(requestable)
52✔
348
          # temporary changes issue 438
349
          # concat link_to 'Where to find it', requestable.map_url(mfhd_id)
350
          # concat content_tag(:div, sanitize(I18n.t("requests.trace.brief_msg")), class: 'service-item') if requestable.traceable?
351
        end
352
      end
353

354
      def pick_up_locations(requestable, default_pick_ups)
3✔
355
        return [default_pick_ups[0]] if requestable.ill_eligible?
126✔
356
        return available_pick_ups(requestable, default_pick_ups) unless requestable.pending?
124✔
357
        if requestable.delivery_location_label.present?
8✔
358
          [{ label: requestable.delivery_location_label, gfa_pickup: requestable.delivery_location_code, pick_up_location_code: requestable.pick_up_location_code, staff_only: false }]
6✔
359
        else
360
          # TODO: Why is this option here
361
          [{ label: requestable.location[:library][:label], gfa_pickup: gfa_lookup(requestable.location[:library][:code]), staff_only: false }]
2✔
362
        end
363
      end
364

365
      def hidden_fields_for_item(item:, preferred_request_id:)
3✔
366
        hidden = hidden_field_tag("requestable[][item_id]", "", value: preferred_request_id.to_s, id: "requestable_item_id_#{preferred_request_id}")
165✔
367
        hidden += hidden_field_tag("requestable[][barcode]", "", value: item['barcode'].to_s, id: "requestable_barcode_#{preferred_request_id}") unless item["barcode"].nil?
165✔
368
        hidden += hidden_field_tag("requestable[][enum]", "", value: item.enum_value.to_s, id: "requestable_enum_#{preferred_request_id}") if item.enum_value.present?
165✔
369
        hidden += hidden_field_tag("requestable[][copy_number]", "", value: item.copy_number.to_s, id: "requestable_copy_number_#{preferred_request_id}")
165✔
370
        hidden + hidden_field_tag("requestable[][status]", "", value: item['status'].to_s, id: "requestable_status_#{preferred_request_id}")
165✔
371
      end
372

373
      def hidden_fields_for_scsb(item:)
3✔
374
        hidden = hidden_field_tag("requestable[][cgd]", "", value: item['cgd'].to_s, id: "requestable_cgd_#{item['id']}")
8✔
375
        hidden += hidden_field_tag("requestable[][cc]", "", value: item['collection_code'].to_s, id: "requestable_collection_code_#{item['id']}")
8✔
376
        hidden + hidden_field_tag("requestable[][use_statement]", "", value: item['use_statement'].to_s, id: "requestable_use_statement_#{item['id']}")
8✔
377
      end
378

379
      def single_pickup(is_charged, name, id, location)
3✔
380
        style = if is_charged
67✔
381
                  'margin-top:10px;'
1✔
382
                else
383
                  ''
66✔
384
                end
385
        hidden = hidden_field_tag name.to_s, "", value: { 'pick_up' => location[:gfa_pickup], 'pick_up_location_code' => location[:pick_up_location_code] }.to_json, class: 'single-pick-up-hidden', id: id
67✔
386
        label = label_tag id, "Pick-up location: #{location[:label]}", class: 'single-pick-up', style: style.to_s
67✔
387
        hidden + label
67✔
388
      end
389
  end
390
end
391
# rubocop:enable Metrics/ModuleLength
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