Skip to content

Commit 5dbfe7a

Browse files
committed
Make Book#sync_copies work per-library system.
1 parent 7870ca0 commit 5dbfe7a

5 files changed

Lines changed: 34 additions & 24 deletions

File tree

‎app/models/book.rb‎

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,22 @@ def self.with_sync_errors
2626
where('last_sync_error is not null')
2727
end
2828

29-
def sync_copies
29+
def sync_copies(library_system)
3030
now = Time.now
3131

32-
LibrarySystem.all.each do |system|
33-
system.find(title, author).each do |scraped_book|
34-
location = Location.where(name: scraped_book.location, library_system_id: system.id).first_or_create
35-
36-
copy = copies.where(location: location,
37-
call_number: scraped_book.call_number,
38-
title: scraped_book.title
39-
).first_or_create
40-
copy.update_attributes(
41-
last_synced_at: now,
42-
url: scraped_book.url,
43-
status: scraped_book.status)
44-
end
32+
library_system.find(title, author).each do |scraped_book|
33+
location = Location.where(name: scraped_book.location, library_system_id: library_system.id).first_or_create
34+
35+
copy = copies.where(location: location,
36+
call_number: scraped_book.call_number,
37+
title: scraped_book.title
38+
).first_or_create
39+
copy.update_attributes(
40+
last_synced_at: now,
41+
url: scraped_book.url,
42+
status: scraped_book.status)
4543
end
46-
self.copies.where('last_synced_at < ?', now).destroy_all
44+
self.copies.for_library_system(library_system).where('last_synced_at < ?', now).destroy_all
4745
update_attributes(last_synced_at: now)
4846

4947
BookNotifier.notify_all(self)

‎app/workers/update_book.rb‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ class UpdateBook
66
@queue = :update_book
77
@retry_limit = 3
88

9-
def self.perform(book_id)
9+
def self.perform(book_id, library_system_id)
1010
book = Book.find(book_id)
11-
book.sync_copies
11+
book.sync_copies(LibrarySystem.find(library_system_id))
1212
book.update_attributes(last_sync_error: nil)
1313
rescue StandardError => e
1414
error_lines = [e.class.name, e.to_s] + e.backtrace

‎lib/tasks/scheduler.rake‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ end
77

88
task :refresh_all_books => :environment do
99
Book.find_each do |book|
10-
Resque.enqueue UpdateBook, book.id
10+
LibrarySystem.all.each do |library_system|
11+
Resque.enqueue UpdateBook, book.id, library_system.id
12+
end
1113
end
1214
end
1315

‎spec/models/book_spec.rb‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
it "produces new copies" do
1616
expect(@book.copies).to be_empty
17-
@book.sync_copies
17+
@book.sync_copies(LibrarySystem::MINUTEMAN)
1818
expect(@book.copies.length).to eq(2)
1919
expect(@book.copies.map(&:title)).to eq([@book.title, @book.title])
2020
end
@@ -23,7 +23,7 @@
2323
create(:location, name: "Main", library_system_id: LibrarySystem::MINUTEMAN.id)
2424

2525
expect(Location.count).to eq(1)
26-
@book.sync_copies
26+
@book.sync_copies(LibrarySystem::MINUTEMAN)
2727
expect(Location.count).to eq(2)
2828
expect(Location.pluck(:name)).to eq(["Main", "Concord"])
2929
end
@@ -33,7 +33,7 @@
3333
copy = create(:copy, book: @book, call_number: "ABC", title: @book.title, status: "Out", location: location)
3434

3535
expect(@book.copies.length).to eq(1)
36-
@book.sync_copies
36+
@book.sync_copies(LibrarySystem::MINUTEMAN)
3737
expect(@book.reload.copies.length).to eq(2)
3838
expect(copy.reload.status).to eq("In")
3939
end
@@ -43,9 +43,19 @@
4343
copy = create(:copy, book: @book, call_number: "ABC", title: @book.title, status: "Out", location: location, last_synced_at: 1.day.ago)
4444

4545
expect(@book.copies.length).to eq(1)
46-
@book.sync_copies
46+
@book.sync_copies(LibrarySystem::MINUTEMAN)
4747
expect(@book.reload.copies.length).to eq(2)
4848
expect(@book.copies).to_not include(copy)
4949
end
50+
51+
it "only deletes copies no longer found for the passed library system" do
52+
location = create(:location, name: "Somewhere Else", library_system_id: LibrarySystem::MINUTEMAN.id)
53+
copy = create(:copy, book: @book, call_number: "ABC", title: @book.title, status: "Out", location: location, last_synced_at: 1.day.ago)
54+
55+
expect(@book.copies.length).to eq(1)
56+
@book.sync_copies(LibrarySystem::BOSTON)
57+
expect(@book.reload.copies.length).to eq(1)
58+
expect(@book.copies).to include(copy)
59+
end
5060
end
5161
end

‎spec/workers/update_book_spec.rb‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class StubbedError < StandardError; end
77
Book.any_instance.stub(:sync_copies) {|book_id| raise StubbedError.new("oh no!") }
88
book = create(:book)
99

10-
expect{ UpdateBook.perform(book.id) }.to raise_error(StubbedError)
10+
expect{ UpdateBook.perform(book.id, LibrarySystem::MINUTEMAN.id) }.to raise_error(StubbedError)
1111
expect(book.reload.last_sync_error).to_not be_nil
1212
expect(book.reload.last_sync_error).to include('StubbedError')
1313
end
@@ -16,7 +16,7 @@ class StubbedError < StandardError; end
1616
Book.any_instance.stub(:sync_copies)
1717
book = create(:book, last_sync_error: "things went poorly")
1818

19-
expect{ UpdateBook.perform(book.id) }.to_not raise_error
19+
expect{ UpdateBook.perform(book.id, LibrarySystem::MINUTEMAN.id) }.to_not raise_error
2020
expect(book.reload.last_sync_error).to be_nil
2121
end
2222
end

0 commit comments

Comments
 (0)