Permalink
Browse files

Added nodes

  • Loading branch information...
1 parent ce62c71 commit f7b50fcd9aadbbdbb1f8636aa7ec833c471017b1 @KentonWhite committed May 24, 2010
@@ -0,0 +1,54 @@
+class NodesController < ApplicationController
+
+ before_filter :login_required
+ before_filter :get_maze
+
+ def index
+ @nodes = Node.all
+ end
+
+ def show
+ @node = Node.find(params[:id])
+ end
+
+ def new
+ @node = Node.new
+ end
+
+ def create
+ @node = Node.new(params[:node])
+ if @node.save
+ flash[:notice] = "Successfully created node."
+ redirect_to [@maze, @node]
+ else
+ render :action => 'new'
+ end
+ end
+
+ def edit
+ @node = Node.find(params[:id])
+ end
+
+ def update
+ @node = Node.find(params[:id])
+ if @node.update_attributes(params[:node])
+ flash[:notice] = "Successfully updated node."
+ redirect_to [@maze, @node]
+ else
+ render :action => 'edit'
+ end
+ end
+
+ def destroy
+ @node = Node.find(params[:id])
+ @node.destroy
+ flash[:notice] = "Successfully destroyed node."
+ redirect_to maze_nodes_url @maze
+ end
+
+ protected
+
+ def get_maze
+ @maze = current_user.mazes.find params[:maze_id]
+ end
+end
@@ -0,0 +1,2 @@
+module NodesHelper
+end
View
@@ -1,6 +1,7 @@
class Maze < ActiveRecord::Base
- belongs_to :maze
+ belongs_to :maze
+ has_many :nodes
attr_accessible :title, :user_id
end
View
@@ -0,0 +1,6 @@
+class Node < ActiveRecord::Base
+
+ belongs_to :node
+
+ attr_accessible :title, :content, :maze_id
+end
@@ -0,0 +1,12 @@
+<%= form_for([@maze, @node]) do |f| %>
+ <%= f.error_messages %>
+ <p>
+ <%= f.label :title %><br />
+ <%= f.text_field :title %>
+ </p>
+ <p>
+ <%= f.label :content %><br />
+ <%= f.text_area :content %>
+ </p>
+ <p><%= f.submit %></p>
+<% end %>
@@ -0,0 +1,8 @@
+<% title "Edit Node" %>
+
+<%= render 'form' %>
+
+<p>
+ <%= link_to "Show", [@maze, @node] %> |
+ <%= link_to "View All", maze_nodes_path(@maze) %>
+</p>
@@ -0,0 +1,19 @@
+<% title "Nodes" %>
+
+<table>
+ <tr>
+ <th>Title</th>
+ <th>Text</th>
+ </tr>
+ <% for node in @nodes %>
+ <tr>
+ <td><%= node.title %></td>
+ <td><%= node.content %></td>
+ <td><%= link_to "Show", [@maze, node] %></td>
+ <td><%= link_to "Edit", edit_maze_node_path(@maze, node) %></td>
+ <td><%= link_to "Destroy", [@maze, node], :confirm => 'Are you sure?', :method => :delete %></td>
+ </tr>
+ <% end %>
+</table>
+
+<p><%= link_to "New Node", new_maze_node_path(@maze) %></p>
@@ -0,0 +1,5 @@
+<% title "New Node" %>
+
+<%= render 'form' %>
+
+<p><%= link_to "Back to List", maze_nodes_path(@maze) %></p>
@@ -0,0 +1,16 @@
+<% title "Node" %>
+
+<p>
+ <strong>Title:</strong>
+ <%= @node.title %>
+</p>
+<p>
+ <strong>Content:</strong>
+ <%= @node.content %>
+</p>
+
+<p>
+ <%= link_to "Edit", edit_maze_node_path(@maze, @node) %> |
+ <%= link_to "Destroy", [@maze, @node], :confirm => 'Are you sure?', :method => :delete %> |
+ <%= link_to "View All", maze_nodes_path(@maze) %>
+</p>
View
@@ -1,5 +1,8 @@
Mazer::Application.routes.draw do |map|
- resources :mazes
+
+ resources :mazes do
+ resources :nodes
+ end
match 'signup' => 'users#new', :as => :signup
@@ -0,0 +1,14 @@
+class CreateNodes < ActiveRecord::Migration
+ def self.up
+ create_table :nodes do |t|
+ t.string :title
+ t.text :content
+ t.integer :maze_id
+ t.timestamps
+ end
+ end
+
+ def self.down
+ drop_table :nodes
+ end
+end
View
@@ -9,7 +9,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20100524201102) do
+ActiveRecord::Schema.define(:version => 20100524203626) do
create_table "mazes", :force => true do |t|
t.string "title"
@@ -18,6 +18,14 @@
t.datetime "updated_at"
end
+ create_table "nodes", :force => true do |t|
+ t.string "title"
+ t.text "content"
+ t.integer "maze_id"
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ end
+
create_table "users", :force => true do |t|
t.string "username"
t.string "email"
@@ -0,0 +1,9 @@
+one:
+ title: MyString
+ text: MyText
+ maze_id: 1
+
+two:
+ title: MyString
+ text: MyText
+ maze_id: 1
@@ -0,0 +1,54 @@
+require 'test_helper'
+
+class NodesControllerTest < ActionController::TestCase
+ def test_index
+ get :index
+ assert_template 'index'
+ end
+
+ def test_show
+ get :show, :id => Node.first
+ assert_template 'show'
+ end
+
+ def test_new
+ get :new
+ assert_template 'new'
+ end
+
+ def test_create_invalid
+ Node.any_instance.stubs(:valid?).returns(false)
+ post :create
+ assert_template 'new'
+ end
+
+ def test_create_valid
+ Node.any_instance.stubs(:valid?).returns(true)
+ post :create
+ assert_redirected_to node_url(assigns(:node))
+ end
+
+ def test_edit
+ get :edit, :id => Node.first
+ assert_template 'edit'
+ end
+
+ def test_update_invalid
+ Node.any_instance.stubs(:valid?).returns(false)
+ put :update, :id => Node.first
+ assert_template 'edit'
+ end
+
+ def test_update_valid
+ Node.any_instance.stubs(:valid?).returns(true)
+ put :update, :id => Node.first
+ assert_redirected_to node_url(assigns(:node))
+ end
+
+ def test_destroy
+ node = Node.first
+ delete :destroy, :id => node
+ assert_redirected_to nodes_url
+ assert !Node.exists?(node.id)
+ end
+end
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class NodeTest < ActiveSupport::TestCase
+ def test_should_be_valid
+ assert Node.new.valid?
+ end
+end

0 comments on commit f7b50fc

Please sign in to comment.