Permalink
Browse files

Basic scaffolding for maze

  • Loading branch information...
1 parent 36f7e0a commit 61e91d742733d77c15cbdd062c7a4622f9f8af87 @KentonWhite committed May 24, 2010
View
47 app/controllers/mazes_controller.rb
@@ -0,0 +1,47 @@
+class MazesController < ApplicationController
+
+ before_filter :login_required
+
+ def index
+ @mazes = Maze.all
+ end
+
+ def show
+ @maze = Maze.find(params[:id])
+ end
+
+ def new
+ @maze = Maze.new
+ end
+
+ def create
+ @maze = Maze.new(params[:maze])
+ if @maze.save
+ flash[:notice] = "Successfully created maze."
+ redirect_to @maze
+ else
+ render :action => 'new'
+ end
+ end
+
+ def edit
+ @maze = Maze.find(params[:id])
+ end
+
+ def update
+ @maze = Maze.find(params[:id])
+ if @maze.update_attributes(params[:maze])
+ flash[:notice] = "Successfully updated maze."
+ redirect_to @maze
+ else
+ render :action => 'edit'
+ end
+ end
+
+ def destroy
+ @maze = Maze.find(params[:id])
+ @maze.destroy
+ flash[:notice] = "Successfully destroyed maze."
+ redirect_to mazes_url
+ end
+end
View
2 app/helpers/mazes_helper.rb
@@ -0,0 +1,2 @@
+module MazesHelper
+end
View
3 app/models/maze.rb
@@ -0,0 +1,3 @@
+class Maze < ActiveRecord::Base
+ attr_accessible :title, :user_id
+end
View
12 app/views/mazes/_form.html.erb
@@ -0,0 +1,12 @@
+<%= form_for @maze do |f| %>
+ <%= f.error_messages %>
+ <p>
+ <%= f.label :title %><br />
+ <%= f.text_field :title %>
+ </p>
+ <p>
+ <%= f.label :user_id %><br />
+ <%= f.text_field :user_id %>
+ </p>
+ <p><%= f.submit %></p>
+<% end %>
View
8 app/views/mazes/edit.html.erb
@@ -0,0 +1,8 @@
+<% title "Edit Maze" %>
+
+<%= render 'form' %>
+
+<p>
+ <%= link_to "Show", @maze %> |
+ <%= link_to "View All", mazes_path %>
+</p>
View
19 app/views/mazes/index.html.erb
@@ -0,0 +1,19 @@
+<% title "Mazes" %>
+
+<table>
+ <tr>
+ <th>Title</th>
+ <th>User</th>
+ </tr>
+ <% for maze in @mazes %>
+ <tr>
+ <td><%= maze.title %></td>
+ <td><%= maze.user_id %></td>
+ <td><%= link_to "Show", maze %></td>
+ <td><%= link_to "Edit", edit_maze_path(maze) %></td>
+ <td><%= link_to "Destroy", maze, :confirm => 'Are you sure?', :method => :delete %></td>
+ </tr>
+ <% end %>
+</table>
+
+<p><%= link_to "New Maze", new_maze_path %></p>
View
5 app/views/mazes/new.html.erb
@@ -0,0 +1,5 @@
+<% title "New Maze" %>
+
+<%= render 'form' %>
+
+<p><%= link_to "Back to List", mazes_path %></p>
View
16 app/views/mazes/show.html.erb
@@ -0,0 +1,16 @@
+<% title "Maze" %>
+
+<p>
+ <strong>Title:</strong>
+ <%= @maze.title %>
+</p>
+<p>
+ <strong>User:</strong>
+ <%= @maze.user_id %>
+</p>
+
+<p>
+ <%= link_to "Edit", edit_maze_path(@maze) %> |
+ <%= link_to "Destroy", @maze, :confirm => 'Are you sure?', :method => :delete %> |
+ <%= link_to "View All", mazes_path %>
+</p>
View
2 config/routes.rb
@@ -1,4 +1,6 @@
Mazer::Application.routes.draw do |map|
+ resources :mazes
+
match 'signup' => 'users#new', :as => :signup
match 'logout' => 'sessions#destroy', :as => :logout
View
13 db/migrate/20100524201102_create_mazes.rb
@@ -0,0 +1,13 @@
+class CreateMazes < ActiveRecord::Migration
+ def self.up
+ create_table :mazes do |t|
+ t.string :title
+ t.integer :user_id
+ t.timestamps
+ end
+ end
+
+ def self.down
+ drop_table :mazes
+ end
+end
View
9 db/schema.rb
@@ -9,7 +9,14 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20100523221212) do
+ActiveRecord::Schema.define(:version => 20100524201102) do
+
+ create_table "mazes", :force => true do |t|
+ t.string "title"
+ t.integer "user_id"
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ end
create_table "users", :force => true do |t|
t.string "username"
View
7 test/fixtures/mazes.yml
@@ -0,0 +1,7 @@
+one:
+ title: MyString
+ user_id: 1
+
+two:
+ title: MyString
+ user_id: 1
View
54 test/functional/mazes_controller_test.rb
@@ -0,0 +1,54 @@
+require 'test_helper'
+
+class MazesControllerTest < ActionController::TestCase
+ def test_index
+ get :index
+ assert_template 'index'
+ end
+
+ def test_show
+ get :show, :id => Maze.first
+ assert_template 'show'
+ end
+
+ def test_new
+ get :new
+ assert_template 'new'
+ end
+
+ def test_create_invalid
+ Maze.any_instance.stubs(:valid?).returns(false)
+ post :create
+ assert_template 'new'
+ end
+
+ def test_create_valid
+ Maze.any_instance.stubs(:valid?).returns(true)
+ post :create
+ assert_redirected_to maze_url(assigns(:maze))
+ end
+
+ def test_edit
+ get :edit, :id => Maze.first
+ assert_template 'edit'
+ end
+
+ def test_update_invalid
+ Maze.any_instance.stubs(:valid?).returns(false)
+ put :update, :id => Maze.first
+ assert_template 'edit'
+ end
+
+ def test_update_valid
+ Maze.any_instance.stubs(:valid?).returns(true)
+ put :update, :id => Maze.first
+ assert_redirected_to maze_url(assigns(:maze))
+ end
+
+ def test_destroy
+ maze = Maze.first
+ delete :destroy, :id => maze
+ assert_redirected_to mazes_url
+ assert !Maze.exists?(maze.id)
+ end
+end
View
7 test/unit/maze_test.rb
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class MazeTest < ActiveSupport::TestCase
+ def test_should_be_valid
+ assert Maze.new.valid?
+ end
+end

0 comments on commit 61e91d7

Please sign in to comment.