Skip to content
Open

test #18

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>book_memo2</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
</buildSpec>
<natures>
<nature>org.radrails.rails.core.railsnature</nature>
</natures>
</projectDescription>
9 changes: 9 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,17 @@ GEM
mime-types (1.18)
multi_json (1.3.5)
nokogiri (1.5.2)
nokogiri (1.5.2-x86-mingw32)
polyglot (0.3.3)
pry (0.9.9.6)
coderay (~> 1.0.5)
method_source (~> 0.7.1)
slop (>= 2.4.4, < 3)
pry (0.9.9.6-x86-mingw32)
coderay (~> 1.0.5)
method_source (~> 0.7.1)
slop (>= 2.4.4, < 3)
win32console (~> 1.3)
pry-rails (0.1.6)
pry
rack (1.4.1)
Expand Down Expand Up @@ -140,6 +146,7 @@ GEM
rack (~> 1.0)
tilt (~> 1.1, != 1.3.0)
sqlite3 (1.3.6)
sqlite3 (1.3.6-x86-mingw32)
thor (0.14.6)
tilt (1.3.3)
treetop (1.4.10)
Expand All @@ -149,11 +156,13 @@ GEM
uglifier (1.2.4)
execjs (>= 0.3.0)
multi_json (>= 1.0.2)
win32console (1.3.2-x86-mingw32)
xpath (0.1.4)
nokogiri (~> 1.3)

PLATFORMS
ruby
x86-mingw32

DEPENDENCIES
capybara
Expand Down
3 changes: 3 additions & 0 deletions app/assets/javascripts/books.js.coffee
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/



11 changes: 4 additions & 7 deletions app/controllers/books_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,7 @@ def show
# GET /books/new.json
def new
@book = Book.new

respond_to do |format|
format.html # new.html.erb
format.xml { render xml: @book }
format.json { render json: @book }
end
@book.memos.build
end

# GET /books/1/edit
Expand All @@ -43,7 +38,9 @@ def edit
# POST /books.json
def create
@book = Book.new(params[:book])

params[:memos].each do |memo|
@book.memos.build(memo)
end
respond_to do |format|
if @book.save
format.html { redirect_to @book, notice: 'Book was successfully created.' }
Expand Down
1 change: 1 addition & 0 deletions app/helpers/books_helper.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
module BooksHelper

end
15 changes: 11 additions & 4 deletions app/models/book.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
# encoding: UTF-8
class Book < ActiveRecord::Base
attr_accessible :memo, :purchased_on, :title

has_many :memos, :dependent => :destroy
accepts_nested_attributes_for :memos
attr_accessible :purchased_on, :title, :memos, :id
validates :title, :presence => true

before_create :total_books_count

def total_books_count
self.memo += "【 累計冊数#{Book.count + 1} 】"

def new_memo_attributes=(memo_attributes)
memo_attributes.each do |attributes|
memos.build(attributes)
end
end


end
19 changes: 15 additions & 4 deletions app/views/books/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,18 @@
</div>
<% end %>

<div class="field">
<div class="field" id="title">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :memo %><br />
<%= f.text_area :memo %>
<div id="memos">
Memo:<br>
<input name="memos[][memo]" size="30" type="text"/><br>
<button id="addYunyun" type="button" onClick="yunyun()">Add Memo</button>

</div>


<div class="field">
<%= f.label :purchased_on %><br />
<%= f.date_select :purchased_on %>
Expand All @@ -27,3 +31,10 @@
<%= f.submit %>
</div>
<% end %>
<script>
var yunyun = function(){
$('#addYunyun').before('<input name="memos[][memo]" size="30" type="text"/><br>');
}
</script>


2 changes: 0 additions & 2 deletions app/views/books/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@
<% @books.each do |book| %>
<tr>
<td><%= book.title %></td>
<td><%= book.memo %></td>
<td><%= book.purchased_on %></td>
<td><%= link_to 'Show', book %></td>
<td><%= link_to 'Edit', edit_book_path(book) %></td>
<td><%= link_to 'Destroy', book, confirm: 'Are you sure?', method: :delete %></td>
</tr>
<% end %>
Expand Down
14 changes: 9 additions & 5 deletions app/views/books/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,21 @@
<b>Title:</b>
<%= @book.title %>
</p>

<p>
<b>Memo:</b>
<%= @book.memo %>
<b>Memos:</b><br>
<table>
<% @book.memos.each do |m| %>
<tr>
<td><%= m.memo %></td>

</tr>
<% end %>
</table>
</p>

<p>
<b>Purchased on:</b>
<%= @book.purchased_on %>
</p>


<%= link_to 'Edit', edit_book_path(@book) %> |
<%= link_to 'Back', books_path %>
2 changes: 1 addition & 1 deletion app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<title>BookMemo2</title>
<%= stylesheet_link_tag "application", :media => "all" %>
<%= javascript_include_tag "application" %>
<%= javascript_include_tag :application %>
<%= csrf_meta_tags %>
</head>
<body>
Expand Down
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
BookMemo2::Application.routes.draw do
resources :memos

resources :books

# The priority is based upon order of creation:
Expand Down
2 changes: 1 addition & 1 deletion db/migrate/20120526050801_create_books.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
class CreateBooks < ActiveRecord::Migration
def change
create_table :books do |t|
t.string :id
t.string :title
t.text :memo
t.date :purchased_on

t.timestamps
Expand Down
9 changes: 8 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20120526050801) do
ActiveRecord::Schema.define(:version => 20120612123218) do

create_table "books", :force => true do |t|
t.string "title"
Expand All @@ -21,4 +21,11 @@
t.datetime "updated_at", :null => false
end

create_table "memos", :force => true do |t|
t.text "memo"
t.string "book_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end

end
8 changes: 1 addition & 7 deletions spec/models/book_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,5 @@
end
end

describe '#total_books_count' do
let(:book){ FactoryGirl.build :book }
before { book.save }
it "memoに累計冊数が表示されること" do
book.memo.should include "【 累計冊数#{Book.count} 】"
end
end

end
15 changes: 15 additions & 0 deletions spec/requests/books_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,21 @@
end
end

describe "POST /books" do
it "creates a book" do
post_via_redirect books_path,
:book=>{:title=>"The road to the future",
:memos=>[{:memo=>"aaa"},{:memo=>"bbb"}],
:purchased_on(1i)=>"2012",
:purchased_on(2i)=>"6",
:purchased_on(3i)=>"13"
}
response.body.should include("mow lawn")
end
end



describe '/books/:id/edit' do
let!(:book){ FactoryGirl.create :book }
subject { page }
Expand Down