From 3ea3a07c442fcef8c37d113cd03e642b02ab8587 Mon Sep 17 00:00:00 2001 From: jimi Date: Fri, 6 Mar 2009 09:19:26 -0600 Subject: [PATCH] Generate person scaffolding, store tasks directory --- app/controllers/people_controller.rb | 44 ++++++++++++++++++ app/helpers/people_helper.rb | 2 + app/models/person.rb | 2 + app/views/people/_form.html.erb | 12 +++++ app/views/people/edit.html.erb | 8 ++++ app/views/people/index.html.erb | 19 ++++++++ app/views/people/new.html.erb | 5 ++ app/views/people/show.html.erb | 16 +++++++ config/routes.rb | 2 + db/migrate/20090306144503_create_people.rb | 13 ++++++ db/schema.rb | 21 +++++++++ lib/tasks/.gitignore | 0 test/fixtures/people.yml | 7 +++ test/functional/people_controller_test.rb | 54 ++++++++++++++++++++++ test/unit/person_test.rb | 7 +++ 15 files changed, 212 insertions(+) create mode 100644 app/controllers/people_controller.rb create mode 100644 app/helpers/people_helper.rb create mode 100644 app/models/person.rb create mode 100644 app/views/people/_form.html.erb create mode 100644 app/views/people/edit.html.erb create mode 100644 app/views/people/index.html.erb create mode 100644 app/views/people/new.html.erb create mode 100644 app/views/people/show.html.erb create mode 100644 db/migrate/20090306144503_create_people.rb create mode 100644 db/schema.rb create mode 100644 lib/tasks/.gitignore create mode 100644 test/fixtures/people.yml create mode 100644 test/functional/people_controller_test.rb create mode 100644 test/unit/person_test.rb diff --git a/app/controllers/people_controller.rb b/app/controllers/people_controller.rb new file mode 100644 index 0000000..0df8471 --- /dev/null +++ b/app/controllers/people_controller.rb @@ -0,0 +1,44 @@ +class PeopleController < ApplicationController + def index + @people = Person.all + end + + def show + @person = Person.find(params[:id]) + end + + def new + @person = Person.new + end + + def create + @person = Person.new(params[:person]) + if @person.save + flash[:notice] = "Successfully created person." + redirect_to @person + else + render :action => 'new' + end + end + + def edit + @person = Person.find(params[:id]) + end + + def update + @person = Person.find(params[:id]) + if @person.update_attributes(params[:person]) + flash[:notice] = "Successfully updated person." + redirect_to @person + else + render :action => 'edit' + end + end + + def destroy + @person = Person.find(params[:id]) + @person.destroy + flash[:notice] = "Successfully destroyed person." + redirect_to people_url + end +end diff --git a/app/helpers/people_helper.rb b/app/helpers/people_helper.rb new file mode 100644 index 0000000..b682fbf --- /dev/null +++ b/app/helpers/people_helper.rb @@ -0,0 +1,2 @@ +module PeopleHelper +end diff --git a/app/models/person.rb b/app/models/person.rb new file mode 100644 index 0000000..2f2e286 --- /dev/null +++ b/app/models/person.rb @@ -0,0 +1,2 @@ +class Person < ActiveRecord::Base +end diff --git a/app/views/people/_form.html.erb b/app/views/people/_form.html.erb new file mode 100644 index 0000000..247a043 --- /dev/null +++ b/app/views/people/_form.html.erb @@ -0,0 +1,12 @@ +<% form_for @person do |f| %> + <%= f.error_messages %> +

+ <%= f.label :firstname %>
+ <%= f.text_field :firstname %> +

+

+ <%= f.label :lastname %>
+ <%= f.text_field :lastname %> +

+

<%= f.submit "Submit" %>

+<% end %> diff --git a/app/views/people/edit.html.erb b/app/views/people/edit.html.erb new file mode 100644 index 0000000..17820a1 --- /dev/null +++ b/app/views/people/edit.html.erb @@ -0,0 +1,8 @@ +<% title "Edit Person" %> + +<%= render :partial => 'form' %> + +

+ <%= link_to "Show", @person %> | + <%= link_to "View All", people_path %> +

diff --git a/app/views/people/index.html.erb b/app/views/people/index.html.erb new file mode 100644 index 0000000..1a8218c --- /dev/null +++ b/app/views/people/index.html.erb @@ -0,0 +1,19 @@ +<% title "People" %> + + + + + + + <% for person in @people %> + + + + + + + + <% end %> +
FirstnameLastname
<%=h person.firstname %><%=h person.lastname %><%= link_to "Show", person %><%= link_to "Edit", edit_person_path(person) %><%= link_to "Destroy", person, :confirm => 'Are you sure?', :method => :delete %>
+ +

<%= link_to "New Person", new_person_path %>

diff --git a/app/views/people/new.html.erb b/app/views/people/new.html.erb new file mode 100644 index 0000000..7f118fc --- /dev/null +++ b/app/views/people/new.html.erb @@ -0,0 +1,5 @@ +<% title "New Person" %> + +<%= render :partial => 'form' %> + +

<%= link_to "Back to List", people_path %>

diff --git a/app/views/people/show.html.erb b/app/views/people/show.html.erb new file mode 100644 index 0000000..91316a7 --- /dev/null +++ b/app/views/people/show.html.erb @@ -0,0 +1,16 @@ +<% title "Person" %> + +

+ Firstname: + <%=h @person.firstname %> +

+

+ Lastname: + <%=h @person.lastname %> +

+ +

+ <%= link_to "Edit", edit_person_path(@person) %> | + <%= link_to "Destroy", @person, :confirm => 'Are you sure?', :method => :delete %> | + <%= link_to "View All", people_path %> +

diff --git a/config/routes.rb b/config/routes.rb index 4f3d9d2..3f5d6fa 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,6 @@ ActionController::Routing::Routes.draw do |map| + map.resources :people + # The priority is based upon order of creation: first created -> highest priority. # Sample of regular route: diff --git a/db/migrate/20090306144503_create_people.rb b/db/migrate/20090306144503_create_people.rb new file mode 100644 index 0000000..de50474 --- /dev/null +++ b/db/migrate/20090306144503_create_people.rb @@ -0,0 +1,13 @@ +class CreatePeople < ActiveRecord::Migration + def self.up + create_table :people do |t| + t.string :firstname + t.string :lastname + t.timestamps + end + end + + def self.down + drop_table :people + end +end diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 0000000..060eae1 --- /dev/null +++ b/db/schema.rb @@ -0,0 +1,21 @@ +# This file is auto-generated from the current state of the database. Instead of editing this file, +# please use the migrations feature of Active Record to incrementally modify your database, and +# then regenerate this schema definition. +# +# Note that this schema.rb definition is the authoritative source for your database schema. If you need +# to create the application database on another system, you should be using db:schema:load, not running +# all the migrations from scratch. The latter is a flawed and unsustainable approach (the more migrations +# you'll amass, the slower it'll run and the greater likelihood for issues). +# +# It's strongly recommended to check this file into your version control system. + +ActiveRecord::Schema.define(:version => 20090306144503) do + + create_table "people", :force => true do |t| + t.string "firstname" + t.string "lastname" + t.datetime "created_at" + t.datetime "updated_at" + end + +end diff --git a/lib/tasks/.gitignore b/lib/tasks/.gitignore new file mode 100644 index 0000000..e69de29 diff --git a/test/fixtures/people.yml b/test/fixtures/people.yml new file mode 100644 index 0000000..951b956 --- /dev/null +++ b/test/fixtures/people.yml @@ -0,0 +1,7 @@ +one: + firstname: MyString + lastname: MyString + +two: + firstname: MyString + lastname: MyString diff --git a/test/functional/people_controller_test.rb b/test/functional/people_controller_test.rb new file mode 100644 index 0000000..b11ad39 --- /dev/null +++ b/test/functional/people_controller_test.rb @@ -0,0 +1,54 @@ +require 'test_helper' + +class PeopleControllerTest < ActionController::TestCase + def test_index + get :index + assert_template 'index' + end + + def test_show + get :show, :id => Person.first + assert_template 'show' + end + + def test_new + get :new + assert_template 'new' + end + + def test_create_invalid + Person.any_instance.stubs(:valid?).returns(false) + post :create + assert_template 'new' + end + + def test_create_valid + Person.any_instance.stubs(:valid?).returns(true) + post :create + assert_redirected_to person_url(assigns(:person)) + end + + def test_edit + get :edit, :id => Person.first + assert_template 'edit' + end + + def test_update_invalid + Person.any_instance.stubs(:valid?).returns(false) + put :update, :id => Person.first + assert_template 'edit' + end + + def test_update_valid + Person.any_instance.stubs(:valid?).returns(true) + put :update, :id => Person.first + assert_redirected_to person_url(assigns(:person)) + end + + def test_destroy + person = Person.first + delete :destroy, :id => person + assert_redirected_to people_url + assert !Person.exists?(person.id) + end +end diff --git a/test/unit/person_test.rb b/test/unit/person_test.rb new file mode 100644 index 0000000..9acc619 --- /dev/null +++ b/test/unit/person_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class PersonTest < ActiveSupport::TestCase + def test_should_be_valid + assert Person.new.valid? + end +end