Permalink
Browse files

Book!

  • Loading branch information...
1 parent 5b4c07e commit a05ec21a6768a0c437f649f54a9e0afeb9b64618 @sorah sorah committed Jan 12, 2011
Showing with 25 additions and 6 deletions.
  1. +13 −1 app/models/account.rb
  2. +1 −4 app/views/accounts/_account.html.erb
  3. +9 −0 db/migrate/20110112055458_add_isbn_to_books.rb
  4. +2 −1 db/schema.rb
View
14 app/models/account.rb
@@ -47,7 +47,19 @@ def self.import io
end
(record['ruby-books'] || []).each do |key|
- account.books.create!(:key => key)
+ isbn10 = key[3..11]
+ check_digit = 0
+ isbn10.split(//).each_with_index do |c, i|
+ check_digit += c.to_i * (10 - i)
+ end
+ check_digit = 11 - (check_digit % 11)
+ check_digit = case check_digit
+ when 10; "X"
+ when 11; 0
+ else; check_digit
+ end
+ isbn10 = "#{isbn10}#{check_digit}"
+ account.books.create!(:key => key, :isbn => isbn10)
end
end
end
View
5 app/views/accounts/_account.html.erb
@@ -60,20 +60,17 @@
</tr>
<% end %>
- <!-- FIXME: unsure how to link to ISBN. :-( -->
- <!--
<% unless account.books.empty? %>
<tr>
<td>Books:</td>
<td>
<ul>
<% account.books.each do |book| %>
- <li><%= book.key %></li>
+ <li><a href="http://www.amazon.<%= book.isbn.match(/^4/) ? "co.jp":"com" %>/dp/<%= book.isbn %>"><%= book.key %></a></li>
<% end %>
</ul>
</td>
</tr>
<% end %>
- -->
</table>
</li>
View
9 db/migrate/20110112055458_add_isbn_to_books.rb
@@ -0,0 +1,9 @@
+class AddIsbnToBooks < ActiveRecord::Migration
+ def self.up
+ add_column :books, :isbn, :string
+ end
+
+ def self.down
+ remove_column :books, :isbn
+ end
+end
View
3 db/schema.rb
@@ -10,7 +10,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20110101002342) do
+ActiveRecord::Schema.define(:version => 20110112055458) do
create_table "accounts", :force => true do |t|
t.string "username"
@@ -23,6 +23,7 @@
t.integer "account_id"
t.datetime "created_at"
t.datetime "updated_at"
+ t.string "isbn"
end
create_table "names", :force => true do |t|

0 comments on commit a05ec21

Please sign in to comment.