Skip to content

Commit

Permalink
Merge pull request #900 from sul-dlss/pref-cn-fix
Browse files Browse the repository at this point in the history
Handle preferred barcodes that don't exist in the holdings.
  • Loading branch information
jchristo4 committed Sep 12, 2014
2 parents 4791ac3 + bba8c0b commit 8013588
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
11 changes: 7 additions & 4 deletions lib/holdings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ def find_by_barcode(barcode)
end
end
def preferred_callnumber
@preferred_callnumber ||= if @document[:preferred_barcode]
find_by_barcode(@document[:preferred_barcode])
else
callnumbers.first
@preferred_callnumber ||= begin
if @document[:preferred_barcode] &&
(found_callnumber = find_by_barcode(@document[:preferred_barcode])).present?
found_callnumber
else
callnumbers.first
end
end
end
def present?
Expand Down
14 changes: 14 additions & 0 deletions spec/lib/holdings_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,17 @@
)
)
}
let(:bad_preferred) {
Holdings.new(
SolrDocument.new(
preferred_barcode: 'does-not-exist',
item_display: [
'54321 -|- GREEN -|- STACKS -|- -|- -|- -|- -|- -|- callnumber1 -|- 1',
'12345 -|- GREEN -|- STACKS -|- -|- -|- -|- -|- -|- callnumber2 -|- 2'
]
)
)
}
let(:no_preferred) {
Holdings.new(
SolrDocument.new(
Expand All @@ -119,6 +130,9 @@
it 'should return the callnumber based on preferred barcode' do
expect(preferred.preferred_callnumber.barcode).to eq '12345'
end
it 'should return the first callnumber when the preferred barcode does not exist in the holdings' do
expect(bad_preferred.preferred_callnumber.barcode).to eq '54321'
end
it 'should return the first callnumber if there is no preferred barcode available' do
expect(no_preferred.preferred_callnumber.barcode).to eq '54321'
end
Expand Down

0 comments on commit 8013588

Please sign in to comment.