Add video mvc
authorIra W. Snyder <devel@irasnyder.com>
Wed, 21 Nov 2007 22:46:35 +0000 (14:46 -0800)
committerIra W. Snyder <devel@irasnyder.com>
Wed, 21 Nov 2007 22:46:35 +0000 (14:46 -0800)
Signed-off-by: Ira W. Snyder <devel@irasnyder.com>
15 files changed:
app/controllers/video_controller.rb [new file with mode: 0644]
app/helpers/video_helper.rb [new file with mode: 0644]
app/models/video.rb [new file with mode: 0644]
app/views/layouts/video.rhtml [new file with mode: 0644]
app/views/video/_form.rhtml [new file with mode: 0644]
app/views/video/edit.rhtml [new file with mode: 0644]
app/views/video/list.rhtml [new file with mode: 0644]
app/views/video/new.rhtml [new file with mode: 0644]
app/views/video/show.rhtml [new file with mode: 0644]
db/development.sqlite3
db/migrate/004_create_videos.rb [new file with mode: 0644]
db/schema.rb
test/fixtures/videos.yml [new file with mode: 0644]
test/functional/video_controller_test.rb [new file with mode: 0644]
test/unit/video_test.rb [new file with mode: 0644]

diff --git a/app/controllers/video_controller.rb b/app/controllers/video_controller.rb
new file mode 100644 (file)
index 0000000..a5508ae
--- /dev/null
@@ -0,0 +1,51 @@
+class VideoController < ApplicationController
+  def index
+    list
+    render :action => 'list'
+  end
+
+  # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
+  verify :method => :post, :only => [ :destroy, :create, :update ],
+         :redirect_to => { :action => :list }
+
+  def list
+    @video_pages, @videos = paginate :videos, :per_page => 10
+  end
+
+  def show
+    @video = Video.find(params[:id])
+  end
+
+  def new
+    @video = Video.new
+  end
+
+  def create
+    @video = Video.new(params[:video])
+    if @video.save
+      flash[:notice] = 'Video was successfully created.'
+      redirect_to :action => 'list'
+    else
+      render :action => 'new'
+    end
+  end
+
+  def edit
+    @video = Video.find(params[:id])
+  end
+
+  def update
+    @video = Video.find(params[:id])
+    if @video.update_attributes(params[:video])
+      flash[:notice] = 'Video was successfully updated.'
+      redirect_to :action => 'show', :id => @video
+    else
+      render :action => 'edit'
+    end
+  end
+
+  def destroy
+    Video.find(params[:id]).destroy
+    redirect_to :action => 'list'
+  end
+end
diff --git a/app/helpers/video_helper.rb b/app/helpers/video_helper.rb
new file mode 100644 (file)
index 0000000..86b3138
--- /dev/null
@@ -0,0 +1,2 @@
+module VideoHelper
+end
diff --git a/app/models/video.rb b/app/models/video.rb
new file mode 100644 (file)
index 0000000..4ecf31b
--- /dev/null
@@ -0,0 +1,6 @@
+class Video < ActiveRecord::Base
+  belongs_to :rentable
+
+  validates_presence_of :director
+  validates_presence_of :genre
+end
diff --git a/app/views/layouts/video.rhtml b/app/views/layouts/video.rhtml
new file mode 100644 (file)
index 0000000..65eeb4e
--- /dev/null
@@ -0,0 +1,17 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
+       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
+<head>
+  <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
+  <title>Video: <%= controller.action_name %></title>
+  <%= stylesheet_link_tag 'scaffold' %>
+</head>
+<body>
+
+<p style="color: green"><%= flash[:notice] %></p>
+
+<%= yield  %>
+
+</body>
+</html>
diff --git a/app/views/video/_form.rhtml b/app/views/video/_form.rhtml
new file mode 100644 (file)
index 0000000..ab7e456
--- /dev/null
@@ -0,0 +1,13 @@
+<%= error_messages_for 'video' %>
+
+<!--[form:video]-->
+<p><label for="video_newrelease">Newrelease</label><br/>
+<select id="video_newrelease" name="video[newrelease]"><option value="false" selected>False</option><option value="true">True</option></select></p>
+
+<p><label for="video_director">Director</label><br/>
+<%= text_field 'video', 'director'  %></p>
+
+<p><label for="video_genre">Genre</label><br/>
+<%= text_field 'video', 'genre'  %></p>
+<!--[eoform:video]-->
+
diff --git a/app/views/video/edit.rhtml b/app/views/video/edit.rhtml
new file mode 100644 (file)
index 0000000..6f56611
--- /dev/null
@@ -0,0 +1,9 @@
+<h1>Editing video</h1>
+
+<% form_tag :action => 'update', :id => @video do %>
+  <%= render :partial => 'form' %>
+  <%= submit_tag 'Edit' %>
+<% end %>
+
+<%= link_to 'Show', :action => 'show', :id => @video %> |
+<%= link_to 'Back', :action => 'list' %>
diff --git a/app/views/video/list.rhtml b/app/views/video/list.rhtml
new file mode 100644 (file)
index 0000000..b981202
--- /dev/null
@@ -0,0 +1,27 @@
+<h1>Listing videos</h1>
+
+<table>
+  <tr>
+  <% for column in Video.content_columns %>
+    <th><%= column.human_name %></th>
+  <% end %>
+  </tr>
+  
+<% for video in @videos %>
+  <tr>
+  <% for column in Video.content_columns %>
+    <td><%=h video.send(column.name) %></td>
+  <% end %>
+    <td><%= link_to 'Show', :action => 'show', :id => video %></td>
+    <td><%= link_to 'Edit', :action => 'edit', :id => video %></td>
+    <td><%= link_to 'Destroy', { :action => 'destroy', :id => video }, :confirm => 'Are you sure?', :method => :post %></td>
+  </tr>
+<% end %>
+</table>
+
+<%= link_to 'Previous page', { :page => @video_pages.current.previous } if @video_pages.current.previous %>
+<%= link_to 'Next page', { :page => @video_pages.current.next } if @video_pages.current.next %> 
+
+<br />
+
+<%= link_to 'New video', :action => 'new' %>
diff --git a/app/views/video/new.rhtml b/app/views/video/new.rhtml
new file mode 100644 (file)
index 0000000..66bbda4
--- /dev/null
@@ -0,0 +1,8 @@
+<h1>New video</h1>
+
+<% form_tag :action => 'create' do %>
+  <%= render :partial => 'form' %>
+  <%= submit_tag "Create" %>
+<% end %>
+
+<%= link_to 'Back', :action => 'list' %>
diff --git a/app/views/video/show.rhtml b/app/views/video/show.rhtml
new file mode 100644 (file)
index 0000000..d7d089d
--- /dev/null
@@ -0,0 +1,8 @@
+<% for column in Video.content_columns %>
+<p>
+  <b><%= column.human_name %>:</b> <%=h @video.send(column.name) %>
+</p>
+<% end %>
+
+<%= link_to 'Edit', :action => 'edit', :id => @video %> |
+<%= link_to 'Back', :action => 'list' %>
index 3f744c7..d7d382e 100644 (file)
Binary files a/db/development.sqlite3 and b/db/development.sqlite3 differ
diff --git a/db/migrate/004_create_videos.rb b/db/migrate/004_create_videos.rb
new file mode 100644 (file)
index 0000000..dcc02fa
--- /dev/null
@@ -0,0 +1,14 @@
+class CreateVideos < ActiveRecord::Migration
+  def self.up
+    create_table :videos do |t|
+      t.column :rentable_id, :integer
+      t.column :newrelease, :boolean, :default => false
+      t.column :director, :string
+      t.column :genre, :integer
+    end
+  end
+
+  def self.down
+    drop_table :videos
+  end
+end
index 13107c5..c773ea1 100644 (file)
@@ -2,7 +2,7 @@
 # migrations feature of ActiveRecord to incrementally modify your database, and
 # then regenerate this schema definition.
 
-ActiveRecord::Schema.define(:version => 3) do
+ActiveRecord::Schema.define(:version => 4) do
 
   create_table "coitems", :force => true do |t|
     t.column "customer_id", :integer
@@ -24,4 +24,11 @@ ActiveRecord::Schema.define(:version => 3) do
     t.column "genre", :string
   end
 
+  create_table "videos", :force => true do |t|
+    t.column "rentable_id", :integer
+    t.column "newrelease",  :boolean, :default => false
+    t.column "director",    :string
+    t.column "genre",       :integer
+  end
+
 end
diff --git a/test/fixtures/videos.yml b/test/fixtures/videos.yml
new file mode 100644 (file)
index 0000000..b49c4eb
--- /dev/null
@@ -0,0 +1,5 @@
+# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
+one:
+  id: 1
+two:
+  id: 2
diff --git a/test/functional/video_controller_test.rb b/test/functional/video_controller_test.rb
new file mode 100644 (file)
index 0000000..32b1fc8
--- /dev/null
@@ -0,0 +1,92 @@
+require File.dirname(__FILE__) + '/../test_helper'
+require 'video_controller'
+
+# Re-raise errors caught by the controller.
+class VideoController; def rescue_action(e) raise e end; end
+
+class VideoControllerTest < Test::Unit::TestCase
+  fixtures :videos
+
+  def setup
+    @controller = VideoController.new
+    @request    = ActionController::TestRequest.new
+    @response   = ActionController::TestResponse.new
+
+    @first_id = videos(:first).id
+  end
+
+  def test_index
+    get :index
+    assert_response :success
+    assert_template 'list'
+  end
+
+  def test_list
+    get :list
+
+    assert_response :success
+    assert_template 'list'
+
+    assert_not_nil assigns(:videos)
+  end
+
+  def test_show
+    get :show, :id => @first_id
+
+    assert_response :success
+    assert_template 'show'
+
+    assert_not_nil assigns(:video)
+    assert assigns(:video).valid?
+  end
+
+  def test_new
+    get :new
+
+    assert_response :success
+    assert_template 'new'
+
+    assert_not_nil assigns(:video)
+  end
+
+  def test_create
+    num_videos = Video.count
+
+    post :create, :video => {}
+
+    assert_response :redirect
+    assert_redirected_to :action => 'list'
+
+    assert_equal num_videos + 1, Video.count
+  end
+
+  def test_edit
+    get :edit, :id => @first_id
+
+    assert_response :success
+    assert_template 'edit'
+
+    assert_not_nil assigns(:video)
+    assert assigns(:video).valid?
+  end
+
+  def test_update
+    post :update, :id => @first_id
+    assert_response :redirect
+    assert_redirected_to :action => 'show', :id => @first_id
+  end
+
+  def test_destroy
+    assert_nothing_raised {
+      Video.find(@first_id)
+    }
+
+    post :destroy, :id => @first_id
+    assert_response :redirect
+    assert_redirected_to :action => 'list'
+
+    assert_raise(ActiveRecord::RecordNotFound) {
+      Video.find(@first_id)
+    }
+  end
+end
diff --git a/test/unit/video_test.rb b/test/unit/video_test.rb
new file mode 100644 (file)
index 0000000..21c6c43
--- /dev/null
@@ -0,0 +1,10 @@
+require File.dirname(__FILE__) + '/../test_helper'
+
+class VideoTest < Test::Unit::TestCase
+  fixtures :videos
+
+  # Replace this with your real tests.
+  def test_truth
+    assert true
+  end
+end