From 21ce651984ee622244ebc411c1a4a38e199be650 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Mon, 14 Nov 2011 07:42:13 -0500 Subject: [PATCH] Adding scaffolding for "Person" --- .../app/assets/javascripts/people.js.coffee | 3 + .../app/assets/stylesheets/people.css.scss | 3 + .../app/assets/stylesheets/scaffolds.css.scss | 56 +++++++ .../app/controllers/people_controller.rb | 83 +++++++++ rails/gerbil/app/helpers/people_helper.rb | 2 + rails/gerbil/app/models/person.rb | 2 + rails/gerbil/app/views/people/_form.html.erb | 29 ++++ rails/gerbil/app/views/people/edit.html.erb | 6 + rails/gerbil/app/views/people/index.html.erb | 27 +++ rails/gerbil/app/views/people/new.html.erb | 5 + rails/gerbil/app/views/people/show.html.erb | 20 +++ rails/gerbil/config/routes.rb | 2 + .../migrate/20111114120238_create_people.rb | 11 ++ rails/gerbil/db/schema.rb | 23 +++ .../controllers/people_controller_spec.rb | 157 ++++++++++++++++++ .../gerbil/spec/helpers/people_helper_spec.rb | 15 ++ rails/gerbil/spec/models/person_spec.rb | 5 + rails/gerbil/spec/requests/people_spec.rb | 11 ++ .../spec/routing/people_routing_spec.rb | 35 ++++ .../spec/views/people/edit.html.erb_spec.rb | 22 +++ .../spec/views/people/index.html.erb_spec.rb | 28 ++++ .../spec/views/people/new.html.erb_spec.rb | 22 +++ .../spec/views/people/show.html.erb_spec.rb | 21 +++ 23 files changed, 588 insertions(+) create mode 100644 rails/gerbil/app/assets/javascripts/people.js.coffee create mode 100644 rails/gerbil/app/assets/stylesheets/people.css.scss create mode 100644 rails/gerbil/app/assets/stylesheets/scaffolds.css.scss create mode 100644 rails/gerbil/app/controllers/people_controller.rb create mode 100644 rails/gerbil/app/helpers/people_helper.rb create mode 100644 rails/gerbil/app/models/person.rb create mode 100644 rails/gerbil/app/views/people/_form.html.erb create mode 100644 rails/gerbil/app/views/people/edit.html.erb create mode 100644 rails/gerbil/app/views/people/index.html.erb create mode 100644 rails/gerbil/app/views/people/new.html.erb create mode 100644 rails/gerbil/app/views/people/show.html.erb create mode 100644 rails/gerbil/db/migrate/20111114120238_create_people.rb create mode 100644 rails/gerbil/db/schema.rb create mode 100644 rails/gerbil/spec/controllers/people_controller_spec.rb create mode 100644 rails/gerbil/spec/helpers/people_helper_spec.rb create mode 100644 rails/gerbil/spec/models/person_spec.rb create mode 100644 rails/gerbil/spec/requests/people_spec.rb create mode 100644 rails/gerbil/spec/routing/people_routing_spec.rb create mode 100644 rails/gerbil/spec/views/people/edit.html.erb_spec.rb create mode 100644 rails/gerbil/spec/views/people/index.html.erb_spec.rb create mode 100644 rails/gerbil/spec/views/people/new.html.erb_spec.rb create mode 100644 rails/gerbil/spec/views/people/show.html.erb_spec.rb diff --git a/rails/gerbil/app/assets/javascripts/people.js.coffee b/rails/gerbil/app/assets/javascripts/people.js.coffee new file mode 100644 index 0000000..7615679 --- /dev/null +++ b/rails/gerbil/app/assets/javascripts/people.js.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/ diff --git a/rails/gerbil/app/assets/stylesheets/people.css.scss b/rails/gerbil/app/assets/stylesheets/people.css.scss new file mode 100644 index 0000000..5217462 --- /dev/null +++ b/rails/gerbil/app/assets/stylesheets/people.css.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the People controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/rails/gerbil/app/assets/stylesheets/scaffolds.css.scss b/rails/gerbil/app/assets/stylesheets/scaffolds.css.scss new file mode 100644 index 0000000..05188f0 --- /dev/null +++ b/rails/gerbil/app/assets/stylesheets/scaffolds.css.scss @@ -0,0 +1,56 @@ +body { + background-color: #fff; + color: #333; + font-family: verdana, arial, helvetica, sans-serif; + font-size: 13px; + line-height: 18px; } + +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; + &:visited { + color: #666; } + &:hover { + color: #fff; + background-color: #000; } } + +div { + &.field, &.actions { + margin-bottom: 10px; } } + +#notice { + color: green; } + +.field_with_errors { + padding: 2px; + background-color: red; + display: table; } + +#error_explanation { + width: 450px; + border: 2px solid red; + padding: 7px; + padding-bottom: 0; + margin-bottom: 20px; + background-color: #f0f0f0; + h2 { + text-align: left; + font-weight: bold; + padding: 5px 5px 5px 15px; + font-size: 12px; + margin: -7px; + margin-bottom: 0px; + background-color: #c00; + color: #fff; } + ul li { + font-size: 12px; + list-style: square; } } diff --git a/rails/gerbil/app/controllers/people_controller.rb b/rails/gerbil/app/controllers/people_controller.rb new file mode 100644 index 0000000..cf4b1b5 --- /dev/null +++ b/rails/gerbil/app/controllers/people_controller.rb @@ -0,0 +1,83 @@ +class PeopleController < ApplicationController + # GET /people + # GET /people.json + def index + @people = Person.all + + respond_to do |format| + format.html # index.html.erb + format.json { render :json => @people } + end + end + + # GET /people/1 + # GET /people/1.json + def show + @person = Person.find(params[:id]) + + respond_to do |format| + format.html # show.html.erb + format.json { render :json => @person } + end + end + + # GET /people/new + # GET /people/new.json + def new + @person = Person.new + + respond_to do |format| + format.html # new.html.erb + format.json { render :json => @person } + end + end + + # GET /people/1/edit + def edit + @person = Person.find(params[:id]) + end + + # POST /people + # POST /people.json + def create + @person = Person.new(params[:person]) + + respond_to do |format| + if @person.save + format.html { redirect_to @person, :notice => 'Person was successfully created.' } + format.json { render :json => @person, :status => :created, :location => @person } + else + format.html { render :action => "new" } + format.json { render :json => @person.errors, :status => :unprocessable_entity } + end + end + end + + # PUT /people/1 + # PUT /people/1.json + def update + @person = Person.find(params[:id]) + + respond_to do |format| + if @person.update_attributes(params[:person]) + format.html { redirect_to @person, :notice => 'Person was successfully updated.' } + format.json { head :ok } + else + format.html { render :action => "edit" } + format.json { render :json => @person.errors, :status => :unprocessable_entity } + end + end + end + + # DELETE /people/1 + # DELETE /people/1.json + def destroy + @person = Person.find(params[:id]) + @person.destroy + + respond_to do |format| + format.html { redirect_to people_url } + format.json { head :ok } + end + end +end diff --git a/rails/gerbil/app/helpers/people_helper.rb b/rails/gerbil/app/helpers/people_helper.rb new file mode 100644 index 0000000..b682fbf --- /dev/null +++ b/rails/gerbil/app/helpers/people_helper.rb @@ -0,0 +1,2 @@ +module PeopleHelper +end diff --git a/rails/gerbil/app/models/person.rb b/rails/gerbil/app/models/person.rb new file mode 100644 index 0000000..2f2e286 --- /dev/null +++ b/rails/gerbil/app/models/person.rb @@ -0,0 +1,2 @@ +class Person < ActiveRecord::Base +end diff --git a/rails/gerbil/app/views/people/_form.html.erb b/rails/gerbil/app/views/people/_form.html.erb new file mode 100644 index 0000000..7f287c9 --- /dev/null +++ b/rails/gerbil/app/views/people/_form.html.erb @@ -0,0 +1,29 @@ +<%= form_for(@person) do |f| %> + <% if @person.errors.any? %> +
+

<%= pluralize(@person.errors.count, "error") %> prohibited this person from being saved:

+ + +
+ <% end %> + +
+ <%= f.label :name %>
+ <%= f.text_field :name %> +
+
+ <%= f.label :age %>
+ <%= f.number_field :age %> +
+
+ <%= f.label :zipcode %>
+ <%= f.text_field :zipcode %> +
+
+ <%= f.submit %> +
+<% end %> diff --git a/rails/gerbil/app/views/people/edit.html.erb b/rails/gerbil/app/views/people/edit.html.erb new file mode 100644 index 0000000..4d91b25 --- /dev/null +++ b/rails/gerbil/app/views/people/edit.html.erb @@ -0,0 +1,6 @@ +

Editing person

+ +<%= render 'form' %> + +<%= link_to 'Show', @person %> | +<%= link_to 'Back', people_path %> diff --git a/rails/gerbil/app/views/people/index.html.erb b/rails/gerbil/app/views/people/index.html.erb new file mode 100644 index 0000000..981ce38 --- /dev/null +++ b/rails/gerbil/app/views/people/index.html.erb @@ -0,0 +1,27 @@ +

Listing people

+ + + + + + + + + + + +<% @people.each do |person| %> + + + + + + + + +<% end %> +
NameAgeZipcode
<%= person.name %><%= person.age %><%= person.zipcode %><%= 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/rails/gerbil/app/views/people/new.html.erb b/rails/gerbil/app/views/people/new.html.erb new file mode 100644 index 0000000..3618249 --- /dev/null +++ b/rails/gerbil/app/views/people/new.html.erb @@ -0,0 +1,5 @@ +

New person

+ +<%= render 'form' %> + +<%= link_to 'Back', people_path %> diff --git a/rails/gerbil/app/views/people/show.html.erb b/rails/gerbil/app/views/people/show.html.erb new file mode 100644 index 0000000..31d0f6e --- /dev/null +++ b/rails/gerbil/app/views/people/show.html.erb @@ -0,0 +1,20 @@ +

<%= notice %>

+ +

+ Name: + <%= @person.name %> +

+ +

+ Age: + <%= @person.age %> +

+ +

+ Zipcode: + <%= @person.zipcode %> +

+ + +<%= link_to 'Edit', edit_person_path(@person) %> | +<%= link_to 'Back', people_path %> diff --git a/rails/gerbil/config/routes.rb b/rails/gerbil/config/routes.rb index e064232..a5d455b 100644 --- a/rails/gerbil/config/routes.rb +++ b/rails/gerbil/config/routes.rb @@ -1,4 +1,6 @@ Gerbil::Application.routes.draw do + resources :people + # The priority is based upon order of creation: # first created -> highest priority. diff --git a/rails/gerbil/db/migrate/20111114120238_create_people.rb b/rails/gerbil/db/migrate/20111114120238_create_people.rb new file mode 100644 index 0000000..29f934b --- /dev/null +++ b/rails/gerbil/db/migrate/20111114120238_create_people.rb @@ -0,0 +1,11 @@ +class CreatePeople < ActiveRecord::Migration + def change + create_table :people do |t| + t.string :name + t.integer :age + t.string :zipcode + + t.timestamps + end + end +end diff --git a/rails/gerbil/db/schema.rb b/rails/gerbil/db/schema.rb new file mode 100644 index 0000000..43de42d --- /dev/null +++ b/rails/gerbil/db/schema.rb @@ -0,0 +1,23 @@ +# 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 => 20111114120238) do + + create_table "people", :force => true do |t| + t.string "name" + t.integer "age" + t.string "zipcode" + t.datetime "created_at" + t.datetime "updated_at" + end + +end diff --git a/rails/gerbil/spec/controllers/people_controller_spec.rb b/rails/gerbil/spec/controllers/people_controller_spec.rb new file mode 100644 index 0000000..af6f11d --- /dev/null +++ b/rails/gerbil/spec/controllers/people_controller_spec.rb @@ -0,0 +1,157 @@ +require 'spec_helper' + +# This spec was generated by rspec-rails when you ran the scaffold generator. +# It demonstrates how one might use RSpec to specify the controller code that +# was generated by Rails when you ran the scaffold generator. +# +# It assumes that the implementation code is generated by the rails scaffold +# generator. If you are using any extension libraries to generate different +# controller code, this generated spec may or may not pass. +# +# It only uses APIs available in rails and/or rspec-rails. There are a number +# of tools you can use to make these specs even more expressive, but we're +# sticking to rails and rspec-rails APIs to keep things simple and stable. +# +# Compared to earlier versions of this generator, there is very limited use of +# stubs and message expectations in this spec. Stubs are only used when there +# is no simpler way to get a handle on the object needed for the example. +# Message expectations are only used when there is no simpler way to specify +# that an instance is receiving a specific message. + +describe PeopleController do + + # This should return the minimal set of attributes required to create a valid + # Person. As you add validations to Person, be sure to + # update the return value of this method accordingly. + def valid_attributes + {} + end + + describe "GET index" do + it "assigns all people as @people" do + person = Person.create! valid_attributes + get :index + assigns(:people).should eq([person]) + end + end + + describe "GET show" do + it "assigns the requested person as @person" do + person = Person.create! valid_attributes + get :show, :id => person.id + assigns(:person).should eq(person) + end + end + + describe "GET new" do + it "assigns a new person as @person" do + get :new + assigns(:person).should be_a_new(Person) + end + end + + describe "GET edit" do + it "assigns the requested person as @person" do + person = Person.create! valid_attributes + get :edit, :id => person.id + assigns(:person).should eq(person) + end + end + + describe "POST create" do + describe "with valid params" do + it "creates a new Person" do + expect { + post :create, :person => valid_attributes + }.to change(Person, :count).by(1) + end + + it "assigns a newly created person as @person" do + post :create, :person => valid_attributes + assigns(:person).should be_a(Person) + assigns(:person).should be_persisted + end + + it "redirects to the created person" do + post :create, :person => valid_attributes + response.should redirect_to(Person.last) + end + end + + describe "with invalid params" do + it "assigns a newly created but unsaved person as @person" do + # Trigger the behavior that occurs when invalid params are submitted + Person.any_instance.stub(:save).and_return(false) + post :create, :person => {} + assigns(:person).should be_a_new(Person) + end + + it "re-renders the 'new' template" do + # Trigger the behavior that occurs when invalid params are submitted + Person.any_instance.stub(:save).and_return(false) + post :create, :person => {} + response.should render_template("new") + end + end + end + + describe "PUT update" do + describe "with valid params" do + it "updates the requested person" do + person = Person.create! valid_attributes + # Assuming there are no other people in the database, this + # specifies that the Person created on the previous line + # receives the :update_attributes message with whatever params are + # submitted in the request. + Person.any_instance.should_receive(:update_attributes).with({'these' => 'params'}) + put :update, :id => person.id, :person => {'these' => 'params'} + end + + it "assigns the requested person as @person" do + person = Person.create! valid_attributes + put :update, :id => person.id, :person => valid_attributes + assigns(:person).should eq(person) + end + + it "redirects to the person" do + person = Person.create! valid_attributes + put :update, :id => person.id, :person => valid_attributes + response.should redirect_to(person) + end + end + + describe "with invalid params" do + it "assigns the person as @person" do + person = Person.create! valid_attributes + # Trigger the behavior that occurs when invalid params are submitted + Person.any_instance.stub(:save).and_return(false) + put :update, :id => person.id, :person => {} + assigns(:person).should eq(person) + end + + it "re-renders the 'edit' template" do + person = Person.create! valid_attributes + # Trigger the behavior that occurs when invalid params are submitted + Person.any_instance.stub(:save).and_return(false) + put :update, :id => person.id, :person => {} + response.should render_template("edit") + end + end + end + + describe "DELETE destroy" do + it "destroys the requested person" do + person = Person.create! valid_attributes + expect { + delete :destroy, :id => person.id + }.to change(Person, :count).by(-1) + end + + it "redirects to the people list" do + person = Person.create! valid_attributes + delete :destroy, :id => person.id + response.should redirect_to(people_url) + end + end + +end diff --git a/rails/gerbil/spec/helpers/people_helper_spec.rb b/rails/gerbil/spec/helpers/people_helper_spec.rb new file mode 100644 index 0000000..9d4bf57 --- /dev/null +++ b/rails/gerbil/spec/helpers/people_helper_spec.rb @@ -0,0 +1,15 @@ +require 'spec_helper' + +# Specs in this file have access to a helper object that includes +# the PeopleHelper. For example: +# +# describe PeopleHelper do +# describe "string concat" do +# it "concats two strings with spaces" do +# helper.concat_strings("this","that").should == "this that" +# end +# end +# end +describe PeopleHelper do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/rails/gerbil/spec/models/person_spec.rb b/rails/gerbil/spec/models/person_spec.rb new file mode 100644 index 0000000..6f582eb --- /dev/null +++ b/rails/gerbil/spec/models/person_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe Person do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/rails/gerbil/spec/requests/people_spec.rb b/rails/gerbil/spec/requests/people_spec.rb new file mode 100644 index 0000000..ba7a96c --- /dev/null +++ b/rails/gerbil/spec/requests/people_spec.rb @@ -0,0 +1,11 @@ +require 'spec_helper' + +describe "People" do + describe "GET /people" do + it "works! (now write some real specs)" do + # Run the generator again with the --webrat flag if you want to use webrat methods/matchers + get people_path + response.status.should be(200) + end + end +end diff --git a/rails/gerbil/spec/routing/people_routing_spec.rb b/rails/gerbil/spec/routing/people_routing_spec.rb new file mode 100644 index 0000000..70db5e0 --- /dev/null +++ b/rails/gerbil/spec/routing/people_routing_spec.rb @@ -0,0 +1,35 @@ +require "spec_helper" + +describe PeopleController do + describe "routing" do + + it "routes to #index" do + get("/people").should route_to("people#index") + end + + it "routes to #new" do + get("/people/new").should route_to("people#new") + end + + it "routes to #show" do + get("/people/1").should route_to("people#show", :id => "1") + end + + it "routes to #edit" do + get("/people/1/edit").should route_to("people#edit", :id => "1") + end + + it "routes to #create" do + post("/people").should route_to("people#create") + end + + it "routes to #update" do + put("/people/1").should route_to("people#update", :id => "1") + end + + it "routes to #destroy" do + delete("/people/1").should route_to("people#destroy", :id => "1") + end + + end +end diff --git a/rails/gerbil/spec/views/people/edit.html.erb_spec.rb b/rails/gerbil/spec/views/people/edit.html.erb_spec.rb new file mode 100644 index 0000000..fc7c2d1 --- /dev/null +++ b/rails/gerbil/spec/views/people/edit.html.erb_spec.rb @@ -0,0 +1,22 @@ +require 'spec_helper' + +describe "people/edit.html.erb" do + before(:each) do + @person = assign(:person, stub_model(Person, + :name => "MyString", + :age => 1, + :zipcode => "MyString" + )) + end + + it "renders the edit person form" do + render + + # Run the generator again with the --webrat flag if you want to use webrat matchers + assert_select "form", :action => people_path(@person), :method => "post" do + assert_select "input#person_name", :name => "person[name]" + assert_select "input#person_age", :name => "person[age]" + assert_select "input#person_zipcode", :name => "person[zipcode]" + end + end +end diff --git a/rails/gerbil/spec/views/people/index.html.erb_spec.rb b/rails/gerbil/spec/views/people/index.html.erb_spec.rb new file mode 100644 index 0000000..48064ea --- /dev/null +++ b/rails/gerbil/spec/views/people/index.html.erb_spec.rb @@ -0,0 +1,28 @@ +require 'spec_helper' + +describe "people/index.html.erb" do + before(:each) do + assign(:people, [ + stub_model(Person, + :name => "Name", + :age => 1, + :zipcode => "Zipcode" + ), + stub_model(Person, + :name => "Name", + :age => 1, + :zipcode => "Zipcode" + ) + ]) + end + + it "renders a list of people" do + render + # Run the generator again with the --webrat flag if you want to use webrat matchers + assert_select "tr>td", :text => "Name".to_s, :count => 2 + # Run the generator again with the --webrat flag if you want to use webrat matchers + assert_select "tr>td", :text => 1.to_s, :count => 2 + # Run the generator again with the --webrat flag if you want to use webrat matchers + assert_select "tr>td", :text => "Zipcode".to_s, :count => 2 + end +end diff --git a/rails/gerbil/spec/views/people/new.html.erb_spec.rb b/rails/gerbil/spec/views/people/new.html.erb_spec.rb new file mode 100644 index 0000000..7540260 --- /dev/null +++ b/rails/gerbil/spec/views/people/new.html.erb_spec.rb @@ -0,0 +1,22 @@ +require 'spec_helper' + +describe "people/new.html.erb" do + before(:each) do + assign(:person, stub_model(Person, + :name => "MyString", + :age => 1, + :zipcode => "MyString" + ).as_new_record) + end + + it "renders new person form" do + render + + # Run the generator again with the --webrat flag if you want to use webrat matchers + assert_select "form", :action => people_path, :method => "post" do + assert_select "input#person_name", :name => "person[name]" + assert_select "input#person_age", :name => "person[age]" + assert_select "input#person_zipcode", :name => "person[zipcode]" + end + end +end diff --git a/rails/gerbil/spec/views/people/show.html.erb_spec.rb b/rails/gerbil/spec/views/people/show.html.erb_spec.rb new file mode 100644 index 0000000..ac751ea --- /dev/null +++ b/rails/gerbil/spec/views/people/show.html.erb_spec.rb @@ -0,0 +1,21 @@ +require 'spec_helper' + +describe "people/show.html.erb" do + before(:each) do + @person = assign(:person, stub_model(Person, + :name => "Name", + :age => 1, + :zipcode => "Zipcode" + )) + end + + it "renders attributes in

" do + render + # Run the generator again with the --webrat flag if you want to use webrat matchers + rendered.should match(/Name/) + # Run the generator again with the --webrat flag if you want to use webrat matchers + rendered.should match(/1/) + # Run the generator again with the --webrat flag if you want to use webrat matchers + rendered.should match(/Zipcode/) + end +end