Permalink
Browse files

Scaffolding! It makes me look productive!

  • Loading branch information...
L. David Varvel
L. David Varvel committed Sep 30, 2011
1 parent 6b0b4ef commit 4f440617230e3da3ae0393c93981067669e4766a
@@ -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/
@@ -0,0 +1,3 @@
+// Place all the styles related to the users controller here.
+// They will automatically be included in application.css.
+// You can use Sass (SCSS) here: http://sass-lang.com/
@@ -0,0 +1,83 @@
+class UsersController < ApplicationController
+ # GET /users
+ # GET /users.json
+ def index
+ @users = User.all
+
+ respond_to do |format|
+ format.html # index.html.erb
+ format.json { render :json => @users }
+ end
+ end
+
+ # GET /users/1
+ # GET /users/1.json
+ def show
+ @user = User.find(params[:id])
+
+ respond_to do |format|
+ format.html # show.html.erb
+ format.json { render :json => @user }
+ end
+ end
+
+ # GET /users/new
+ # GET /users/new.json
+ def new
+ @user = User.new
+
+ respond_to do |format|
+ format.html # new.html.erb
+ format.json { render :json => @user }
+ end
+ end
+
+ # GET /users/1/edit
+ def edit
+ @user = User.find(params[:id])
+ end
+
+ # POST /users
+ # POST /users.json
+ def create
+ @user = User.new(params[:user])
+
+ respond_to do |format|
+ if @user.save
+ format.html { redirect_to @user, :notice => 'User was successfully created.' }
+ format.json { render :json => @user, :status => :created, :location => @user }
+ else
+ format.html { render :action => "new" }
+ format.json { render :json => @user.errors, :status => :unprocessable_entity }
+ end
+ end
+ end
+
+ # PUT /users/1
+ # PUT /users/1.json
+ def update
+ @user = User.find(params[:id])
+
+ respond_to do |format|
+ if @user.update_attributes(params[:user])
+ format.html { redirect_to @user, :notice => 'User was successfully updated.' }
+ format.json { head :ok }
+ else
+ format.html { render :action => "edit" }
+ format.json { render :json => @user.errors, :status => :unprocessable_entity }
+ end
+ end
+ end
+
+ # DELETE /users/1
+ # DELETE /users/1.json
+ def destroy
+ @user = User.find(params[:id])
+ @user.destroy
+
+ respond_to do |format|
+ format.html { redirect_to users_url }
+ format.json { head :ok }
+ end
+ end
+end
@@ -0,0 +1,2 @@
+module UsersHelper
+end
View
@@ -0,0 +1,2 @@
+class User < ActiveRecord::Base
+end
@@ -0,0 +1,17 @@
+<%= form_for(@user) do |f| %>
+ <% if @user.errors.any? %>
+ <div id="error_explanation">
+ <h2><%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:</h2>
+
+ <ul>
+ <% @user.errors.full_messages.each do |msg| %>
+ <li><%= msg %></li>
+ <% end %>
+ </ul>
+ </div>
+ <% end %>
+
+ <div class="actions">
+ <%= f.submit %>
+ </div>
+<% end %>
@@ -0,0 +1,6 @@
+<h1>Editing user</h1>
+
+<%= render 'form' %>
+
+<%= link_to 'Show', @user %> |
+<%= link_to 'Back', users_path %>
@@ -0,0 +1,21 @@
+<h1>Listing users</h1>
+
+<table>
+ <tr>
+ <th></th>
+ <th></th>
+ <th></th>
+ </tr>
+
+<% @users.each do |user| %>
+ <tr>
+ <td><%= link_to 'Show', user %></td>
+ <td><%= link_to 'Edit', edit_user_path(user) %></td>
+ <td><%= link_to 'Destroy', user, :confirm => 'Are you sure?', :method => :delete %></td>
+ </tr>
+<% end %>
+</table>
+
+<br />
+
+<%= link_to 'New User', new_user_path %>
@@ -0,0 +1,5 @@
+<h1>New user</h1>
+
+<%= render 'form' %>
+
+<%= link_to 'Back', users_path %>
@@ -0,0 +1,5 @@
+<p id="notice"><%= notice %></p>
+
+
+<%= link_to 'Edit', edit_user_path(@user) %> |
+<%= link_to 'Back', users_path %>
View
@@ -1,4 +1,6 @@
Podblog::Application.routes.draw do
+ resources :users
+
resources :podcasts
resources :posts
@@ -0,0 +1,8 @@
+class CreateUsers < ActiveRecord::Migration
+ def change
+ create_table :users do |t|
+
+ t.timestamps
+ end
+ end
+end
@@ -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 UsersController do
+
+ # This should return the minimal set of attributes required to create a valid
+ # User. As you add validations to User, be sure to
+ # update the return value of this method accordingly.
+ def valid_attributes
+ {}
+ end
+
+ describe "GET index" do
+ it "assigns all users as @users" do
+ user = User.create! valid_attributes
+ get :index
+ assigns(:users).should eq([user])
+ end
+ end
+
+ describe "GET show" do
+ it "assigns the requested user as @user" do
+ user = User.create! valid_attributes
+ get :show, :id => user.id.to_s
+ assigns(:user).should eq(user)
+ end
+ end
+
+ describe "GET new" do
+ it "assigns a new user as @user" do
+ get :new
+ assigns(:user).should be_a_new(User)
+ end
+ end
+
+ describe "GET edit" do
+ it "assigns the requested user as @user" do
+ user = User.create! valid_attributes
+ get :edit, :id => user.id.to_s
+ assigns(:user).should eq(user)
+ end
+ end
+
+ describe "POST create" do
+ describe "with valid params" do
+ it "creates a new User" do
+ expect {
+ post :create, :user => valid_attributes
+ }.to change(User, :count).by(1)
+ end
+
+ it "assigns a newly created user as @user" do
+ post :create, :user => valid_attributes
+ assigns(:user).should be_a(User)
+ assigns(:user).should be_persisted
+ end
+
+ it "redirects to the created user" do
+ post :create, :user => valid_attributes
+ response.should redirect_to(User.last)
+ end
+ end
+
+ describe "with invalid params" do
+ it "assigns a newly created but unsaved user as @user" do
+ # Trigger the behavior that occurs when invalid params are submitted
+ User.any_instance.stub(:save).and_return(false)
+ post :create, :user => {}
+ assigns(:user).should be_a_new(User)
+ end
+
+ it "re-renders the 'new' template" do
+ # Trigger the behavior that occurs when invalid params are submitted
+ User.any_instance.stub(:save).and_return(false)
+ post :create, :user => {}
+ response.should render_template("new")
+ end
+ end
+ end
+
+ describe "PUT update" do
+ describe "with valid params" do
+ it "updates the requested user" do
+ user = User.create! valid_attributes
+ # Assuming there are no other users in the database, this
+ # specifies that the User created on the previous line
+ # receives the :update_attributes message with whatever params are
+ # submitted in the request.
+ User.any_instance.should_receive(:update_attributes).with({'these' => 'params'})
+ put :update, :id => user.id, :user => {'these' => 'params'}
+ end
+
+ it "assigns the requested user as @user" do
+ user = User.create! valid_attributes
+ put :update, :id => user.id, :user => valid_attributes
+ assigns(:user).should eq(user)
+ end
+
+ it "redirects to the user" do
+ user = User.create! valid_attributes
+ put :update, :id => user.id, :user => valid_attributes
+ response.should redirect_to(user)
+ end
+ end
+
+ describe "with invalid params" do
+ it "assigns the user as @user" do
+ user = User.create! valid_attributes
+ # Trigger the behavior that occurs when invalid params are submitted
+ User.any_instance.stub(:save).and_return(false)
+ put :update, :id => user.id.to_s, :user => {}
+ assigns(:user).should eq(user)
+ end
+
+ it "re-renders the 'edit' template" do
+ user = User.create! valid_attributes
+ # Trigger the behavior that occurs when invalid params are submitted
+ User.any_instance.stub(:save).and_return(false)
+ put :update, :id => user.id.to_s, :user => {}
+ response.should render_template("edit")
+ end
+ end
+ end
+
+ describe "DELETE destroy" do
+ it "destroys the requested user" do
+ user = User.create! valid_attributes
+ expect {
+ delete :destroy, :id => user.id.to_s
+ }.to change(User, :count).by(-1)
+ end
+
+ it "redirects to the users list" do
+ user = User.create! valid_attributes
+ delete :destroy, :id => user.id.to_s
+ response.should redirect_to(users_url)
+ end
+ end
+
+end
@@ -0,0 +1,15 @@
+require 'spec_helper'
+
+# Specs in this file have access to a helper object that includes
+# the UsersHelper. For example:
+#
+# describe UsersHelper do
+# describe "string concat" do
+# it "concats two strings with spaces" do
+# helper.concat_strings("this","that").should == "this that"
+# end
+# end
+# end
+describe UsersHelper do
+ pending "add some examples to (or delete) #{__FILE__}"
+end
View
@@ -0,0 +1,5 @@
+require 'spec_helper'
+
+describe User do
+ pending "add some examples to (or delete) #{__FILE__}"
+end
@@ -0,0 +1,11 @@
+require 'spec_helper'
+
+describe "Users" do
+ describe "GET /users" 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 users_path
+ response.status.should be(200)
+ end
+ end
+end
Oops, something went wrong.

0 comments on commit 4f44061

Please sign in to comment.