Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Homework #14

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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: 2 additions & 1 deletion .rspec
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
--drb
--colour
--format=d
--format=d
5 changes: 5 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ group :test, :development do
gem "rspec-rails", "~> 2.0"
gem 'capybara'
gem 'launchy'
gem 'guard', '0.10.0'
gem 'guard-rspec'
gem 'guard-spork'
gem 'libnotify'
gem 'rb-inotify'
end
gem 'pry-rails'
gem 'factory_girl_rails'
18 changes: 18 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ GEM
factory_girl (~> 3.3.0)
railties (>= 3.0.0)
ffi (1.0.11)
guard (0.10.0)
ffi (>= 0.5.0)
thor (~> 0.14.6)
guard-rspec (0.7.3)
guard (>= 0.10.0)
guard-spork (0.8.0)
guard (>= 0.10.0)
spork (>= 0.8.4)
hike (1.2.1)
i18n (0.6.0)
journey (1.0.3)
Expand All @@ -67,6 +75,8 @@ GEM
json (1.7.3)
launchy (2.1.0)
addressable (~> 2.2.6)
libnotify (0.7.2)
ffi (~> 1.0.0)
libwebsocket (0.1.3)
addressable
mail (2.4.4)
Expand Down Expand Up @@ -107,6 +117,8 @@ GEM
rdoc (~> 3.4)
thor (~> 0.14.6)
rake (0.9.2.2)
rb-inotify (0.8.8)
ffi (>= 0.5.0)
rdoc (3.12)
json (~> 1.4)
rspec (2.10.0)
Expand Down Expand Up @@ -135,6 +147,7 @@ GEM
multi_json (~> 1.0)
rubyzip
slop (2.4.4)
spork (0.9.2)
sprockets (2.1.3)
hike (~> 1.2)
rack (~> 1.0)
Expand All @@ -159,10 +172,15 @@ DEPENDENCIES
capybara
coffee-rails (~> 3.2.1)
factory_girl_rails
guard (= 0.10.0)
guard-rspec
guard-spork
jquery-rails
launchy
libnotify
pry-rails
rails (= 3.2.3)
rb-inotify
rspec-rails (~> 2.0)
sass-rails (~> 3.2.3)
sqlite3
Expand Down
31 changes: 31 additions & 0 deletions Guardfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# A sample Guardfile
# More info at https://github.com/guard/guard#readme

guard 'spork', :cucumber_env => { 'RAILS_ENV' => 'test' }, :rspec_env => { 'RAILS_ENV' => 'test' } do
watch('config/application.rb')
watch('config/environment.rb')
watch(%r{^config/environments/.+\.rb$})
watch(%r{^config/initializers/.+\.rb$})
watch('Gemfile')
watch('Gemfile.lock')
watch('spec/spec_helper.rb') { :rspec }
watch('test/test_helper.rb') { :test_unit }
watch(%r{features/support/}) { :cucumber }
end

guard 'rspec', :version => 2 do
watch(%r{^spec/.+_spec\.rb$})
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
watch('spec/spec_helper.rb') { "spec" }

# Rails example
watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
watch(%r{^app/(.*)(\.erb|\.haml)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
watch('config/routes.rb') { "spec/routing" }
watch('app/controllers/application_controller.rb') { "spec/controllers" }
# Capybara request specs
watch(%r{^app/views/(.+)/.*\.(erb|haml)$}) { |m| "spec/requests/#{m[1]}_spec.rb" }
end

11 changes: 11 additions & 0 deletions app/assets/javascripts/books.js.coffee
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
# 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/

count = 0
$("#add-memo").on("click", () ->
$('#memo').append($('<div />').addClass('field')).append($('<textarea />').attr({
'cols': '40',
'name': "book[memo_attributes][#{count++}][value]",
'rows': '5'
}))
return false

)
3 changes: 3 additions & 0 deletions app/assets/javascripts/memos.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/memos.css.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the memos controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
30 changes: 30 additions & 0 deletions app/controllers/memos_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class MemosController < ApplicationController

# POST /memos
# POST /memos.json
def create
@memo = Memo.new(params[:memo])

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

# DELETE /memos/1
# DELETE /memos/1.json
def destroy
@memo = Memo.find(params[:id])
@memo.destroy

respond_to do |format|
#format.html { redirect_to memos_url }
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
11 changes: 9 additions & 2 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
attr_accessible :memo_attributes, :purchased_on, :title, :memo
validates :title, :presence => true

before_create :total_books_count

has_many :memo, dependent: :destroy
accepts_nested_attributes_for :memo

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

belongs_to :book
end
12 changes: 9 additions & 3 deletions app/views/books/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,16 @@
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :memo %><br />
<%= f.text_area :memo %>
<div id="memo">
<% @book.memo.each do |m| %>
<%= fields_for m do |mm| %>
<div class="field">
<%= mm.text_area :value %>
</div>
<% end %>
<% end %>
</div>
<button id="add-memo">Add memo</button>
<div class="field">
<%= f.label :purchased_on %><br />
<%= f.date_select :purchased_on %>
Expand Down
1 change: 1 addition & 0 deletions app/views/books/_memo_view.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<li><%= memo.value if memo %></li>
2 changes: 1 addition & 1 deletion app/views/books/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<% @books.each do |book| %>
<tr>
<td><%= book.title %></td>
<td><%= book.memo %></td>
<td><ul><%= render partial: 'memo_view', collection: book.memo, as: :memo %></ul></td>
<td><%= book.purchased_on %></td>
<td><%= link_to 'Show', book %></td>
<td><%= link_to 'Edit', edit_book_path(book) %></td>
Expand Down
4 changes: 3 additions & 1 deletion app/views/books/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

<p>
<b>Memo:</b>
<%= @book.memo %>
<ul>
<%= render partial: 'memo_view', collection: @book.memo, as: :memo %>
</ul>
</p>

<p>
Expand Down
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,12 +3,12 @@
<head>
<title>BookMemo2</title>
<%= stylesheet_link_tag "application", :media => "all" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
</head>
<body>

<%= yield %>

<%= javascript_include_tag "application" %>
</body>
</html>
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, "error") %> 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 :value %><br />
<%= f.text_field :value %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
6 changes: 6 additions & 0 deletions app/views/memos/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<h1>Editing memo</h1>

<%= render 'form' %>

<%= link_to 'Show', @memo %> |
<%= link_to 'Back', memos_path %>
23 changes: 23 additions & 0 deletions app/views/memos/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<h1>Listing memos</h1>

<table>
<tr>
<th>Value</th>
<th></th>
<th></th>
<th></th>
</tr>

<% @memos.each do |memo| %>
<tr>
<td><%= memo.value %></td>
<td><%= link_to 'Show', memo %></td>
<td><%= link_to 'Edit', edit_memo_path(memo) %></td>
<td><%= link_to 'Destroy', memo, confirm: 'Are you sure?', method: :delete %></td>
</tr>
<% end %>
</table>

<br />

<%= link_to 'New Memo', new_memo_path %>
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', memos_path %>
10 changes: 10 additions & 0 deletions app/views/memos/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<p id="notice"><%= notice %></p>

<p>
<b>Value:</b>
<%= @memo.value %>
</p>


<%= link_to 'Edit', edit_memo_path(@memo) %> |
<%= link_to 'Back', memos_path %>
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, only: [:create, :destroy]

resources :books

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

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

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

ActiveRecord::Schema.define(:version => 20120526050801) do
ActiveRecord::Schema.define(:version => 20120615071239) 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"
t.string "value"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end

end
Loading