Skip to content
Open
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
2 changes: 2 additions & 0 deletions app/controllers/books_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ def index
# GET /books/1
# GET /books/1.json
def show
session[:book_id][email protected]

respond_to do |format|
format.html # show.html.erb
format.xml { render xml: @book }
Expand Down
75 changes: 75 additions & 0 deletions app/controllers/memos_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
class MemosController < ApplicationController

# GET /memos/new
# GET /memos/new.json
def new
#Return to mainmenu if book value in session is nil
if session[:book_id].nil?
redirect_to books_path
return
end

@memo = Memo.new
@book = Book.find(session[:book_id])
@memo.book_id = @book.id

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

# POST /memos
# POST /memos.json
def create
#Return to mainmenu if book value in session is nil
if session[:book_id].nil?
redirect_to books_path
return
end

@memo = Memo.new(params[:memo])
@book = Book.find(session[:book_id])
@memo.book_id = @book.id

#Return to mainmenu if already logged in
if @memo.book_id.nil?
redirect_to @book
return
end

respond_to do |format|
if @memo.save
format.html { redirect_to @book }
format.xml { render xml: @memo, status: :created, location: @memo }
format.json { render json: @memo, status: :created, location: @memo }
else
format.html { render action: "new" }
format.xml { render xml: @memo.errors, status: :unprocessable_entity }
format.json { render json: @memo.errors, status: :unprocessable_entity }
end
end
end

# DELETE /memos/1
# DELETE /memos/1.json
def destroy
#Return to mainmenu if book value in session is nil
if session[:book_id].nil?
redirect_to books_path
return
end

@memo = Memo.find(params[:id])
@book = Book.find(session[:book_id])
@memo.destroy

respond_to do |format|
format.html { redirect_to @book }
format.xml { head :no_content }
format.json { head :no_content }
end
end

end
2 changes: 2 additions & 0 deletions app/helpers/memos_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module MemosHelper
end
5 changes: 3 additions & 2 deletions app/models/book.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# encoding: UTF-8
class Book < ActiveRecord::Base
attr_accessible :memo, :purchased_on, :title
has_many :memos, :dependent => :destroy
attr_accessible :purchased_on, :title
validates :title, :presence => true

before_create :total_books_count

def total_books_count
self.memo += "【 累計冊数#{Book.count + 1} 】"
#self.memo += "【 累計冊数#{Book.count + 1} 】"
end
end
7 changes: 7 additions & 0 deletions app/models/memo.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Memo < ActiveRecord::Base
attr_accessible :content

validates_length_of :content, :maximum => 100, :allow_blank => false, :message => "Memo must not be empty and must contain less than 100 characters"

belongs_to :book
end
4 changes: 0 additions & 4 deletions app/views/books/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :memo %><br />
<%= f.text_area :memo %>
</div>
<div class="field">
<%= f.label :purchased_on %><br />
<%= f.date_select :purchased_on %>
Expand Down
4 changes: 2 additions & 2 deletions app/views/books/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<table>
<tr>
<th>Title</th>
<th>Memo</th>

<th>Purchased on</th>
<th></th>
<th></th>
Expand All @@ -13,7 +13,7 @@
<% @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>
Expand Down
19 changes: 14 additions & 5 deletions app/views/books/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,25 @@
<%= @book.title %>
</p>

<p>
<b>Memo:</b>
<%= @book.memo %>
</p>

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

<table border="1" cellspacing="0" cellpadding="5">
<tr>
<th width="300px">Memo (<%= link_to 'Add new', new_memo_path %>)</th>
<th width="50px">&nbsp;</th>
</tr>

<% @book.memos.each do |memo| %>
<tr>
<td><%= memo.content %></td>
<td><%= link_to 'Delete', memo, confirm: 'Are you sure?', method: :delete %></td>
</tr>
<% end %>
</table>


<%= link_to 'Edit', edit_book_path(@book) %> |
<%= link_to 'Back', books_path %>
21 changes: 21 additions & 0 deletions app/views/memos/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<%= form_for(@memo) do |f| %>
<% if @memo.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@memo.errors.count, "memo") %> prohibited this memo from being saved:</h2>

<ul>
<% @memo.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>

<div class="field">
<%= f.label :content %><br />
<%= f.text_area :content %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
5 changes: 5 additions & 0 deletions app/views/memos/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<h1>New memo</h1>

<%= render 'form' %>

<%= link_to 'Back', @book %>
4 changes: 2 additions & 2 deletions config/initializers/session_store.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Be sure to restart your server when you modify this file.

BookMemo2::Application.config.session_store :cookie_store, key: '_book_memo2_session'
#BookMemo2::Application.config.session_store :cookie_store, key: '_book_memo2_session'

# Use the database for sessions instead of the cookie-based default,
# which shouldn't be used to store highly confidential information
# (create the session table with "rails generate session_migration")
# BookMemo2::Application.config.session_store :active_record_store
BookMemo2::Application.config.session_store :active_record_store
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
BookMemo2::Application.routes.draw do
resources :books
resources :memos

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

t.timestamps
Expand Down
12 changes: 12 additions & 0 deletions db/migrate/20120614114819_create_memos.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class CreateMemos < ActiveRecord::Migration
def change
create_table :memos do |t|

t.integer :book_id, :null =>false, :options =>
"CONSTRAINT fk_memo_books REFERENCES books(id)"
t.text :content

t.timestamps
end
end
end
12 changes: 12 additions & 0 deletions db/migrate/20120614164222_add_sessions_table.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class AddSessionsTable < ActiveRecord::Migration
def change
create_table :sessions do |t|
t.string :session_id, :null => false
t.text :data
t.timestamps
end

add_index :sessions, :session_id
add_index :sessions, :updated_at
end
end
20 changes: 18 additions & 2 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,30 @@
#
# It's strongly recommended to check this file into your version control system.

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

create_table "books", :force => true do |t|
t.string "title"
t.text "memo"
t.date "purchased_on"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end

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

create_table "sessions", :force => true do |t|
t.string "session_id", :null => false
t.text "data"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end

add_index "sessions", ["session_id"], :name => "index_sessions_on_session_id"
add_index "sessions", ["updated_at"], :name => "index_sessions_on_updated_at"

end
2 changes: 1 addition & 1 deletion spec/controllers/books_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
# Book. As you add validations to Book, be sure to
# update the return value of this method accordingly.
def valid_attributes
{ title: 'book title', memo: '' }
{ title: 'book title'}
end

# This should return the minimal set of values that should be in the session
Expand Down
96 changes: 96 additions & 0 deletions spec/controllers/memos_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
require 'spec_helper'

# This spec was generated by rspec-rails when you ran the scaffold generator.
# It demonstrates how one might use RSpec to specify the controller code that
# was generated by Rails when you ran the scaffold generator.
#
# It assumes that the implementation code is generated by the rails scaffold
# generator. If you are using any extension libraries to generate different
# controller code, this generated spec may or may not pass.
#
# It only uses APIs available in rails and/or rspec-rails. There are a number
# of tools you can use to make these specs even more expressive, but we're
# sticking to rails and rspec-rails APIs to keep things simple and stable.
#
# Compared to earlier versions of this generator, there is very limited use of
# stubs and message expectations in this spec. Stubs are only used when there
# is no simpler way to get a handle on the object needed for the example.
# Message expectations are only used when there is no simpler way to specify
# that an instance is receiving a specific message.

describe MemosController do

# This should return the minimal set of attributes required to create a valid
# Memo. As you add validations to Memo, be sure to
# update the return value of this method accordingly.
def valid_attributes
{}
end

# This should return the minimal set of values that should be in the session
# in order to pass any filters (e.g. authentication) defined in
# MemosController. Be sure to keep this updated too.
def valid_session
{}
end

describe "GET new" do
it "assigns a new memo as @memo" do
get :new, {}, valid_session
assigns(:memo).should be_a_new(Memo)
end
end

describe "POST create" do
describe "with valid params" do
it "creates a new Memo" do
expect {
post :create, {:memo => valid_attributes}, valid_session
}.to change(Memo, :count).by(1)
end

it "assigns a newly created memo as @memo" do
post :create, {:memo => valid_attributes}, valid_session
assigns(:memo).should be_a(Memo)
assigns(:memo).should be_persisted
end

it "redirects to the created memo" do
post :create, {:memo => valid_attributes}, valid_session
response.should redirect_to(Memo.last)
end
end

describe "with invalid params" do
it "assigns a newly created but unsaved memo as @memo" do
# Trigger the behavior that occurs when invalid params are submitted
Memo.any_instance.stub(:save).and_return(false)
post :create, {:memo => {}}, valid_session
assigns(:memo).should be_a_new(Memo)
end

it "re-renders the 'new' template" do
# Trigger the behavior that occurs when invalid params are submitted
Memo.any_instance.stub(:save).and_return(false)
post :create, {:memo => {}}, valid_session
response.should render_template("new")
end
end
end

describe "DELETE destroy" do
it "destroys the requested memo" do
memo = Memo.create! valid_attributes
expect {
delete :destroy, {:id => memo.to_param}, valid_session
}.to change(Memo, :count).by(-1)
end

it "redirects to the memos list" do
memo = Memo.create! valid_attributes
delete :destroy, {:id => memo.to_param}, valid_session
response.should redirect_to(memos_url)
end
end

end
2 changes: 1 addition & 1 deletion spec/factories/books.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
FactoryGirl.define do
factory :book do
sequence(:title) { |n| "title_#{n}" }
memo "this is memo"
#memo "this is memo"
purchased_on Time.now
end
end
Loading