Skip to content

Commit

Permalink
Implementing date-based sorting for call numbers in the browse lists
Browse files Browse the repository at this point in the history
  • Loading branch information
jrgriffiniii committed Sep 14, 2018
1 parent 81deec1 commit 590f54e
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 3 deletions.
1 change: 1 addition & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ Metrics/LineLength:
- 'lib/tasks/pulsearch.rake'
- 'lib/tasks/server.rake'
- '**/Gemfile'
- 'app/controllers/orangelight/browsables_controller.rb'

Metrics/MethodLength:
Exclude:
Expand Down
6 changes: 6 additions & 0 deletions app/controllers/orangelight/browsables_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ def index
@list_name = params[:model].name.demodulize.tableize.humanize
@list_name = 'author-title headings' if @list_name == 'Name titles'

if @model == 'call_numbers'
@orangelight_browsables_sorted = Orangelight::CallNumber.sort_by_label_date(@orangelight_browsables.sort)
else
@orangelight_browsables_sorted = @orangelight_browsables.sort
end

respond_to do |format|
format.html # index.html.erb
format.json { render json: @orangelight_browsables }
Expand Down
41 changes: 41 additions & 0 deletions app/models/orangelight/call_number.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,45 @@ class Orangelight::CallNumber < ApplicationRecord
def self.table_name_prefix
'orangelight_'
end

# Sorts an Array of CallNumber instances using their label dates
# @param [Array<Orangelight::CallNumber>] call_numbers
# @return [Array<Orangelight::CallNumber>] the (re)sorted Array
def self.sort_by_label_date(call_numbers)
sorted = []

call_numbers.each_slice(2) do |a, b|
if b.nil?
sorted << a
elsif a.label_date && b.label_date && ((a.label_date <=> b.label_date) == 1)
sorted << b
sorted << a
else
sorted << a
sorted << b
end
end

sorted
end

# Constructs a Date by parsing the label
# @return [Date]
def label_date
m = / (\d{4})(?!\d)/.match(label)
return unless m&.captures&.length == 1

values = m.captures
value = Date.parse("#{values.last}-01-01")
value
end

# Compares two CallNumber instances
# @see Object#<=>
# @param [Orangelight::CallNumber] other
# @return [Integer]
def <=>(other)
return 1 unless other.is_a?(self.class)
sort <=> other.sort
end
end
4 changes: 2 additions & 2 deletions app/views/orangelight/browsables/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

<% if @model != 'call_numbers' %>
<ul class="browse">
<% @orangelight_browsables.each_with_index do |orangelight_name, i| %>
<% @orangelight_browsables_sorted.each_with_index do |orangelight_name, i| %>

<% if orangelight_name.id == @match && @exact_match %>
<li class="alert alert-info clickable-row" data-href="/catalog/?f[<%=@facet%>][]=<%=CGI.escape orangelight_name.label%>">
Expand Down Expand Up @@ -73,7 +73,7 @@
</thead>

<tbody >
<% @orangelight_browsables.each_with_index do |orangelight_name, i| %>
<% @orangelight_browsables_sorted.each_with_index do |orangelight_name, i| %>
<% if orangelight_name.id == @match && @exact_match %>
<tr class="alert alert-info clickable-row" data-href="/catalog/<%=orangelight_name.bibid%>">
<strong>
Expand Down
2 changes: 1 addition & 1 deletion spec/features/browsables_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
describe 'Browse by Call Number' do
it 'displays two browse entries before exact match' do
visit '/browse/call_numbers?q=PL856.U673+A61213+2011&rpp=10'
expect(page.all('tr')[3][:class]).to eq('alert alert-info clickable-row')
expect(page.all('tr')[4][:class]).to eq('alert alert-info clickable-row')
end
end

Expand Down
66 changes: 66 additions & 0 deletions spec/models/orangelight/call_number_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe Orangelight::CallNumber do
subject(:call_number) { described_class.new(attributes) }

let(:attributes) do
{
label: 'Z342 .M48 1988'
}
end

describe '.sort_by_label_date' do
let(:call_number_2) { described_class.new(label: 'Z342 .M48 1990') }
let(:call_number_1) { described_class.new(label: 'Z342 .M48 1988') }
let(:call_numbers) do
[
call_number_2,
call_number_1
]
end
let(:sorted_call_numbers) { described_class.sort_by_label_date(call_numbers) }

it 'sorts by label dates' do
expect(sorted_call_numbers).not_to be_empty
expect(sorted_call_numbers.length).to eq(2)
expect(sorted_call_numbers.first).to eq(call_number_1)
expect(sorted_call_numbers.last).to eq(call_number_2)
end
end

describe '#label_date' do
let(:label_date) { Date.parse('1988-01-01') }

it 'constructs a Date from the label attribute' do
expect(call_number.label_date).to eq(label_date)
end

context 'when the label does not contain a date' do
let(:attributes) do
{
label: 'Z342 .M48'
}
end

it 'does not construct a Date' do
expect(call_number.label_date).to be_nil
end
end
end

describe '#<=>' do
let(:attributes) do
{
sort: 'Z342 .M48 1988'
}
end
let(:call_number_2) { described_class.new(sort: 'Z342 .M48 1990') }

it 'compares two CallNumbers by the #sorted attribute' do
expect(call_number <=> call_number_2).to eq(-1)
expect(call_number_2 <=> call_number).to eq(1)
end
end
end

0 comments on commit 590f54e

Please sign in to comment.