Skip to content

Commit

Permalink
Book with same isbn but different volume/edition
Browse files Browse the repository at this point in the history
  • Loading branch information
multiscan committed Jul 4, 2013
1 parent c0b98a2 commit eda37f4
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 154 deletions.
11 changes: 10 additions & 1 deletion app/controllers/adm/books_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ def new
end
end

# GET /adm/books/id
def duplicate
original = Book.find(params[:book_id])
@book = original.duplicate
respond_to do |format|
format.html
end
end

# GET /adm/books/1/edit
def edit
@book = Book.find(params[:id])
Expand All @@ -48,7 +57,7 @@ def create
@isbn_step = false
render "new"
else
flash[:notice] = "A book with the same ISBN number was found on our database. Please review the data."
flash[:notice] = "A book with the same ISBN number was found on our database. Please review the data or click on duplicate to add a different volume or edition."
render "edit"
end
else
Expand Down
19 changes: 10 additions & 9 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def link_to_nebis(n)
end

def val_or_dash(s)
s.blank? ? "-" : s
s.present? ? s : "-"
end

def item_status(item)
Expand Down Expand Up @@ -239,7 +239,8 @@ def barcode_128(str, ox, oy, w, h, also_text=false)
def item_label(item)
sid=item.id.to_s

d=2.mm # margins
m=4.mm # margins
d=1.mm # min distance between boxes
fs=12 # font size
maxtw=15.mm # max text box width

Expand All @@ -255,18 +256,18 @@ def item_label(item)
]
w = bounds.right - bounds.left
h = bounds.top - bounds.bottom
stroke_bounds
bh = h - 2*d
# stroke_bounds
bh = h - 2*m
hw = 0.5 * w
tw = [lines.map{|e| width_of(e, :size=>fs)}.max, (2*w + 4*d)/3.0, 15.mm].min
y = h - d
x = d
tw = [lines.map{|e| width_of(e, :size=>fs)}.max, 2*(w + d + m)/3.0, 15.mm].min
y = h - m
x = m
text_box(lines.join("\n"), at: [x,y], width: tw, height: bh, :align => :center, :overflow => :shrink_to_fit)
x = hw - 0.5*tw
text_box(lines.join("\n"), at: [x,y], width: tw, height: bh, :align => :center, :overflow => :shrink_to_fit)
x = hw + 0.5*tw+d
rotate(90, :origin => [x,d]) do
barcode_25i(sid, x, d, bh, w-d-x, true)
rotate(90, :origin => [x,m]) do
barcode_25i(sid, x, m, bh, w-m-x, true)
end
end

Expand Down
6 changes: 5 additions & 1 deletion app/models/book.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,14 @@ def self.ask_the_web(n)
lcb=LocBook.new(n)
gbl = GoogleBookList.new(n)
res = gbl.count > 0 ? gbl.to_ah : []
res << lcb.to_h
res << lcb.to_h if lcb.found?
res
end

def duplicate
Book.create attributes.except("id", "created_at", "updated_at").merge(:parent_id => self.id)
end

def self.duplicated_isbn_count
self.select("COUNT(isbn) as total, isbn").group(:isbn).having("COUNT(isbn) > 1 AND isbn!=''").order('total').map{|b| [b.isbn,b.total]}
end
Expand Down
162 changes: 22 additions & 140 deletions app/models/loc_book.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def id
@isbn
end
def title
return nil unless found?
begin
@title ||= @data.locate("titleInfo/title/?[0]")[0]
rescue
Expand All @@ -22,9 +23,11 @@ def title
@title
end
def author
return nil unless found?
authors.join(", ")
end
def authors
return [] unless found?
begin
@authors ||= @data.locate("name/namePart").select {|np| np.attributes[:type]!="date"}.map{|n| n.text}
rescue
Expand All @@ -33,6 +36,7 @@ def authors
@authors
end
def publisher
return nil unless found?
begin
@publisher ||= @data.locate("originInfo/publisher")[0].text
rescue
Expand All @@ -41,6 +45,7 @@ def publisher
@publisher
end
def pubyear
return nil unless found?
begin
@pubyear ||= @data.locate("originInfo/dateIssued")[0].text.gsub(/[^0-9]*/, "")
rescue
Expand All @@ -49,6 +54,7 @@ def pubyear
@pubyear
end
def series
return nil unless found?
begin
@series ||= @data.locate("relatedItem").select{|r| r.locate("@type")[0]=="series"}[0].locate("titleInfo")[0].nodes[0].text
rescue
Expand All @@ -57,6 +63,7 @@ def series
@series
end
def collation
return nil unless found?
begin
@collation ||= @data.locate("physicalDescription/extent")[0].text
rescue
Expand All @@ -69,6 +76,7 @@ def collation
# 'QA166.17 .R34 1992' -> [QA, 166.17, .R34, 1992] = [call1, call2, call3, call4]
#
def lcc
return nil unless found?
begin
@lcc = @data.locate("classification").select{|ca| ca.attributes[:authority]=="lcc"}[0].text
rescue
Expand All @@ -77,21 +85,27 @@ def lcc
@lcc
end
def lcc_array
return [] unless found?
lcc.split(" ")
end
def call1
return nil unless found?
lcc_array[0].gsub(/^([A-Z]+).*$/, '\1')
end
def call2
return nil unless found?
lcc_array[0].gsub(/^[A-Z]*/, '')
end
def call3
return nil unless found?
lcc_array[1] || ""
end
def call4
return nil unless found?
lcc_array[2] || ""
end
def isbns
return [] unless found?
begin
@isbns ||= @data.locate("identifier").select{|i| i.attributes[:type]=="isbn"}.map{|i| i.text.gsub(/ .*$/, '')}
rescue
Expand All @@ -100,16 +114,20 @@ def isbns
@isbns
end
def isbn_10
return nil unless found?
isbns.select {|n| n.length==10}.uniq[0]
end
def isbn_13
return nil unless found?
isbns.select {|n| n.length==13}.uniq[0]
end
def isbn
return nil unless found?
@isbn || isbn_10 || isbn_13
end
def lccn
# library of congress number
return nil unless found?
begin
@lccn ||= @data.locate("identifier").select{|i| i.attributes[:type]=="lccn"}[0].nodes[0]
rescue
Expand All @@ -118,6 +136,7 @@ def lccn
@lccn
end
def uris
return nil unless found?
begin
@uris ||= @data.locate("identifier").select{|i| i.attributes[:type]=="uri"}.map{|u| u.nodes[0]}
rescue
Expand All @@ -126,15 +145,18 @@ def uris
@uris
end
def toc_uri
return nil unless found?
uris.select{ |u| u =~ /-t.html/ }[0]
# @data.locate("location/url").select{|u| u.attributes[:displayLabel]=~/content/}[0].nodes[0]
end
def desc_uri
return nil unless found?
uris.select{ |u| u =~ /-d.html/ }[0]
# @data.locate("location/url").select{|u| u.attributes[:displayLabel]=~/desciption/}[0].nodes[0]
end

def to_h
return {} unless found?
b = {
:title => title,
:isbn => isbn,
Expand All @@ -154,145 +176,5 @@ def to_h
return b
end

# def subtitle
# volinfo["subtitle"]
# end
# def author
# volinfo["authors"].join(", ")
# end
# def description
# volinfo["description"]
# end
# def collation
# unless @collation
# p=[]
# p << "#{pages} pages" if pages
# p << size unless size.empty?
# @collation = p.join("; ")
# end
# @collation
# end
# def isbn_10
# identifiers["ISBN_10"]
# end
# def isbn_13
# identifiers["ISBN_13"]
# end
# def isbn
# isbn_10 || isbn_13
# end
# def pages
# volinfo["pageCount"]
# end
# def categories
# volinfo["categories"]
# end
# def tags
# unless @tags
# c=volinfo["categories"]
# if c
# t=[]
# c.each {|cc| t += cc.split("/")}
# t.delete("General")
# @tags = t.sort.uniq
# else
# @tags=[]
# end
# end
# @tags
# end
# def language
# volinfo["language"]
# end

# def imagelink
# images["large"] || images["medium"] || images["small"] || images["thumbnail"] || images["smallThumbnail"]
# end


private

# def volinfo(i=0)
# @volinfo ||= @data["volumeInfo"] || {}
# end

# def authors
# volinfo["authors"] || []
# end

# def identifiers
# unless @identifiers
# @identifiers = {}
# (volinfo["industryIdentifiers"] || []).each do |i|
# @identifiers[i["type"]] = i["identifier"]
# end
# end
# @identifiers
# end

# def images
# volinfo["imageLinks"] || {}
# end

# def size
# unless @size
# dims=volinfo["dimensions"]
# if dims
# h,w,t=dims["height"], dims["width"], dims["thickness"]
# p=[]
# p << "h: #{h}" if h
# p << "w: #{w}" if w
# p << "t: #{t}" if t
# @size = p.join(", ")
# else
# @size = ""
# end
# end
# @size
# end

# # TODO:
# def parse_date(s)
# s ? s.gsub(/^.*([0-9][0-9][0-9][0-9]).*$/, '\1').to_i : nil
# end
end

# class LocBookList
# def initialize(isbn)
# url="https://www.googleapis.com/books/v1/volumes?q=isbn:#{isbn}"
# @list=ActiveSupport::JSON.decode(open(url).read)
# @count=@list["totalItems"]
# if @count > 0
# # WARNING: I keep only the first page...
# @count = @list["items"].size
# @items=Array.new(@count, nil)
# end
# end

# def all
# @items.each_index {|i| at(i)}
# @items
# end

# def to_ah
# all.map {|i| i.to_h}
# end

# def count
# @count
# end

# def [](i)
# at(i)
# end

# def at(i)
# return nil unless i<@count
# @items[i] ||= LocBook.new(@list["items"][i]["id"])
# end

# def first
# at(0)
# end

# end
19 changes: 19 additions & 0 deletions app/views/adm/books/duplicate.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
-content_for(:title) do
Edit a Book duplicate
%small
ISBN:
=@book.isbn

= simple_form_for([:adm, @book]) do |f|
= f.error_notification

.form-inputs
.row
.span1= f.input :volume, :input_html=>{:min => 1, :max => 10, :class=>"span1"}
.span2= f.input :pubyear, :input_html=>{:min => 1900, :max => 2100, :class=>"span1"}
.span4= f.input :edition, :input_html=>{:class=>"span4"}

=f.input :notes, :label=>false, :input_html=>{:class=>"span10"}

.form-actions
= f.button :submit
4 changes: 3 additions & 1 deletion app/views/adm/books/edit.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
%p
\... or
= link_to 'add an item', new_adm_book_item_path(@book)
to this book.
to this book or
=link_to 'duplicate it', adm_book_duplicate_path(@book)
\.

= render 'form'

Expand Down
Loading

0 comments on commit eda37f4

Please sign in to comment.