diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb new file mode 100644 index 0000000000000000000000000000000000000000..916d8bc99b0eade2f968bc3d5cb51c9786187d9e --- /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 0000000000000000000000000000000000000000..2310a240d78bd6361668625f782ada9d71cdd6c5 --- /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 0000000000000000000000000000000000000000..4a57cf079beb30714a0a77c1fc7e9cab8e144483 --- /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 0000000000000000000000000000000000000000..30bf3ceaba2a8ebfcd579295ddaabb6a1c7e8cd7 --- /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 0000000000000000000000000000000000000000..3e87c53ddf4aca50e799fdfcb84a09fa441ad266 --- /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 0000000000000000000000000000000000000000..b02e6f27674a47d2039b808329725b3edd80e252 --- /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 0000000000000000000000000000000000000000..be88e00809a21615899e2a16b840ca2c592dc47a --- /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 0000000000000000000000000000000000000000..3144e59ace5372d9304ba702f0ef2abee9ef2dbb --- /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 0000000000000000000000000000000000000000..562ade0a1892c51dca3efab50ae24dbdf81f2eca --- /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 0000000000000000000000000000000000000000..8f239a3597cef31a95c9636e7f60440e92f6fd3a --- /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 0000000000000000000000000000000000000000..8794d28ae419929e241d892341c7429b84e5c74e --- /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 0000000000000000000000000000000000000000..f1e22817b259c7a1d00d5d00e64bc147757308e6 --- /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 0000000000000000000000000000000000000000..5468f7a2d90fc88f295f8beb1cfc595699bfce10 --- /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