Skip to content

Commit

Permalink
Added demonstration resource notes
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarl Friis committed Nov 10, 2010
1 parent ff11980 commit ba9a79c
Show file tree
Hide file tree
Showing 16 changed files with 285 additions and 0 deletions.
83 changes: 83 additions & 0 deletions app/controllers/notes_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
class NotesController < ApplicationController
# GET /notes
# GET /notes.xml
def index
@notes = Note.all

respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @notes }
end
end

# GET /notes/1
# GET /notes/1.xml
def show
@note = Note.find(params[:id])

respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @note }
end
end

# GET /notes/new
# GET /notes/new.xml
def new
@note = Note.new

respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @note }
end
end

# GET /notes/1/edit
def edit
@note = Note.find(params[:id])
end

# POST /notes
# POST /notes.xml
def create
@note = Note.new(params[:note])

respond_to do |format|
if @note.save
format.html { redirect_to(@note, :notice => 'Note was successfully created.') }
format.xml { render :xml => @note, :status => :created, :location => @note }
else
format.html { render :action => "new" }
format.xml { render :xml => @note.errors, :status => :unprocessable_entity }
end
end
end

# PUT /notes/1
# PUT /notes/1.xml
def update
@note = Note.find(params[:id])

respond_to do |format|
if @note.update_attributes(params[:note])
format.html { redirect_to(@note, :notice => 'Note was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @note.errors, :status => :unprocessable_entity }
end
end
end

# DELETE /notes/1
# DELETE /notes/1.xml
def destroy
@note = Note.find(params[:id])
@note.destroy

respond_to do |format|
format.html { redirect_to(notes_url) }
format.xml { head :ok }
end
end
end
2 changes: 2 additions & 0 deletions app/helpers/notes_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module NotesHelper
end
2 changes: 2 additions & 0 deletions app/models/note.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class Note < ActiveRecord::Base
end
17 changes: 17 additions & 0 deletions app/views/layouts/notes.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
<title>Notes: <%= controller.action_name %></title>
<%= stylesheet_link_tag 'scaffold' %>
</head>
<body>

<p style="color: green"><%= notice %></p>

<%= yield %>

</body>
</html>
12 changes: 12 additions & 0 deletions app/views/notes/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<h1>Editing note</h1>

<% form_for(@note) do |f| %>
<%= f.error_messages %>

<p>
<%= f.submit 'Update' %>
</p>
<% end %>

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

<table>
<tr>
</tr>

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

<br />

<%= link_to 'New note', new_note_path %>
11 changes: 11 additions & 0 deletions app/views/notes/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<h1>New note</h1>

<% form_for(@note) do |f| %>
<%= f.error_messages %>

<p>
<%= f.submit 'Create' %>
</p>
<% end %>

<%= link_to 'Back', notes_path %>
3 changes: 3 additions & 0 deletions app/views/notes/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

<%= link_to 'Edit', edit_note_path(@note) %> |
<%= link_to 'Back', notes_path %>
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
ActionController::Routing::Routes.draw do |map|
map.resources :notes

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

# Sample of regular route:
Expand Down
12 changes: 12 additions & 0 deletions db/migrate/20101110122413_create_notes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class CreateNotes < ActiveRecord::Migration
def self.up
create_table :notes do |t|

t.timestamps
end
end

def self.down
drop_table :notes
end
end
1 change: 1 addition & 0 deletions log/development.log
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Logfile created on Wed Nov 10 13:24:12 +0100 2010
54 changes: 54 additions & 0 deletions public/stylesheets/scaffold.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
body { background-color: #fff; color: #333; }

body, p, ol, ul, td {
font-family: verdana, arial, helvetica, sans-serif;
font-size: 13px;
line-height: 18px;
}

pre {
background-color: #eee;
padding: 10px;
font-size: 11px;
}

a { color: #000; }
a:visited { color: #666; }
a:hover { color: #fff; background-color:#000; }

.fieldWithErrors {
padding: 2px;
background-color: red;
display: table;
}

#errorExplanation {
width: 400px;
border: 2px solid red;
padding: 7px;
padding-bottom: 12px;
margin-bottom: 20px;
background-color: #f0f0f0;
}

#errorExplanation h2 {
text-align: left;
font-weight: bold;
padding: 5px 5px 5px 15px;
font-size: 12px;
margin: -7px;
background-color: #c00;
color: #fff;
}

#errorExplanation p {
color: #333;
margin-bottom: 0;
padding: 5px;
}

#errorExplanation ul li {
font-size: 12px;
list-style: square;
}

11 changes: 11 additions & 0 deletions test/fixtures/notes.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html

# This model initially had no columns defined. If you add columns to the
# model remove the '{}' from the fixture names and add the columns immediately
# below each fixture, per the syntax in the comments below
#
one: {}
# column: value
#
two: {}
# column: value
45 changes: 45 additions & 0 deletions test/functional/notes_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
require 'test_helper'

class NotesControllerTest < ActionController::TestCase
test "should get index" do
get :index
assert_response :success
assert_not_nil assigns(:notes)
end

test "should get new" do
get :new
assert_response :success
end

test "should create note" do
assert_difference('Note.count') do
post :create, :note => { }
end

assert_redirected_to note_path(assigns(:note))
end

test "should show note" do
get :show, :id => notes(:one).to_param
assert_response :success
end

test "should get edit" do
get :edit, :id => notes(:one).to_param
assert_response :success
end

test "should update note" do
put :update, :id => notes(:one).to_param, :note => { }
assert_redirected_to note_path(assigns(:note))
end

test "should destroy note" do
assert_difference('Note.count', -1) do
delete :destroy, :id => notes(:one).to_param
end

assert_redirected_to notes_path
end
end
4 changes: 4 additions & 0 deletions test/unit/helpers/notes_helper_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require 'test_helper'

class NotesHelperTest < ActionView::TestCase
end
8 changes: 8 additions & 0 deletions test/unit/note_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require 'test_helper'

class NoteTest < ActiveSupport::TestCase
# Replace this with your real tests.
test "the truth" do
assert true
end
end

0 comments on commit ba9a79c

Please sign in to comment.