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

pulibrary / orangelight / 62bad3f1-d46d-40af-822c-403d653da2a8

17 Jun 2025 05:30PM UTC coverage: 0.447% (-94.9%) from 95.337%
62bad3f1-d46d-40af-822c-403d653da2a8

push

circleci

maxkadel
Install chrome & chromedriver for smoke specs

43 of 9610 relevant lines covered (0.45%)

0.01 hits per line

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

0.0
/app/controllers/orangelight/browsables_controller.rb
1
# frozen_string_literal: true
2

3
class Orangelight::BrowsablesController < ApplicationController
×
4
  # GET /orangelight/names
5
  # GET /orangelight/names.json
6

7
  def index
×
8
    # if rpp isn't specified default is 50
9
    # if rpp has values other than 10, 25, 50, 100 then set it to 50
10
    # previous/next page links need to pass
11
    # manually set rpp
12

13
    assign_values
×
14

15
    # makes sure valid page is displayed
16
    if !(@last_id - @rpp + 1..@last_id).cover?(@start) && @is_last
×
17
      @start = @last_id - @rpp + 1
×
18
      @start = 1 if @start < 1 # catch for start ids higher than last id
×
19
    end
×
20

21
    assign_pagination_values
×
22

23
    @facet = facet
×
24
    @list_name = list_name
×
25

26
    respond_to do |format|
×
27
      format.html # index.html.erb
×
28
      format.json { render json: @orangelight_browsables }
×
29
    end
×
30
  end
×
31

32
  private
×
33

34
    def assign_values
×
35
      @model = model_table_name
×
36
      @rpp = rpp
×
37
      @page_link = page_link
×
38

39
      # @start gets the id of the first entry to display on page
40
      # specific ids are given based on search results
41
      @start = first_model_id
×
42

43
      populate_search_results
×
44

45
      # gets last page of table's results
46
      @last_id = last_model_id
×
47

48
      # makes sure no next page link is shown for last page
49
      @is_last = (@last_id - @rpp + 1) <= @start
×
50
    end
×
51

52
    def rpp
×
53
      if rpp_param.nil?
×
54
        50
×
55
      else
×
56
        validate_rpp(requested_rpp)
×
57
      end
×
58
    end
×
59

60
    def page_link
×
61
      if rpp_param.nil?
×
62
        '?'
×
63
      else
×
64
        size = validate_rpp(requested_rpp)
×
65
        "?rpp=#{size}&"
×
66
      end
×
67
    end
×
68

69
    def validate_rpp(size)
×
70
      rpp_range = [10, 25, 50, 100]
×
71
      if rpp_range.include? size
×
72
        size
×
73
      else
×
74
        50
×
75
      end
×
76
    end
×
77

78
    def populate_search_results
×
79
      return if query_param.nil?
×
80
      # 'subject_facet' is the default vocabulary type if there is no vocabulary type specified
81
      search_result = if vocabulary_search_on_facet == 'subject_facet'
×
82
                        model_param.where('sort <= ?', search_term).order('sort').last
×
83
                      else
×
84
                        model_param.where(sort: search_term).where(vocabulary: vocabulary_search_on_facet).first
×
85
                      end
×
86

87
      return if search_result.nil?
×
88

89
      populate_search_params(search_result)
×
90
    end
×
91

92
    def populate_search_params(search_result)
×
93
      @search_result = search_result.label
×
94
      @search_term = search_term
×
95
      @exact_match = search_term == search_result.sort
×
96
      @match = search_result.id
×
97
      @match_vocabulary = search_result.vocabulary if @model == 'subjects'
×
98
      @start = search_result.id - 1
×
99
      @start -= 1 if @exact_match
×
100
      @start = 1 if @start < 1
×
101
      @query = query_param
×
102
    end
×
103

104
    def assign_pagination_values
×
105
      @is_first = @start == 1
×
106

107
      @page_last = if (@start + @rpp - 1) > @last_id
×
108
                     @last_id
×
109
                   else
×
110
                     @start + @rpp - 1
×
111
                   end
×
112

113
      @prev = @start - @rpp
×
114
      @prev = 1 if @prev < 1
×
115
      @next = @start + @rpp
×
116

117
      @orangelight_browsables = model_param.where(id: @start..@page_last)
×
118
    end
×
119

120
    def facet
×
121
      if browsing_names?
×
122
        'author_s'
×
123
      elsif browsing_subjects?
×
124
        vocab_param
×
125
      elsif browsing_titles?
×
126
        'name_title_browse_s'
×
127
      end
×
128
    end
×
129

130
    # Retrieve the Model mapped to the request parameter
131
    # @return [Class]
132
    def model_param
×
133
      params[:model]
×
134
    end
×
135

136
    def vocab_param
×
137
      params[:vocab]
×
138
    end
×
139

140
    def vocabulary_search_on_facet
×
141
      # TODO: move vocab_types to a config file
142
      # the hash also exists in vocab_type method
143
      vocab_types = {
×
144
        'Library of Congress subject heading' => 'lc_subject_facet',
×
145
        'Library of Congress genre/form terms for library and archival materials' => 'lcgft_genre_facet',
×
146
        'Art & architecture thesaurus' => 'aat_genre_facet',
×
147
        'Homosaurus terms' => 'homoit_subject_facet',
×
148
        'Homosaurus genres' => 'homoit_genre_facet',
×
149
        'Rare books genre term' => 'rbgenr_genre_facet',
×
150
        'Chinese traditional subjects' => 'siku_subject_facet',
×
151
        'Locally assigned term' => 'local_subject_facet'
×
152
      }
×
153
      vocab_types_invert = vocab_types.invert
×
154
      vocab_types_invert.fetch(vocab_param, 'subject_facet')
×
155
    end
×
156

157
    # Generates the name of the table mapped to the model in the request
158
    # @return [String]
159
    def model_table_name
×
160
      model_name = model_param.name
×
161
      model_class = model_name.demodulize
×
162
      model_class.tableize
×
163
    end
×
164

165
    # Determines whether or not the client is requesting to browse call numbers
166
    # @return [Boolean]
167
    def browsing_call_numbers?
×
168
      model_table_name == 'call_numbers'
×
169
    end
×
170

171
    # Determines whether or not the client is requesting to browse names
172
    # @return [Boolean]
173
    def browsing_names?
×
174
      model_table_name == 'names'
×
175
    end
×
176

177
    # Determines whether or not the client is requesting to browse subjects
178
    # @return [Boolean]
179
    def browsing_subjects?
×
180
      model_table_name == 'subjects'
×
181
    end
×
182

183
    # Determines whether or not the client is requesting to browse titles
184
    # @return [Boolean]
185
    def browsing_titles?
×
186
      model_table_name == 'name_titles'
×
187
    end
×
188

189
    # Generates the name of the list (based upon the model for the request)
190
    # @return [String]
191
    def list_name
×
192
      value = model_table_name.humanize
×
193
      return 'author-title headings' if value == 'Name titles'
×
194
      value
×
195
    end
×
196

197
    # Retrieves the ID requested by the client
198
    # @return [String]
199
    def id_param
×
200
      params[:id]
×
201
    end
×
202

203
    # Retrieves the query transmitted by the client
204
    # @return [String]
205
    def query_param
×
206
      params[:q]
×
207
    end
×
208

209
    # Retrieves the ID for the first object requested by the client
210
    # @return [String]
211
    def start_param
×
212
      params[:start]
×
213
    end
×
214

215
    # Retrieves the ID of the first object for this request
216
    # @return [Integer]
217
    def first_model_id
×
218
      return 1 if start_param.nil?
×
219
      start_param.to_i
×
220
    end
×
221

222
    # Normalizes the query transmitted by the client
223
    # @return [String]
224
    def search_term
×
225
      return StringFunctions.cn_normalize(query_param) if browsing_call_numbers?
×
226

227
      query_param.normalize_em
×
228
    end
×
229

230
    # Retrieves the ID of the last object for this request
231
    # @return [Integer]
232
    def last_model_id
×
233
      return model_param.last.id if model_param.last
×
234
      1
×
235
    end
×
236

237
    # Retrieves the requested rows per page (rpp) from the client
238
    # @return [String]
239
    def rpp_param
×
240
      params[:rpp]
×
241
    end
×
242

243
    # Generates the requested rows per page as an Integer
244
    # @return [Integer]
245
    def requested_rpp
×
246
      rpp_param.to_i
×
247
    end
×
248

249
    # Use callbacks to share common setup or constraints between actions.
250
    def set_orangelight_browsable
×
251
      @orangelight_browsable = model_param.find(id_param) if model_param
×
252
    end
×
253

254
    # Never trust parameters from the scary internet; run the params we receive through an allowlist
255
    def orangelight_browsable_params
×
256
      params.require(:orangelight_browsable).permit(:model, :id)
×
257
    end
×
258
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