From 58b95a37c52fd21040b3b2070bd671242411d68d Mon Sep 17 00:00:00 2001
From: Per Cederqvist <ceder@lysator.liu.se>
Date: Wed, 7 Jun 2006 21:11:40 +0000
Subject: [PATCH] After running './script/generate scaffold User'

---
 app/controllers/users_controller.rb      | 51 ++++++++++++++
 app/helpers/users_helper.rb              |  2 +
 app/models/user.rb                       |  2 +
 app/views/layouts/users.rhtml            | 13 ++++
 app/views/users/_form.rhtml              |  7 ++
 app/views/users/edit.rhtml               |  9 +++
 app/views/users/list.rhtml               | 27 ++++++++
 app/views/users/new.rhtml                |  8 +++
 app/views/users/show.rhtml               |  8 +++
 public/stylesheets/scaffold.css          | 74 ++++++++++++++++++++
 test/fixtures/users.yml                  |  5 ++
 test/functional/users_controller_test.rb | 88 ++++++++++++++++++++++++
 test/unit/user_test.rb                   | 10 +++
 13 files changed, 304 insertions(+)
 create mode 100644 app/controllers/users_controller.rb
 create mode 100644 app/helpers/users_helper.rb
 create mode 100644 app/models/user.rb
 create mode 100644 app/views/layouts/users.rhtml
 create mode 100644 app/views/users/_form.rhtml
 create mode 100644 app/views/users/edit.rhtml
 create mode 100644 app/views/users/list.rhtml
 create mode 100644 app/views/users/new.rhtml
 create mode 100644 app/views/users/show.rhtml
 create mode 100644 public/stylesheets/scaffold.css
 create mode 100644 test/fixtures/users.yml
 create mode 100644 test/functional/users_controller_test.rb
 create mode 100644 test/unit/user_test.rb

diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb
new file mode 100644
index 0000000..916d8bc
--- /dev/null
+++ b/app/controllers/users_controller.rb
@@ -0,0 +1,51 @@
+class UsersController < ApplicationController
+  def index
+    list
+    render :action => 'list'
+  end
+
+  # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
+  verify :method => :post, :only => [ :destroy, :create, :update ],
+         :redirect_to => { :action => :list }
+
+  def list
+    @user_pages, @users = paginate :users, :per_page => 10
+  end
+
+  def show
+    @user = User.find(params[:id])
+  end
+
+  def new
+    @user = User.new
+  end
+
+  def create
+    @user = User.new(params[:user])
+    if @user.save
+      flash[:notice] = 'User was successfully created.'
+      redirect_to :action => 'list'
+    else
+      render :action => 'new'
+    end
+  end
+
+  def edit
+    @user = User.find(params[:id])
+  end
+
+  def update
+    @user = User.find(params[:id])
+    if @user.update_attributes(params[:user])
+      flash[:notice] = 'User was successfully updated.'
+      redirect_to :action => 'show', :id => @user
+    else
+      render :action => 'edit'
+    end
+  end
+
+  def destroy
+    User.find(params[:id]).destroy
+    redirect_to :action => 'list'
+  end
+end
diff --git a/app/helpers/users_helper.rb b/app/helpers/users_helper.rb
new file mode 100644
index 0000000..2310a24
--- /dev/null
+++ b/app/helpers/users_helper.rb
@@ -0,0 +1,2 @@
+module UsersHelper
+end
diff --git a/app/models/user.rb b/app/models/user.rb
new file mode 100644
index 0000000..4a57cf0
--- /dev/null
+++ b/app/models/user.rb
@@ -0,0 +1,2 @@
+class User < ActiveRecord::Base
+end
diff --git a/app/views/layouts/users.rhtml b/app/views/layouts/users.rhtml
new file mode 100644
index 0000000..30bf3ce
--- /dev/null
+++ b/app/views/layouts/users.rhtml
@@ -0,0 +1,13 @@
+<html>
+<head>
+  <title>Users: <%= controller.action_name %></title>
+  <%= stylesheet_link_tag 'scaffold' %>
+</head>
+<body>
+
+<p style="color: green"><%= flash[:notice] %></p>
+
+<%= @content_for_layout %>
+
+</body>
+</html>
diff --git a/app/views/users/_form.rhtml b/app/views/users/_form.rhtml
new file mode 100644
index 0000000..3e87c53
--- /dev/null
+++ b/app/views/users/_form.rhtml
@@ -0,0 +1,7 @@
+<%= error_messages_for 'user' %>
+
+<!--[form:user]-->
+<p><label for="user_username">Username</label><br/>
+<%= text_field 'user', 'username'  %></p>
+<!--[eoform:user]-->
+
diff --git a/app/views/users/edit.rhtml b/app/views/users/edit.rhtml
new file mode 100644
index 0000000..b02e6f2
--- /dev/null
+++ b/app/views/users/edit.rhtml
@@ -0,0 +1,9 @@
+<h1>Editing user</h1>
+
+<%= start_form_tag :action => 'update', :id => @user %>
+  <%= render :partial => 'form' %>
+  <%= submit_tag 'Edit' %>
+<%= end_form_tag %>
+
+<%= link_to 'Show', :action => 'show', :id => @user %> |
+<%= link_to 'Back', :action => 'list' %>
diff --git a/app/views/users/list.rhtml b/app/views/users/list.rhtml
new file mode 100644
index 0000000..be88e00
--- /dev/null
+++ b/app/views/users/list.rhtml
@@ -0,0 +1,27 @@
+<h1>Listing users</h1>
+
+<table>
+  <tr>
+  <% for column in User.content_columns %>
+    <th><%= column.human_name %></th>
+  <% end %>
+  </tr>
+  
+<% for user in @users %>
+  <tr>
+  <% for column in User.content_columns %>
+    <td><%=h user.send(column.name) %></td>
+  <% end %>
+    <td><%= link_to 'Show', :action => 'show', :id => user %></td>
+    <td><%= link_to 'Edit', :action => 'edit', :id => user %></td>
+    <td><%= link_to 'Destroy', { :action => 'destroy', :id => user }, :confirm => 'Are you sure?', :post => true %></td>
+  </tr>
+<% end %>
+</table>
+
+<%= link_to 'Previous page', { :page => @user_pages.current.previous } if @user_pages.current.previous %>
+<%= link_to 'Next page', { :page => @user_pages.current.next } if @user_pages.current.next %> 
+
+<br />
+
+<%= link_to 'New user', :action => 'new' %>
diff --git a/app/views/users/new.rhtml b/app/views/users/new.rhtml
new file mode 100644
index 0000000..3144e59
--- /dev/null
+++ b/app/views/users/new.rhtml
@@ -0,0 +1,8 @@
+<h1>New user</h1>
+
+<%= start_form_tag :action => 'create' %>
+  <%= render :partial => 'form' %>
+  <%= submit_tag "Create" %>
+<%= end_form_tag %>
+
+<%= link_to 'Back', :action => 'list' %>
diff --git a/app/views/users/show.rhtml b/app/views/users/show.rhtml
new file mode 100644
index 0000000..562ade0
--- /dev/null
+++ b/app/views/users/show.rhtml
@@ -0,0 +1,8 @@
+<% for column in User.content_columns %>
+<p>
+  <b><%= column.human_name %>:</b> <%=h @user.send(column.name) %>
+</p>
+<% end %>
+
+<%= link_to 'Edit', :action => 'edit', :id => @user %> |
+<%= link_to 'Back', :action => 'list' %>
diff --git a/public/stylesheets/scaffold.css b/public/stylesheets/scaffold.css
new file mode 100644
index 0000000..8f239a3
--- /dev/null
+++ b/public/stylesheets/scaffold.css
@@ -0,0 +1,74 @@
+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;
+}
+
+div.uploadStatus {
+  margin: 5px;
+}
+
+div.progressBar {
+  margin: 5px;
+}
+
+div.progressBar div.border {
+  background-color: #fff;
+  border: 1px solid grey;
+  width: 100%;
+}
+
+div.progressBar div.background {
+  background-color: #333;
+  height: 18px;
+  width: 0%;
+}
+
diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml
new file mode 100644
index 0000000..8794d28
--- /dev/null
+++ b/test/fixtures/users.yml
@@ -0,0 +1,5 @@
+# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
+first:
+  id: 1
+another:
+  id: 2
diff --git a/test/functional/users_controller_test.rb b/test/functional/users_controller_test.rb
new file mode 100644
index 0000000..f1e2281
--- /dev/null
+++ b/test/functional/users_controller_test.rb
@@ -0,0 +1,88 @@
+require File.dirname(__FILE__) + '/../test_helper'
+require 'users_controller'
+
+# Re-raise errors caught by the controller.
+class UsersController; def rescue_action(e) raise e end; end
+
+class UsersControllerTest < Test::Unit::TestCase
+  fixtures :users
+
+  def setup
+    @controller = UsersController.new
+    @request    = ActionController::TestRequest.new
+    @response   = ActionController::TestResponse.new
+  end
+
+  def test_index
+    get :index
+    assert_response :success
+    assert_template 'list'
+  end
+
+  def test_list
+    get :list
+
+    assert_response :success
+    assert_template 'list'
+
+    assert_not_nil assigns(:users)
+  end
+
+  def test_show
+    get :show, :id => 1
+
+    assert_response :success
+    assert_template 'show'
+
+    assert_not_nil assigns(:user)
+    assert assigns(:user).valid?
+  end
+
+  def test_new
+    get :new
+
+    assert_response :success
+    assert_template 'new'
+
+    assert_not_nil assigns(:user)
+  end
+
+  def test_create
+    num_users = User.count
+
+    post :create, :user => {}
+
+    assert_response :redirect
+    assert_redirected_to :action => 'list'
+
+    assert_equal num_users + 1, User.count
+  end
+
+  def test_edit
+    get :edit, :id => 1
+
+    assert_response :success
+    assert_template 'edit'
+
+    assert_not_nil assigns(:user)
+    assert assigns(:user).valid?
+  end
+
+  def test_update
+    post :update, :id => 1
+    assert_response :redirect
+    assert_redirected_to :action => 'show', :id => 1
+  end
+
+  def test_destroy
+    assert_not_nil User.find(1)
+
+    post :destroy, :id => 1
+    assert_response :redirect
+    assert_redirected_to :action => 'list'
+
+    assert_raise(ActiveRecord::RecordNotFound) {
+      User.find(1)
+    }
+  end
+end
diff --git a/test/unit/user_test.rb b/test/unit/user_test.rb
new file mode 100644
index 0000000..5468f7a
--- /dev/null
+++ b/test/unit/user_test.rb
@@ -0,0 +1,10 @@
+require File.dirname(__FILE__) + '/../test_helper'
+
+class UserTest < Test::Unit::TestCase
+  fixtures :users
+
+  # Replace this with your real tests.
+  def test_truth
+    assert true
+  end
+end
-- 
GitLab