--- /dev/null
+class VideogenreController < 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
+ @videogenre_pages, @videogenres = paginate :videogenres, :per_page => 10
+ end
+
+ def show
+ @videogenre = Videogenre.find(params[:id])
+ end
+
+ def new
+ @videogenre = Videogenre.new
+ end
+
+ def create
+ @videogenre = Videogenre.new(params[:videogenre])
+ if @videogenre.save
+ flash[:notice] = 'Videogenre was successfully created.'
+ redirect_to :action => 'list'
+ else
+ render :action => 'new'
+ end
+ end
+
+ def edit
+ @videogenre = Videogenre.find(params[:id])
+ end
+
+ def update
+ @videogenre = Videogenre.find(params[:id])
+ if @videogenre.update_attributes(params[:videogenre])
+ flash[:notice] = 'Videogenre was successfully updated.'
+ redirect_to :action => 'show', :id => @videogenre
+ else
+ render :action => 'edit'
+ end
+ end
+
+ def destroy
+ Videogenre.find(params[:id]).destroy
+ redirect_to :action => 'list'
+ end
+end
--- /dev/null
+module VideogenreHelper
+end
class Video < Rentable
+ has_many :video_genres
+
validates_presence_of :director
validates_presence_of :video_genre
validates_presence_of :media
+
+ protected
+ def validate
+ errors.add(:video_genre, "does not exist in the database") if video_genre.nil?
+ end
end
--- /dev/null
+class Videogenre < ActiveRecord::Base
+ belongs_to :video
+
+ validates_presence_of :name
+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>Videogenre: <%= controller.action_name %></title>
+ <%= stylesheet_link_tag 'scaffold' %>
+</head>
+<body>
+
+<p style="color: green"><%= flash[:notice] %></p>
+
+<%= yield %>
+
+</body>
+</html>
<%= check_box 'video', 'newrelease' %></p>
<p><label for="video_genre">Genre</label><br/>
-<%= text_field 'video', 'video_genre' %></p>
+<%= select 'video', 'video_genre', Videogenre.find(:all).collect {|c| [c.name.to_s, c.id] } %></p>
<p><label for="video_director">Director</label><br/>
<%= text_field 'video', 'director' %></p>
<table>
<tr>
- <th>Video ID</th>
- <th>Checked Out</th>
- <% for column in Video.content_columns %>
- <th><%= column.human_name %></th>
- <% end %>
+ <th>Video ID</th>
+ <th>Checked Out</th>
+ <th>Title</th>
+ <th>New Release</th>
+ <th>Genre</th>
+ <th>Director</th>
+ <th>Media</th>
</tr>
<% for video in @videos %>
<tr>
- <td><%=h video.id %></td>
- <td><%=h video.checkedout? %></td>
- <% for column in Video.content_columns %>
- <td><%=h video.send(column.name) %></td>
- <% end %>
+ <td><%=h video.id %></td>
+ <td><%=h video.checkedout? %></td>
+ <td><%=h video.title %></td>
+ <td><%=h video.newrelease %></td>
+ <td><%=h Videogenre.find(video.video_genre).name %></td>
+ <td><%=h video.director %></td>
+ <td><%=h video.media %></td>
<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>
--- /dev/null
+<%= error_messages_for 'videogenre' %>
+
+<!--[form:videogenre]-->
+<p><label for="videogenre_name">Name</label><br/>
+<%= text_field 'videogenre', 'name' %></p>
+<!--[eoform:videogenre]-->
+
--- /dev/null
+<h1>Editing videogenre</h1>
+
+<% form_tag :action => 'update', :id => @videogenre do %>
+ <%= render :partial => 'form' %>
+ <%= submit_tag 'Edit' %>
+<% end %>
+
+<%= link_to 'Show', :action => 'show', :id => @videogenre %> |
+<%= link_to 'Back', :action => 'list' %>
--- /dev/null
+<h1>Listing videogenres</h1>
+
+<table>
+ <tr>
+ <% for column in Videogenre.content_columns %>
+ <th><%= column.human_name %></th>
+ <% end %>
+ </tr>
+
+<% for videogenre in @videogenres %>
+ <tr>
+ <% for column in Videogenre.content_columns %>
+ <td><%=h videogenre.send(column.name) %></td>
+ <% end %>
+ <td><%= link_to 'Show', :action => 'show', :id => videogenre %></td>
+ <td><%= link_to 'Edit', :action => 'edit', :id => videogenre %></td>
+ <td><%= link_to 'Destroy', { :action => 'destroy', :id => videogenre }, :confirm => 'Are you sure?', :method => :post %></td>
+ </tr>
+<% end %>
+</table>
+
+<%= link_to 'Previous page', { :page => @videogenre_pages.current.previous } if @videogenre_pages.current.previous %>
+<%= link_to 'Next page', { :page => @videogenre_pages.current.next } if @videogenre_pages.current.next %>
+
+<br />
+
+<%= link_to 'New videogenre', :action => 'new' %>
--- /dev/null
+<h1>New videogenre</h1>
+
+<% form_tag :action => 'create' do %>
+ <%= render :partial => 'form' %>
+ <%= submit_tag "Create" %>
+<% end %>
+
+<%= link_to 'Back', :action => 'list' %>
--- /dev/null
+<% for column in Videogenre.content_columns %>
+<p>
+ <b><%= column.human_name %>:</b> <%=h @videogenre.send(column.name) %>
+</p>
+<% end %>
+
+<%= link_to 'Edit', :action => 'edit', :id => @videogenre %> |
+<%= link_to 'Back', :action => 'list' %>
--- /dev/null
+class CreateVideogenres < ActiveRecord::Migration
+ def self.up
+ create_table :videogenres do |t|
+ t.column :name, :string, :null => false
+ end
+ end
+
+ def self.down
+ drop_table :videogenres
+ end
+end
# migrations feature of ActiveRecord to incrementally modify your database, and
# then regenerate this schema definition.
-ActiveRecord::Schema.define(:version => 10) do
+ActiveRecord::Schema.define(:version => 11) do
create_table "coitems", :force => true do |t|
t.column "customer_id", :integer
t.column "platform", :integer
end
+ create_table "videogenres", :force => true do |t|
+ t.column "name", :string, :null => false
+ 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 'videogenre_controller'
+
+# Re-raise errors caught by the controller.
+class VideogenreController; def rescue_action(e) raise e end; end
+
+class VideogenreControllerTest < Test::Unit::TestCase
+ fixtures :videogenres
+
+ def setup
+ @controller = VideogenreController.new
+ @request = ActionController::TestRequest.new
+ @response = ActionController::TestResponse.new
+
+ @first_id = videogenres(: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(:videogenres)
+ end
+
+ def test_show
+ get :show, :id => @first_id
+
+ assert_response :success
+ assert_template 'show'
+
+ assert_not_nil assigns(:videogenre)
+ assert assigns(:videogenre).valid?
+ end
+
+ def test_new
+ get :new
+
+ assert_response :success
+ assert_template 'new'
+
+ assert_not_nil assigns(:videogenre)
+ end
+
+ def test_create
+ num_videogenres = Videogenre.count
+
+ post :create, :videogenre => {}
+
+ assert_response :redirect
+ assert_redirected_to :action => 'list'
+
+ assert_equal num_videogenres + 1, Videogenre.count
+ end
+
+ def test_edit
+ get :edit, :id => @first_id
+
+ assert_response :success
+ assert_template 'edit'
+
+ assert_not_nil assigns(:videogenre)
+ assert assigns(:videogenre).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 {
+ Videogenre.find(@first_id)
+ }
+
+ post :destroy, :id => @first_id
+ assert_response :redirect
+ assert_redirected_to :action => 'list'
+
+ assert_raise(ActiveRecord::RecordNotFound) {
+ Videogenre.find(@first_id)
+ }
+ end
+end
--- /dev/null
+require File.dirname(__FILE__) + '/../test_helper'
+
+class VideogenreTest < Test::Unit::TestCase
+ fixtures :videogenres
+
+ # Replace this with your real tests.
+ def test_truth
+ assert true
+ end
+end