--- /dev/null
+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
--- /dev/null
+module VideoHelper
+end
--- /dev/null
+class Video < ActiveRecord::Base
+ belongs_to :rentable
+
+ validates_presence_of :director
+ validates_presence_of :genre
+end
--- /dev/null
+<!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>
--- /dev/null
+<%= 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]-->
+
--- /dev/null
+<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' %>
--- /dev/null
+<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' %>
--- /dev/null
+<h1>New video</h1>
+
+<% form_tag :action => 'create' do %>
+ <%= render :partial => 'form' %>
+ <%= submit_tag "Create" %>
+<% end %>
+
+<%= link_to 'Back', :action => 'list' %>
--- /dev/null
+<% 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' %>
--- /dev/null
+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
# 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
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
--- /dev/null
+# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
+one:
+ id: 1
+two:
+ id: 2
--- /dev/null
+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
--- /dev/null
+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