Permalink
Browse files

Make Book#sync_copies work per-library system.

  • Loading branch information...
thegreatape committed Jan 3, 2015
1 parent 7870ca0 commit 5dbfe7a5629b88170cf65801b4c9a8ec37b44574
Showing with 34 additions and 24 deletions.
  1. +13 −15 app/models/book.rb
  2. +2 −2 app/workers/update_book.rb
  3. +3 −1 lib/tasks/scheduler.rake
  4. +14 −4 spec/models/book_spec.rb
  5. +2 −2 spec/workers/update_book_spec.rb
@@ -26,24 +26,22 @@ def self.with_sync_errors
where('last_sync_error is not null')
end
def sync_copies
def sync_copies(library_system)
now = Time.now
LibrarySystem.all.each do |system|
system.find(title, author).each do |scraped_book|
location = Location.where(name: scraped_book.location, library_system_id: system.id).first_or_create
copy = copies.where(location: location,
call_number: scraped_book.call_number,
title: scraped_book.title
).first_or_create
copy.update_attributes(
last_synced_at: now,
url: scraped_book.url,
status: scraped_book.status)
end
library_system.find(title, author).each do |scraped_book|
location = Location.where(name: scraped_book.location, library_system_id: library_system.id).first_or_create
copy = copies.where(location: location,
call_number: scraped_book.call_number,
title: scraped_book.title
).first_or_create
copy.update_attributes(
last_synced_at: now,
url: scraped_book.url,
status: scraped_book.status)
end
self.copies.where('last_synced_at < ?', now).destroy_all
self.copies.for_library_system(library_system).where('last_synced_at < ?', now).destroy_all
update_attributes(last_synced_at: now)
BookNotifier.notify_all(self)
@@ -6,9 +6,9 @@ class UpdateBook
@queue = :update_book
@retry_limit = 3
def self.perform(book_id)
def self.perform(book_id, library_system_id)
book = Book.find(book_id)
book.sync_copies
book.sync_copies(LibrarySystem.find(library_system_id))
book.update_attributes(last_sync_error: nil)
rescue StandardError => e
error_lines = [e.class.name, e.to_s] + e.backtrace
@@ -7,7 +7,9 @@ end
task :refresh_all_books => :environment do
Book.find_each do |book|
Resque.enqueue UpdateBook, book.id
LibrarySystem.all.each do |library_system|
Resque.enqueue UpdateBook, book.id, library_system.id
end
end
end
@@ -14,7 +14,7 @@
it "produces new copies" do
expect(@book.copies).to be_empty
@book.sync_copies
@book.sync_copies(LibrarySystem::MINUTEMAN)
expect(@book.copies.length).to eq(2)
expect(@book.copies.map(&:title)).to eq([@book.title, @book.title])
end
@@ -23,7 +23,7 @@
create(:location, name: "Main", library_system_id: LibrarySystem::MINUTEMAN.id)
expect(Location.count).to eq(1)
@book.sync_copies
@book.sync_copies(LibrarySystem::MINUTEMAN)
expect(Location.count).to eq(2)
expect(Location.pluck(:name)).to eq(["Main", "Concord"])
end
@@ -33,7 +33,7 @@
copy = create(:copy, book: @book, call_number: "ABC", title: @book.title, status: "Out", location: location)
expect(@book.copies.length).to eq(1)
@book.sync_copies
@book.sync_copies(LibrarySystem::MINUTEMAN)
expect(@book.reload.copies.length).to eq(2)
expect(copy.reload.status).to eq("In")
end
@@ -43,9 +43,19 @@
copy = create(:copy, book: @book, call_number: "ABC", title: @book.title, status: "Out", location: location, last_synced_at: 1.day.ago)
expect(@book.copies.length).to eq(1)
@book.sync_copies
@book.sync_copies(LibrarySystem::MINUTEMAN)
expect(@book.reload.copies.length).to eq(2)
expect(@book.copies).to_not include(copy)
end
it "only deletes copies no longer found for the passed library system" do
location = create(:location, name: "Somewhere Else", library_system_id: LibrarySystem::MINUTEMAN.id)
copy = create(:copy, book: @book, call_number: "ABC", title: @book.title, status: "Out", location: location, last_synced_at: 1.day.ago)
expect(@book.copies.length).to eq(1)
@book.sync_copies(LibrarySystem::BOSTON)
expect(@book.reload.copies.length).to eq(1)
expect(@book.copies).to include(copy)
end
end
end
@@ -7,7 +7,7 @@ class StubbedError < StandardError; end
Book.any_instance.stub(:sync_copies) {|book_id| raise StubbedError.new("oh no!") }
book = create(:book)
expect{ UpdateBook.perform(book.id) }.to raise_error(StubbedError)
expect{ UpdateBook.perform(book.id, LibrarySystem::MINUTEMAN.id) }.to raise_error(StubbedError)
expect(book.reload.last_sync_error).to_not be_nil
expect(book.reload.last_sync_error).to include('StubbedError')
end
@@ -16,7 +16,7 @@ class StubbedError < StandardError; end
Book.any_instance.stub(:sync_copies)
book = create(:book, last_sync_error: "things went poorly")
expect{ UpdateBook.perform(book.id) }.to_not raise_error
expect{ UpdateBook.perform(book.id, LibrarySystem::MINUTEMAN.id) }.to_not raise_error
expect(book.reload.last_sync_error).to be_nil
end
end

0 comments on commit 5dbfe7a

Please sign in to comment.