From 0acc9549b65f2c3cd1840f39e31f2177a98bc55e Mon Sep 17 00:00:00 2001 From: "Ira W. Snyder" Date: Wed, 21 Nov 2007 14:46:35 -0800 Subject: [PATCH] Add video mvc Signed-off-by: Ira W. Snyder --- app/controllers/video_controller.rb | 51 +++++++++++++ app/helpers/video_helper.rb | 2 + app/models/video.rb | 6 ++ app/views/layouts/video.rhtml | 17 +++++ app/views/video/_form.rhtml | 13 ++++ app/views/video/edit.rhtml | 9 +++ app/views/video/list.rhtml | 27 +++++++ app/views/video/new.rhtml | 8 ++ app/views/video/show.rhtml | 8 ++ db/development.sqlite3 | Bin 5120 -> 7168 bytes db/migrate/004_create_videos.rb | 14 ++++ db/schema.rb | 9 ++- test/fixtures/videos.yml | 5 ++ test/functional/video_controller_test.rb | 92 +++++++++++++++++++++++ test/unit/video_test.rb | 10 +++ 15 files changed, 270 insertions(+), 1 deletion(-) create mode 100644 app/controllers/video_controller.rb create mode 100644 app/helpers/video_helper.rb create mode 100644 app/models/video.rb create mode 100644 app/views/layouts/video.rhtml create mode 100644 app/views/video/_form.rhtml create mode 100644 app/views/video/edit.rhtml create mode 100644 app/views/video/list.rhtml create mode 100644 app/views/video/new.rhtml create mode 100644 app/views/video/show.rhtml create mode 100644 db/migrate/004_create_videos.rb create mode 100644 test/fixtures/videos.yml create mode 100644 test/functional/video_controller_test.rb create mode 100644 test/unit/video_test.rb diff --git a/app/controllers/video_controller.rb b/app/controllers/video_controller.rb new file mode 100644 index 0000000..a5508ae --- /dev/null +++ b/app/controllers/video_controller.rb @@ -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 index 0000000..86b3138 --- /dev/null +++ b/app/helpers/video_helper.rb @@ -0,0 +1,2 @@ +module VideoHelper +end diff --git a/app/models/video.rb b/app/models/video.rb new file mode 100644 index 0000000..4ecf31b --- /dev/null +++ b/app/models/video.rb @@ -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 index 0000000..65eeb4e --- /dev/null +++ b/app/views/layouts/video.rhtml @@ -0,0 +1,17 @@ + + + + + + Video: <%= controller.action_name %> + <%= stylesheet_link_tag 'scaffold' %> + + + +

<%= flash[:notice] %>

+ +<%= yield %> + + + diff --git a/app/views/video/_form.rhtml b/app/views/video/_form.rhtml new file mode 100644 index 0000000..ab7e456 --- /dev/null +++ b/app/views/video/_form.rhtml @@ -0,0 +1,13 @@ +<%= error_messages_for 'video' %> + + +


+

+ +


+<%= text_field 'video', 'director' %>

+ +


+<%= text_field 'video', 'genre' %>

+ + diff --git a/app/views/video/edit.rhtml b/app/views/video/edit.rhtml new file mode 100644 index 0000000..6f56611 --- /dev/null +++ b/app/views/video/edit.rhtml @@ -0,0 +1,9 @@ +

Editing video

+ +<% 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 index 0000000..b981202 --- /dev/null +++ b/app/views/video/list.rhtml @@ -0,0 +1,27 @@ +

Listing videos

+ + + + <% for column in Video.content_columns %> + + <% end %> + + +<% for video in @videos %> + + <% for column in Video.content_columns %> + + <% end %> + + + + +<% end %> +
<%= column.human_name %>
<%=h video.send(column.name) %><%= link_to 'Show', :action => 'show', :id => video %><%= link_to 'Edit', :action => 'edit', :id => video %><%= link_to 'Destroy', { :action => 'destroy', :id => video }, :confirm => 'Are you sure?', :method => :post %>
+ +<%= 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 %> + +
+ +<%= link_to 'New video', :action => 'new' %> diff --git a/app/views/video/new.rhtml b/app/views/video/new.rhtml new file mode 100644 index 0000000..66bbda4 --- /dev/null +++ b/app/views/video/new.rhtml @@ -0,0 +1,8 @@ +

New video

+ +<% 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 index 0000000..d7d089d --- /dev/null +++ b/app/views/video/show.rhtml @@ -0,0 +1,8 @@ +<% for column in Video.content_columns %> +

+ <%= column.human_name %>: <%=h @video.send(column.name) %> +

+<% end %> + +<%= link_to 'Edit', :action => 'edit', :id => @video %> | +<%= link_to 'Back', :action => 'list' %> diff --git a/db/development.sqlite3 b/db/development.sqlite3 index 3f744c79c4b95388732b01cf594ddad9b6288c7c..d7d382e493cd7f2982fa66e4dcb6dd03363b9551 100644 GIT binary patch delta 241 zcmZqBXt05BO3z)zdU1FN`796LULkJDg#IfD}w_A5cM%mW3pgYp4g~2c@wkW=2c7_jFUN- z7EP9Ae4`u1$}TP`$=J+al9-f}T9%oTnqLed*_?x19Yb6dLL8lZTou4#lOHmvPoBxB zqLr6gUX+@Xnpm8wq>z-K4`k&jxVXAGhWdmksHdsxC@7_57NsVa false + t.column :director, :string + t.column :genre, :integer + end + end + + def self.down + drop_table :videos + end +end diff --git a/db/schema.rb b/db/schema.rb index 13107c5..c773ea1 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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 index 0000000..b49c4eb --- /dev/null +++ b/test/fixtures/videos.yml @@ -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 index 0000000..32b1fc8 --- /dev/null +++ b/test/functional/video_controller_test.rb @@ -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 index 0000000..21c6c43 --- /dev/null +++ b/test/unit/video_test.rb @@ -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 -- 2.25.1