Permalink
Browse files

Scaffold Products, Comments

feature Products
  • Loading branch information...
1 parent 35d8062 commit 5ad2836ad713eebf17a478983be391c2fb281986 @pjfitzgibbons committed Feb 5, 2009
Showing with 1,150 additions and 0 deletions.
  1. +85 −0 app/controllers/comments_controller.rb
  2. +85 −0 app/controllers/products_controller.rb
  3. +2 −0 app/helpers/comments_helper.rb
  4. +2 −0 app/helpers/products_helper.rb
  5. +2 −0 app/models/comment.rb
  6. +2 −0 app/models/product.rb
  7. +20 −0 app/views/comments/edit.html.erb
  8. +22 −0 app/views/comments/index.html.erb
  9. +19 −0 app/views/comments/new.html.erb
  10. +13 −0 app/views/comments/show.html.erb
  11. +20 −0 app/views/products/edit.html.erb
  12. +22 −0 app/views/products/index.html.erb
  13. +19 −0 app/views/products/new.html.erb
  14. +13 −0 app/views/products/show.html.erb
  15. +4 −0 config/routes.rb
  16. +14 −0 db/migrate/20090129031627_create_products.rb
  17. +14 −0 db/migrate/20090129031654_create_comments.rb
  18. +28 −0 db/schema.rb
  19. +26 −0 features/manage_products.feature
  20. +24 −0 features/step_definitions/product_steps.rb
  21. +173 −0 spec/controllers/comments_controller_spec.rb
  22. +59 −0 spec/controllers/comments_routing_spec.rb
  23. +173 −0 spec/controllers/products_controller_spec.rb
  24. +59 −0 spec/controllers/products_routing_spec.rb
  25. +9 −0 spec/fixtures/comments.yml
  26. +9 −0 spec/fixtures/products.yml
  27. +11 −0 spec/helpers/comments_helper_spec.rb
  28. +11 −0 spec/helpers/products_helper_spec.rb
  29. +14 −0 spec/models/comment_spec.rb
  30. +14 −0 spec/models/product_spec.rb
  31. +24 −0 spec/views/comments/edit.html.erb_spec.rb
  32. +25 −0 spec/views/comments/index.html.erb_spec.rb
  33. +24 −0 spec/views/comments/new.html.erb_spec.rb
  34. +18 −0 spec/views/comments/show.html.erb_spec.rb
  35. +24 −0 spec/views/products/edit.html.erb_spec.rb
  36. +25 −0 spec/views/products/index.html.erb_spec.rb
  37. +24 −0 spec/views/products/new.html.erb_spec.rb
  38. +18 −0 spec/views/products/show.html.erb_spec.rb
@@ -0,0 +1,85 @@
+class CommentsController < ApplicationController
+ # GET /comments
+ # GET /comments.xml
+ def index
+ @comments = Comment.find(:all)
+
+ respond_to do |format|
+ format.html # index.html.erb
+ format.xml { render :xml => @comments }
+ end
+ end
+
+ # GET /comments/1
+ # GET /comments/1.xml
+ def show
+ @comment = Comment.find(params[:id])
+
+ respond_to do |format|
+ format.html # show.html.erb
+ format.xml { render :xml => @comment }
+ end
+ end
+
+ # GET /comments/new
+ # GET /comments/new.xml
+ def new
+ @comment = Comment.new
+
+ respond_to do |format|
+ format.html # new.html.erb
+ format.xml { render :xml => @comment }
+ end
+ end
+
+ # GET /comments/1/edit
+ def edit
+ @comment = Comment.find(params[:id])
+ end
+
+ # POST /comments
+ # POST /comments.xml
+ def create
+ @comment = Comment.new(params[:comment])
+
+ respond_to do |format|
+ if @comment.save
+ flash[:notice] = 'Comment was successfully created.'
+ format.html { redirect_to(@comment) }
+ format.xml { render :xml => @comment, :status => :created, :location => @comment }
+ else
+ format.html { render :action => "new" }
+ format.xml { render :xml => @comment.errors, :status => :unprocessable_entity }
+ end
+ end
+ end
+
+ # PUT /comments/1
+ # PUT /comments/1.xml
+ def update
+ @comment = Comment.find(params[:id])
+
+ respond_to do |format|
+ if @comment.update_attributes(params[:comment])
+ flash[:notice] = 'Comment was successfully updated.'
+ format.html { redirect_to(@comment) }
+ format.xml { head :ok }
+ else
+ format.html { render :action => "edit" }
+ format.xml { render :xml => @comment.errors, :status => :unprocessable_entity }
+ end
+ end
+ end
+
+ # DELETE /comments/1
+ # DELETE /comments/1.xml
+ def destroy
+ @comment = Comment.find(params[:id])
+ @comment.destroy
+
+ respond_to do |format|
+ format.html { redirect_to(comments_url) }
+ format.xml { head :ok }
+ end
+ end
+end
@@ -0,0 +1,85 @@
+class ProductsController < ApplicationController
+ # GET /products
+ # GET /products.xml
+ def index
+ @products = Product.find(:all)
+
+ respond_to do |format|
+ format.html # index.html.erb
+ format.xml { render :xml => @products }
+ end
+ end
+
+ # GET /products/1
+ # GET /products/1.xml
+ def show
+ @product = Product.find(params[:id])
+
+ respond_to do |format|
+ format.html # show.html.erb
+ format.xml { render :xml => @product }
+ end
+ end
+
+ # GET /products/new
+ # GET /products/new.xml
+ def new
+ @product = Product.new
+
+ respond_to do |format|
+ format.html # new.html.erb
+ format.xml { render :xml => @product }
+ end
+ end
+
+ # GET /products/1/edit
+ def edit
+ @product = Product.find(params[:id])
+ end
+
+ # POST /products
+ # POST /products.xml
+ def create
+ @product = Product.new(params[:product])
+
+ respond_to do |format|
+ if @product.save
+ flash[:notice] = 'Product was successfully created.'
+ format.html { redirect_to(@product) }
+ format.xml { render :xml => @product, :status => :created, :location => @product }
+ else
+ format.html { render :action => "new" }
+ format.xml { render :xml => @product.errors, :status => :unprocessable_entity }
+ end
+ end
+ end
+
+ # PUT /products/1
+ # PUT /products/1.xml
+ def update
+ @product = Product.find(params[:id])
+
+ respond_to do |format|
+ if @product.update_attributes(params[:product])
+ flash[:notice] = 'Product was successfully updated.'
+ format.html { redirect_to(@product) }
+ format.xml { head :ok }
+ else
+ format.html { render :action => "edit" }
+ format.xml { render :xml => @product.errors, :status => :unprocessable_entity }
+ end
+ end
+ end
+
+ # DELETE /products/1
+ # DELETE /products/1.xml
+ def destroy
+ @product = Product.find(params[:id])
+ @product.destroy
+
+ respond_to do |format|
+ format.html { redirect_to(products_url) }
+ format.xml { head :ok }
+ end
+ end
+end
@@ -0,0 +1,2 @@
+module CommentsHelper
+end
@@ -0,0 +1,2 @@
+module ProductsHelper
+end
View
@@ -0,0 +1,2 @@
+class Comment < ActiveRecord::Base
+end
View
@@ -0,0 +1,2 @@
+class Product < ActiveRecord::Base
+end
@@ -0,0 +1,20 @@
+<h1>Editing comment</h1>
+
+<% form_for(@comment) do |f| %>
+ <%= f.error_messages %>
+
+ <p>
+ <%= f.label :name %><br />
+ <%= f.text_field :name %>
+ </p>
+ <p>
+ <%= f.label :body %><br />
+ <%= f.text_area :body %>
+ </p>
+ <p>
+ <%= f.submit "Update" %>
+ </p>
+<% end %>
+
+<%= link_to 'Show', @comment %> |
+<%= link_to 'Back', comments_path %>
@@ -0,0 +1,22 @@
+<h1>Listing comments</h1>
+
+<table>
+ <tr>
+ <th>Name</th>
+ <th>Body</th>
+ </tr>
+
+<% for comment in @comments %>
+ <tr>
+ <td><%=h comment.name %></td>
+ <td><%=h comment.body %></td>
+ <td><%= link_to 'Show', comment %></td>
+ <td><%= link_to 'Edit', edit_comment_path(comment) %></td>
+ <td><%= link_to 'Destroy', comment, :confirm => 'Are you sure?', :method => :delete %></td>
+ </tr>
+<% end %>
+</table>
+
+<br />
+
+<%= link_to 'New comment', new_comment_path %>
@@ -0,0 +1,19 @@
+<h1>New comment</h1>
+
+<% form_for(@comment) do |f| %>
+ <%= f.error_messages %>
+
+ <p>
+ <%= f.label :name %><br />
+ <%= f.text_field :name %>
+ </p>
+ <p>
+ <%= f.label :body %><br />
+ <%= f.text_area :body %>
+ </p>
+ <p>
+ <%= f.submit "Create" %>
+ </p>
+<% end %>
+
+<%= link_to 'Back', comments_path %>
@@ -0,0 +1,13 @@
+<p>
+ <b>Name:</b>
+ <%=h @comment.name %>
+</p>
+
+<p>
+ <b>Body:</b>
+ <%=h @comment.body %>
+</p>
+
+
+<%= link_to 'Edit', edit_comment_path(@comment) %> |
+<%= link_to 'Back', comments_path %>
@@ -0,0 +1,20 @@
+<h1>Editing product</h1>
+
+<% form_for(@product) do |f| %>
+ <%= f.error_messages %>
+
+ <p>
+ <%= f.label :name %><br />
+ <%= f.text_field :name %>
+ </p>
+ <p>
+ <%= f.label :price %><br />
+ <%= f.text_field :price %>
+ </p>
+ <p>
+ <%= f.submit "Update" %>
+ </p>
+<% end %>
+
+<%= link_to 'Show', @product %> |
+<%= link_to 'Back', products_path %>
@@ -0,0 +1,22 @@
+<h1>Listing products</h1>
+
+<table>
+ <tr>
+ <th>Name</th>
+ <th>Price</th>
+ </tr>
+
+<% for product in @products %>
+ <tr>
+ <td><%=h product.name %></td>
+ <td><%=h product.price %></td>
+ <td><%= link_to 'Show', product %></td>
+ <td><%= link_to 'Edit', edit_product_path(product) %></td>
+ <td><%= link_to 'Destroy', product, :confirm => 'Are you sure?', :method => :delete %></td>
+ </tr>
+<% end %>
+</table>
+
+<br />
+
+<%= link_to 'New product', new_product_path %>
@@ -0,0 +1,19 @@
+<h1>New product</h1>
+
+<% form_for(@product) do |f| %>
+ <%= f.error_messages %>
+
+ <p>
+ <%= f.label :name %><br />
+ <%= f.text_field :name %>
+ </p>
+ <p>
+ <%= f.label :price %><br />
+ <%= f.text_field :price %>
+ </p>
+ <p>
+ <%= f.submit "Create" %>
+ </p>
+<% end %>
+
+<%= link_to 'Back', products_path %>
@@ -0,0 +1,13 @@
+<p>
+ <b>Name:</b>
+ <%=h @product.name %>
+</p>
+
+<p>
+ <b>Price:</b>
+ <%=h @product.price %>
+</p>
+
+
+<%= link_to 'Edit', edit_product_path(@product) %> |
+<%= link_to 'Back', products_path %>
View
@@ -1,4 +1,8 @@
ActionController::Routing::Routes.draw do |map|
+ map.resources :comments
+
+ map.resources :products
+
# The priority is based upon order of creation: first created -> highest priority.
# Sample of regular route:
@@ -0,0 +1,14 @@
+class CreateProducts < ActiveRecord::Migration
+ def self.up
+ create_table :products do |t|
+ t.string :name
+ t.decimal :price, :precision => 12, :scale => 2
+
+ t.timestamps
+ end
+ end
+
+ def self.down
+ drop_table :products
+ end
+end
@@ -0,0 +1,14 @@
+class CreateComments < ActiveRecord::Migration
+ def self.up
+ create_table :comments do |t|
+ t.string :name
+ t.text :body
+
+ t.timestamps
+ end
+ end
+
+ def self.down
+ drop_table :comments
+ end
+end
Oops, something went wrong.

0 comments on commit 5ad2836

Please sign in to comment.