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
3 changes: 3 additions & 0 deletions app/assets/javascripts/detailmemos.js.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# 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/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/detailmemos.css.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the detailmemos controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
8 changes: 8 additions & 0 deletions app/controllers/books_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class BooksController < ApplicationController
before_filter :find_book, :only => [:show, :destroy, :edit, :update]
before_filter :find_memo, :only => :show

# GET /books
# GET /books.json
Expand Down Expand Up @@ -89,4 +90,11 @@ def destroy
def find_book
@book = Book.find(params[:id])
end

private
def find_memo
@memos = Detailmemo.find_all_by_book_id(params[:id])
p @memos
end

end
40 changes: 40 additions & 0 deletions app/controllers/detailmemos_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
class DetailmemosController < ApplicationController
# GET /detailmemos/new
# GET /detailmemos/new.json
def new
@detailmemo = Detailmemo.new

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

# POST /detailmemos
# POST /detailmemos.json
def create
@detailmemo = Detailmemo.new(params[:detailmemo])

respond_to do |format|
if @detailmemo.save
format.html { redirect_to Book, notice: 'Detailmemo was successfully created.' }
format.json { render json: @detailmemo, status: :created, location: @detailmemo }
else
format.html { render action: "new" }
format.json { render json: @detailmemo.errors, status: :unprocessable_entity }
end
end
end

# DELETE /detailmemos/1
# DELETE /detailmemos/1.json
def destroy
@detailmemo = Detailmemo.find(params[:id])
@detailmemo.destroy

respond_to do |format|
format.html { redirect_to book_path(@detailmemo[:book_id]) }
format.json { head :no_content }
end
end
end
2 changes: 2 additions & 0 deletions app/helpers/detailmemos_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module DetailmemosHelper
end
1 change: 1 addition & 0 deletions app/models/book.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# encoding: UTF-8
class Book < ActiveRecord::Base
has_many :detailmemos
attr_accessible :memo, :purchased_on, :title
validates :title, :presence => true

Expand Down
4 changes: 4 additions & 0 deletions app/models/detailmemo.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Detailmemo < ActiveRecord::Base
validates :memo, :length => { :maximum => 100}
attr_accessible :book_id, :memo
end
14 changes: 13 additions & 1 deletion app/views/books/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,24 @@
<b>Memo:</b>
<%= @book.memo %>
</p>
<% if @memos %>
<% for memo in @memos %>
<p class="memo">
<%= memo.memo %>
<%= link_to 'Delete', memo,:controller => 'detailmemos', :action => 'destroy', confirm: 'Are you sure?', :method => :delete %>

</p>
<% end %>
<% end %>

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


<span id="add_memo">
<%= link_to('Add Memo', :controller => 'detailmemos', :action => 'new',
:bookid => @book) %>
</span>
<%= link_to 'Edit', edit_book_path(@book) %> |
<%= link_to 'Back', books_path %>
24 changes: 24 additions & 0 deletions app/views/detailmemos/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<%= form_for(@detailmemo) do |f| %>
<% if @detailmemo.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@detailmemo.errors.count, "error") %> prohibited this detailmemo from being saved:</h2>

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

<div class="field">
<%= f.hidden_field(:book_id,:value => params[:bookid])%>
</div>
<div class="field">
<%= f.label :memo %><br />
<%= f.text_area :memo %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
5 changes: 5 additions & 0 deletions app/views/detailmemos/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<h1>Editing detailmemo</h1>

<%= render 'form' %>

<%= link_to 'Back', book_path %>
5 changes: 5 additions & 0 deletions app/views/detailmemos/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<h1>New detailmemo</h1>

<%= render 'form' %>

<%= link_to 'Back', books_path %>
15 changes: 15 additions & 0 deletions app/views/detailmemos/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<p id="notice"><%= notice %></p>

<p>
<b>Book:</b>
<%= @detailmemo.book_id %>
</p>

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


<%= link_to 'Edit', edit_detailmemo_path(@detailmemo) %> |
<%= link_to 'Back', books_path %>
3 changes: 2 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
BookMemo2::Application.routes.draw do
resources :books

resources :detailmemos
resources :books
# The priority is based upon order of creation:
# first created -> highest priority.

Expand Down
10 changes: 10 additions & 0 deletions db/migrate/20120614162548_create_detailmemos.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateDetailmemos < ActiveRecord::Migration
def change
create_table :detailmemos do |t|
t.integer :book_id
t.text :memo

t.timestamps
end
end
end
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 => 20120614162548) 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 "detailmemos", :force => true do |t|
t.integer "book_id"
t.text "memo"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end

end
96 changes: 96 additions & 0 deletions spec/controllers/detailmemos_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 DetailmemosController do

# This should return the minimal set of attributes required to create a valid
# Detailmemo. As you add validations to Detailmemo, 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
# DetailmemosController. Be sure to keep this updated too.
def valid_session
{}
end

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

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

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

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

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

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

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

it "redirects to the detailmemos list" do
detailmemo = Detailmemo.create! valid_attributes
delete :destroy, {:id => detailmemo.to_param}, valid_session
response.should redirect_to(book_path(1))
end
end

end
8 changes: 8 additions & 0 deletions spec/factories/detailmemos.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Read about factories at https://github.com/thoughtbot/factory_girl

FactoryGirl.define do
factory :detailmemo do
book_id 1
sequence(:memo) { |n| "MyText_#{n}" }
end
end
15 changes: 15 additions & 0 deletions spec/helpers/detailmemos_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'spec_helper'

# Specs in this file have access to a helper object that includes
# the DetailmemosHelper. For example:
#
# describe DetailmemosHelper do
# describe "string concat" do
# it "concats two strings with spaces" do
# helper.concat_strings("this","that").should == "this that"
# end
# end
# end
describe DetailmemosHelper do
pending "add some examples to (or delete) #{__FILE__}"
end
Binary file added spec/models/.detailmemo_spec.rb.swp
Binary file not shown.
16 changes: 16 additions & 0 deletions spec/models/detailmemo_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require 'spec_helper'

describe Detailmemo do
pending "add some examples to (or delete) #{__FILE__}"
context "under 100" do
memo = "a" * 100
subject { Detailmemo.new(:memo => memo).valid?}
it { should be_true}
end

context "under 100" do
memo = "a" * 101
subject { Detailmemo.new(:memo => memo).valid?}
it { should be_false}
end
end
53 changes: 53 additions & 0 deletions spec/requests/detailmemos_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# -*- coding: utf-8 -*-
require 'spec_helper'

describe "Detailmemos" do

describe '/books/:book_id/' do
let!(:book){ FactoryGirl.create :book }
let!(:detailmemo){ FactoryGirl.create :detailmemo }
subject { page }

before { visit "/books/#{book.id}" }
it "show Add Memo link" do
page.should have_css("#add_memo",:text => "Add Memo")
end

context "write memo1" do
let(:memo_data){ 'memo test 1' }

before do
click_on 'Add Memo'
fill_in "detailmemo_memo", with: memo_data
click_on 'Create Detailmemo'
end

it "page is moved" do
current_path.should == books_path
end

it "memo1 is exist" do
visit "/books/#{book.id}"
page.should have_content(memo_data)
end
end
context "write memo2" do
let(:memo_data){ 'memo test 2' }

before do
click_on 'Add Memo'
fill_in "detailmemo_memo", with: memo_data
click_on 'Create Detailmemo'
end

it "page is moved" do
current_path.should == books_path
end

it "memo2 is exist" do
visit "/books/#{book.id}"
page.should have_content(memo_data)
end
end
end
end
Loading