Permalink
Browse files

Create basic link

  • Loading branch information...
1 parent f7b50fc commit f6a5e2202a0ec849a4edc148a8ef8b1303b728cd @KentonWhite committed May 24, 2010
@@ -2,4 +2,14 @@ class ApplicationController < ActionController::Base
include Authentication
protect_from_forgery
layout 'application'
+
+ protected
+
+ def get_maze
+ @maze = current_user.mazes.find params[:maze_id]
+ end
+
+ def get_node
+ @node = @maze.nodes.find params[:node_id]
+ end
end
@@ -0,0 +1,41 @@
+class LinksController < ApplicationController
+
+ before_filter :login_required
+ before_filter :get_maze
+ before_filter :get_node
+
+ def new
+ @link = @node.links.new
+ end
+
+ def create
+ @link = @node.links.new(params[:link])
+ if @link.save
+ flash[:notice] = "Successfully created link."
+ redirect_to root_url
+ else
+ render :action => 'new'
+ end
+ end
+
+ def edit
+ @link = @node.links.find(params[:id])
+ end
+
+ def update
+ @link = @node.links.find(params[:id])
+ if @link.update_attributes(params[:link])
+ flash[:notice] = "Successfully updated link."
+ redirect_to root_url
+ else
+ render :action => 'edit'
+ end
+ end
+
+ def destroy
+ @link = @node.links.find(params[:id])
+ @link.destroy
+ flash[:notice] = "Successfully destroyed link."
+ redirect_to maze_node_path @maze, @node
+ end
+end
@@ -4,19 +4,19 @@ class NodesController < ApplicationController
before_filter :get_maze
def index
- @nodes = Node.all
+ @nodes = @maze.nodes.all
end
def show
- @node = Node.find(params[:id])
+ @node = @maze.nodes.find(params[:id])
end
def new
- @node = Node.new
+ @node = @maze.nodes.new
end
def create
- @node = Node.new(params[:node])
+ @node = @maze.nodes.new(params[:node])
if @node.save
flash[:notice] = "Successfully created node."
redirect_to [@maze, @node]
@@ -26,11 +26,11 @@ def create
end
def edit
- @node = Node.find(params[:id])
+ @node = @maze.nodes.new.find(params[:id])
end
def update
- @node = Node.find(params[:id])
+ @node = @maze.nodes.find(params[:id])
if @node.update_attributes(params[:node])
flash[:notice] = "Successfully updated node."
redirect_to [@maze, @node]
@@ -40,15 +40,10 @@ def update
end
def destroy
- @node = Node.find(params[:id])
+ @node = @maze.nodes.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 LinksHelper
+end
View
@@ -0,0 +1,6 @@
+class Link < ActiveRecord::Base
+
+ belongs_to :node
+
+ attr_accessible :text, :node_id, :to_node_id
+end
View
@@ -1,6 +1,7 @@
class Node < ActiveRecord::Base
- belongs_to :node
+ belongs_to :maze
+ has_many :links
attr_accessible :title, :content, :maze_id
end
@@ -0,0 +1,12 @@
+<%= form_for [@maze,@node,@link] do |f| %>
+ <%= f.error_messages %>
+ <p>
+ <%= f.label :text %><br />
+ <%= f.text_field :text %>
+ </p>
+ <p>
+ <%= f.label :to_node_id %><br />
+ <%= f.text_field :to_node_id %>
+ </p>
+ <p><%= f.submit %></p>
+<% end %>
@@ -0,0 +1,4 @@
+<% title "Edit Link" %>
+
+<%= render 'form' %>
+
@@ -0,0 +1,4 @@
+<% title "New Link" %>
+
+<%= render 'form' %>
+
View
@@ -1,7 +1,9 @@
Mazer::Application.routes.draw do |map|
resources :mazes do
- resources :nodes
+ resources :nodes do
+ resources :links
+ end
end
match 'signup' => 'users#new', :as => :signup
@@ -0,0 +1,14 @@
+class CreateLinks < ActiveRecord::Migration
+ def self.up
+ create_table :links do |t|
+ t.string :text
+ t.integer :node_id
+ t.integer :to_node_id
+ t.timestamps
+ end
+ end
+
+ def self.down
+ drop_table :links
+ end
+end
View
@@ -9,7 +9,15 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20100524203626) do
+ActiveRecord::Schema.define(:version => 20100524210413) do
+
+ create_table "links", :force => true do |t|
+ t.string "text"
+ t.integer "node_id"
+ t.integer "to_node_id"
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ end
create_table "mazes", :force => true do |t|
t.string "title"
View
@@ -0,0 +1,7 @@
+one:
+ text: MyString
+ node_id: 1
+
+two:
+ text: MyString
+ node_id: 1
@@ -0,0 +1,44 @@
+require 'test_helper'
+
+class LinksControllerTest < ActionController::TestCase
+ def test_new
+ get :new
+ assert_template 'new'
+ end
+
+ def test_create_invalid
+ Link.any_instance.stubs(:valid?).returns(false)
+ post :create
+ assert_template 'new'
+ end
+
+ def test_create_valid
+ Link.any_instance.stubs(:valid?).returns(true)
+ post :create
+ assert_redirected_to root_url
+ end
+
+ def test_edit
+ get :edit, :id => Link.first
+ assert_template 'edit'
+ end
+
+ def test_update_invalid
+ Link.any_instance.stubs(:valid?).returns(false)
+ put :update, :id => Link.first
+ assert_template 'edit'
+ end
+
+ def test_update_valid
+ Link.any_instance.stubs(:valid?).returns(true)
+ put :update, :id => Link.first
+ assert_redirected_to root_url
+ end
+
+ def test_destroy
+ link = Link.first
+ delete :destroy, :id => link
+ assert_redirected_to root_url
+ assert !Link.exists?(link.id)
+ end
+end
View
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class LinkTest < ActiveSupport::TestCase
+ def test_should_be_valid
+ assert Link.new.valid?
+ end
+end

0 comments on commit f6a5e22

Please sign in to comment.