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

pulibrary / orangelight / 9d368f39-3678-4b19-aecb-3a247a203167

pending completion
9d368f39-3678-4b19-aecb-3a247a203167

Pull #3363

circleci

Jane Sandberg
Don't allow more than 1000 bookmarks
Pull Request #3363: I2889 bookmark limit v2

28 of 32 new or added lines in 2 files covered. (87.5%)

5283 of 5592 relevant lines covered (94.47%)

1494.21 hits per line

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

95.71
/app/controllers/bookmarks_controller.rb
1
# frozen_string_literal: true
2

3
require 'csv'
2✔
4

5
class BookmarksController < CatalogController
2✔
6
  include Blacklight::Bookmarks
2✔
7
  configure_blacklight do |_config|
2✔
8
    blacklight_config.show.document_actions[:print] =
2✔
9
      {
10
        partial: 'document_action',
11
        name: :print,
12
        modal: false
13
      }
14
  end
15

16
  # Copied from
17
  # https://github.com/projectblacklight/blacklight/blob/040933c7a383cd0c5be5895d51ab1004ef3ad5e1/app/controllers/concerns/blacklight/bookmarks.rb#L40-L57
18
  def index
2✔
19
    @bookmarks = token_or_current_or_guest_user.bookmarks
8✔
20
    @response, deprecated_document_list = search_service.fetch(bookmark_ids)
8✔
21
    # <Princeton Modifications>
22
    # Commented out to use the instance method instead, which adds alma IDs.
23
    # bookmark_ids = @bookmarks.collect { |b| b.document_id.to_s }
24
    # </Princeton Modifications>
25
    @document_list = ActiveSupport::Deprecation::DeprecatedObjectProxy.new(deprecated_document_list, "The @document_list instance variable is now deprecated and will be removed in Blacklight 8.0")
8✔
26
    respond_to do |format|
8✔
27
      format.html {}
8✔
28
      format.rss  { render layout: false }
8✔
29
      format.atom { render layout: false }
8✔
30
      format.json do
8✔
31
        render json: render_search_results_as_json
×
32
      end
33

34
      additional_response_formats(format)
8✔
35
      document_export_formats(format)
8✔
36
    end
37
  end
38

39
  def create
2✔
40
    @bookmarks = if params[:bookmarks]
3✔
41
                   permit_bookmarks[:bookmarks]
2✔
42
                 else
43
                   [{ document_id: params[:id], document_type: blacklight_config.document_model.to_s }]
1✔
44
                 end
45

46
    current_or_guest_user.save! unless current_or_guest_user.persisted?
3✔
47

48
    bookmarks_to_add = @bookmarks.reject { |bookmark| current_or_guest_user.bookmarks.where(bookmark).exists? }
6✔
49
    errors = []
3✔
50
    ActiveRecord::Base.transaction do
3✔
51
      bookmarks_to_add.each do |bookmark|
3✔
52
        errors.concat current_or_guest_user.bookmarks.create(bookmark)&.errors&.full_messages
3✔
53
      end
54
    end
55

56
    if request.xhr?
3✔
57
      errors.empty? ? render(json: { bookmarks: { count: current_or_guest_user.bookmarks.count } }) : render(json: errors, status: "500")
2✔
58
    else
59
      if @bookmarks.any? && errors.empty?
1✔
60
        flash[:notice] = I18n.t('blacklight.bookmarks.add.success', count: @bookmarks.length)
1✔
NEW
61
      elsif @bookmarks.any?
×
NEW
62
        flash[:error] = I18n.t('blacklight.bookmarks.add.failure', count: @bookmarks.length)
×
63
      end
64

65
      redirect_back fallback_location: bookmarks_path
1✔
66
    end
67
  end
68

69
  def print
2✔
70
    fetch_bookmarked_documents
1✔
71
    @url_gen_params = {}
1✔
72
    render 'record_mailer/email_record.text.erb', template: false, content_type: 'text/plain'
1✔
73
  end
74

75
  def csv
2✔
76
    fetch_bookmarked_documents
1✔
77
    send_data csv_output, type: 'text/csv', filename: "bookmarks-#{Time.zone.today}.csv"
1✔
78
  end
79

80
  private
2✔
81

82
    def bookmark_ids
2✔
83
      bookmarks = token_or_current_or_guest_user.bookmarks
12✔
84
      bookmarks.collect { |b| convert_to_alma_id(b.document_id.to_s) }
69✔
85
    end
86

87
    def fetch_bookmarked_documents
2✔
88
      _, @documents = search_service.fetch(bookmark_ids, rows: bookmark_ids.length, fl: '*')
2✔
89
    end
90

91
    def convert_to_alma_id(id)
2✔
92
      if (id.length < 13) && (id =~ /^\d+$/)
57✔
93
        "99#{id}3506421"
51✔
94
      else
95
        id
6✔
96
      end
97
    end
98

99
    # byte-order-mark declaring our output as UTF-8 (required for non-ASCII to be handled by Excel)
100
    def csv_bom
2✔
101
      %w[EF BB BF].map { |a| a.hex.chr }.join
4✔
102
    end
103

104
    def csv_fields
2✔
105
      {
106
        id: 'ID',
49✔
107
        title_citation_display: ['Title', 'Title (Original Script)'],
108
        author_display: ['Author', 'Author (Original Script)'],
109
        format: 'Format',
110
        language_facet: 'Language',
111
        pub_citation_display: 'Published/Created',
112
        pub_date_display: 'Date',
113
        description_display: 'Description',
114
        series_display: 'Series',
115
        location_display: 'Location',
116
        call_number_display: 'Call Number',
117
        notes_display: 'Notes'
118
      }
119
    end
120

121
    def csv_output
2✔
122
      CSV.generate(csv_bom, headers: true) do |csv|
1✔
123
        csv << csv_fields.values.flatten
1✔
124
        @documents.each do |doc|
1✔
125
          csv << csv_fields.keys.map { |field| csv_values(doc, field) }.flatten
26✔
126
        end
127
      end
128
    end
129

130
    def csv_values(doc, field)
2✔
131
      if csv_fields[field] == 'ID'
24✔
132
        "'#{doc[field]}'"
2✔
133
      elsif csv_fields[field].is_a?(Array)
22✔
134
        two_values(doc[field])
4✔
135
      else
136
        Array(doc[field]).join('; ')
18✔
137
      end
138
    end
139

140
    def two_values(arr)
2✔
141
      values = arr || ['', '']
4✔
142
      values << '' if values.length == 1
4✔
143
      values.map { |v| v.chomp(' /') }
12✔
144
    end
145
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