diff --git a/app/controllers/accounts_controller.rb b/app/controllers/accounts_controller.rb new file mode 100644 index 0000000000000000000000000000000000000000..9524f7ea7703f4b3862e88ffe1e6cddaf23debce --- /dev/null +++ b/app/controllers/accounts_controller.rb @@ -0,0 +1,51 @@ +class AccountsController < 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 + @account_pages, @accounts = paginate :accounts, :per_page => 10 + end + + def show + @account = Account.find(params[:id]) + end + + def new + @account = Account.new + end + + def create + @account = Account.new(params[:account]) + if @account.save + flash[:notice] = 'Account was successfully created.' + redirect_to :action => 'list' + else + render :action => 'new' + end + end + + def edit + @account = Account.find(params[:id]) + end + + def update + @account = Account.find(params[:id]) + if @account.update_attributes(params[:account]) + flash[:notice] = 'Account was successfully updated.' + redirect_to :action => 'show', :id => @account + else + render :action => 'edit' + end + end + + def destroy + Account.find(params[:id]).destroy + redirect_to :action => 'list' + end +end diff --git a/app/helpers/accounts_helper.rb b/app/helpers/accounts_helper.rb new file mode 100644 index 0000000000000000000000000000000000000000..17850b4b7f556b88179bb51df4cac7bf4f57acb1 --- /dev/null +++ b/app/helpers/accounts_helper.rb @@ -0,0 +1,2 @@ +module AccountsHelper +end diff --git a/app/views/accounts/_form.rhtml b/app/views/accounts/_form.rhtml new file mode 100644 index 0000000000000000000000000000000000000000..584922de6a001c207c40133b9e400bcbb3abe14b --- /dev/null +++ b/app/views/accounts/_form.rhtml @@ -0,0 +1,7 @@ +<%= error_messages_for 'account' %> + +<!--[form:account]--> +<p><label for="account_name">Name</label><br/> +<%= text_field 'account', 'name' %></p> +<!--[eoform:account]--> + diff --git a/app/views/accounts/edit.rhtml b/app/views/accounts/edit.rhtml new file mode 100644 index 0000000000000000000000000000000000000000..1596c4d1abe8a030b5b42f9f9e23c5605378f29f --- /dev/null +++ b/app/views/accounts/edit.rhtml @@ -0,0 +1,9 @@ +<h1>Editing account</h1> + +<%= start_form_tag :action => 'update', :id => @account %> + <%= render :partial => 'form' %> + <%= submit_tag 'Edit' %> +<%= end_form_tag %> + +<%= link_to 'Show', :action => 'show', :id => @account %> | +<%= link_to 'Back', :action => 'list' %> diff --git a/app/views/accounts/list.rhtml b/app/views/accounts/list.rhtml new file mode 100644 index 0000000000000000000000000000000000000000..0e08b7959bed091ff922fbf0950f69d8aae6f5ac --- /dev/null +++ b/app/views/accounts/list.rhtml @@ -0,0 +1,27 @@ +<h1>Listing accounts</h1> + +<table> + <tr> + <% for column in Account.content_columns %> + <th><%= column.human_name %></th> + <% end %> + </tr> + +<% for account in @accounts %> + <tr> + <% for column in Account.content_columns %> + <td><%=h account.send(column.name) %></td> + <% end %> + <td><%= link_to 'Show', :action => 'show', :id => account %></td> + <td><%= link_to 'Edit', :action => 'edit', :id => account %></td> + <td><%= link_to 'Destroy', { :action => 'destroy', :id => account }, :confirm => 'Are you sure?', :post => true %></td> + </tr> +<% end %> +</table> + +<%= link_to 'Previous page', { :page => @account_pages.current.previous } if @account_pages.current.previous %> +<%= link_to 'Next page', { :page => @account_pages.current.next } if @account_pages.current.next %> + +<br /> + +<%= link_to 'New account', :action => 'new' %> diff --git a/app/views/accounts/new.rhtml b/app/views/accounts/new.rhtml new file mode 100644 index 0000000000000000000000000000000000000000..8a7471ae7888e234866411592543a120e34f1c83 --- /dev/null +++ b/app/views/accounts/new.rhtml @@ -0,0 +1,8 @@ +<h1>New account</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/accounts/show.rhtml b/app/views/accounts/show.rhtml new file mode 100644 index 0000000000000000000000000000000000000000..2e571d22db7a0f1d9712575e7cb9b5bc53ea2a3e --- /dev/null +++ b/app/views/accounts/show.rhtml @@ -0,0 +1,8 @@ +<% for column in Account.content_columns %> +<p> + <b><%= column.human_name %>:</b> <%=h @account.send(column.name) %> +</p> +<% end %> + +<%= link_to 'Edit', :action => 'edit', :id => @account %> | +<%= link_to 'Back', :action => 'list' %> diff --git a/app/views/layouts/accounts.rhtml b/app/views/layouts/accounts.rhtml new file mode 100644 index 0000000000000000000000000000000000000000..ddd9db21eb468b6254a104c3802bd30bc2074017 --- /dev/null +++ b/app/views/layouts/accounts.rhtml @@ -0,0 +1,13 @@ +<html> +<head> + <title>Accounts: <%= 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/test/functional/accounts_controller_test.rb b/test/functional/accounts_controller_test.rb new file mode 100644 index 0000000000000000000000000000000000000000..096322abb94ca0a19c4321074dddf4de068cc65a --- /dev/null +++ b/test/functional/accounts_controller_test.rb @@ -0,0 +1,88 @@ +require File.dirname(__FILE__) + '/../test_helper' +require 'accounts_controller' + +# Re-raise errors caught by the controller. +class AccountsController; def rescue_action(e) raise e end; end + +class AccountsControllerTest < Test::Unit::TestCase + fixtures :accounts + + def setup + @controller = AccountsController.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(:accounts) + end + + def test_show + get :show, :id => 1 + + assert_response :success + assert_template 'show' + + assert_not_nil assigns(:account) + assert assigns(:account).valid? + end + + def test_new + get :new + + assert_response :success + assert_template 'new' + + assert_not_nil assigns(:account) + end + + def test_create + num_accounts = Account.count + + post :create, :account => {} + + assert_response :redirect + assert_redirected_to :action => 'list' + + assert_equal num_accounts + 1, Account.count + end + + def test_edit + get :edit, :id => 1 + + assert_response :success + assert_template 'edit' + + assert_not_nil assigns(:account) + assert assigns(:account).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 Account.find(1) + + post :destroy, :id => 1 + assert_response :redirect + assert_redirected_to :action => 'list' + + assert_raise(ActiveRecord::RecordNotFound) { + Account.find(1) + } + end +end