heliostatic avatar heliostatic committed 41732aa

must faster author search, should still work

Comments (0)

Files changed (6)

app/controllers/books_controller.rb

 
   def authors
     respond_to do |format|
-      format.json { render json: Book.select('DISTINCT author').where('author IS NOT NULL').order(:author).map(&:author) }
+      format.json { render json: Book.all_authors }
     end
   end
 

app/models/book.rb

     self.update_attributes(dead: true)
   end
 
+  def self.all_authors
+    Book.select('DISTINCT author').where('author IS NOT NULL').where('for_review IS NOT TRUE').order(:author).map(&:author)
+  end
+
   def self.text_search(query)
     if query.present?
       where("title @@ :q or author @@ :q or fullpath @@ :q", q: query).order('id DESC')

lib/extensions/book_cleaner.rb

     fields
   end
 
+  def bulk_guess_fields(cache, expensive=false)
+    @cache = cache
+    fields = get_fields_from_path(self.fullpath, self.file_type)
+    check_author(fields)
+    check_title(fields)
+    if expensive
+      expensive_check(fields)
+    end
+    fields
+  end
+
 
   def partial_path
     File.basename(self.fullpath, self.file_type)
 
   module ClassMethods
     def full_search(number_to_check=500)
+      cache = Book.select('DISTINCT author').where('author IS NOT NULL').where('for_review IS NOT TRUE').order(:author).map(&:author)
       self.no_title.where(for_review: nil).limit(number_to_check).each do |b|
-        b.best_guess_fields(true)
+        b.bulk_guess_fields(cache, true)
       end
     end
   end
 
   private
     def set_author_if_possible(f)
-      author = f if Book.where(author: f).where('for_review IS NOT TRUE').any?
-      author = flip_and_strip(f) if Book.where(author: flip_and_strip(f)).where('for_review IS NOT TRUE').any?
-      if self.author.nil?
-        self.update_attributes(author: author) if author
-      end
+      if @cache
+        author = f if @cache.include?(f)
+        author = flip_and_strip(f) if @cache.include?(flip_and_strip(f))
+        if self.author.nil?
+          self.update_attributes(author: author) if author
+        end 
+      else
+        author = f if Book.where(author: f).where('for_review IS NOT TRUE').any?
+        author = flip_and_strip(f) if Book.where(author: flip_and_strip(f)).where('for_review IS NOT TRUE').any?
+        if self.author.nil?
+          self.update_attributes(author: author) if author
+        end 
+      end     
       author
     end
 

lib/tasks/book_import.rake

 end
 
 task :check_authors => :environment do
+  cache = Book.all_authors
   Book.find_each do |b|
-    b.best_guess_fields
+    b.bulk_guess_fields(cache)
   end
 end
 

test/fixtures/books.yml

   fullpath: 'title - Dumas, Alexandre.epub'
   file_type: .epub
   title:
-  author: 'Alexandre Dumas'
+  author: 'Alexandre Dumas'
+
+book_with_kelly_link_as_author:
+  fullpath: '/Volumes/text/Books/Bundles/these_things_happen_-_kelly_link.epub'
+  file_type: .epub
+  title: 
+  author: "Kelly Link"

test/unit/book_test.rb

     books(:book_with_mismatched_author_and_author_and_reverse_order).best_guess_fields
     books(:book_with_mismatched_author_and_author_and_reverse_order).title.must_equal 'Title'
   end
+
+  it "should update books in bulk correctly" do
+    cache = Book.all_authors
+    Book.find_each do |b|
+      b.bulk_guess_fields(cache)
+    end
+    Book.all_authors.must_include "Kelly Link"
+    Book.all_authors.must_include "Alexandre Dumas"
+    Book.all_authors.size.must_equal 2
+  end
 end
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.