--- /dev/null
+class GameController < 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
+ @game_pages, @games = paginate :games, :per_page => 10
+ end
+
+ def show
+ @game = Game.find(params[:id])
+ end
+
+ def new
+ @game = Game.new
+ end
+
+ def create
+ # A new rentable must be created whenever we create a new game
+ @rentable = Rentable.new
+ @rentable.save
+ @game = Game.new(params[:game])
+ @game.rentable_id = @rentable.id
+ if @game.save
+ flash[:notice] = 'Game was successfully created.'
+ redirect_to :action => 'list'
+ else
+ render :action => 'new'
+ end
+ end
+
+ def edit
+ @game = Game.find(params[:id])
+ end
+
+ def update
+ @game = Game.find(params[:id])
+ if @game.update_attributes(params[:game])
+ flash[:notice] = 'Game was successfully updated.'
+ redirect_to :action => 'show', :id => @game
+ else
+ render :action => 'edit'
+ end
+ end
+
+ def destroy
+ Game.find(params[:id]).destroy
+ redirect_to :action => 'list'
+ end
+end
--- /dev/null
+module GameHelper
+end
--- /dev/null
+class Game < ActiveRecord::Base
+ has_one :rentable
+
+ validates_presence_of :title
+ validates_presence_of :genre
+ validates_presence_of :platform
+end
class Rentable < ActiveRecord::Base
has_many :coitem
belongs_to :video
+ belongs_to :game
#validates_presence_of :genre
end
--- /dev/null
+<%= error_messages_for 'game' %>
+
+<!--[form:game]-->
+<p><label for="game_title">Title</label><br/>
+<%= text_field 'game', 'title' %></p>
+
+<p><label for="game_platform">Platform</label><br/>
+<%= text_field 'game', 'platform' %></p>
+
+<p><label for="game_genre">Genre</label><br/>
+<%= text_field 'game', 'genre' %></p>
+<!--[eoform:game]-->
+
--- /dev/null
+<h1>Editing game</h1>
+
+<% form_tag :action => 'update', :id => @game do %>
+ <%= render :partial => 'form' %>
+ <%= submit_tag 'Edit' %>
+<% end %>
+
+<%= link_to 'Show', :action => 'show', :id => @game %> |
+<%= link_to 'Back', :action => 'list' %>
--- /dev/null
+<h1>Listing games</h1>
+
+<table>
+ <tr>
+ <% for column in Game.content_columns %>
+ <th><%= column.human_name %></th>
+ <% end %>
+ </tr>
+
+<% for game in @games %>
+ <tr>
+ <% for column in Game.content_columns %>
+ <td><%=h game.send(column.name) %></td>
+ <% end %>
+ <td><%= link_to 'Show', :action => 'show', :id => game %></td>
+ <td><%= link_to 'Edit', :action => 'edit', :id => game %></td>
+ <td><%= link_to 'Destroy', { :action => 'destroy', :id => game }, :confirm => 'Are you sure?', :method => :post %></td>
+ </tr>
+<% end %>
+</table>
+
+<%= link_to 'Previous page', { :page => @game_pages.current.previous } if @game_pages.current.previous %>
+<%= link_to 'Next page', { :page => @game_pages.current.next } if @game_pages.current.next %>
+
+<br />
+
+<%= link_to 'New game', :action => 'new' %>
--- /dev/null
+<h1>New game</h1>
+
+<% form_tag :action => 'create' do %>
+ <%= render :partial => 'form' %>
+ <%= submit_tag "Create" %>
+<% end %>
+
+<%= link_to 'Back', :action => 'list' %>
--- /dev/null
+<% for column in Game.content_columns %>
+<p>
+ <b><%= column.human_name %>:</b> <%=h @game.send(column.name) %>
+</p>
+<% end %>
+
+<%= link_to 'Edit', :action => 'edit', :id => @game %> |
+<%= link_to 'Back', :action => 'list' %>
--- /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>Game: <%= controller.action_name %></title>
+ <%= stylesheet_link_tag 'scaffold' %>
+</head>
+<body>
+
+<p style="color: green"><%= flash[:notice] %></p>
+
+<%= yield %>
+
+</body>
+</html>
<table>
<tr>
- <th>Title</th>
<% for column in Rentable.content_columns %>
<th><%= column.human_name %></th>
<% end %>
<% for rentable in @rentables %>
<tr>
- <td>
- <%=h Video.find(:first, :conditions => "rentable_id='#{rentable.id}'").title.to_s if Video.find(:first, :conditions => "rentable_id='#{rentable.id}'") %>
- </td>
<% for column in Rentable.content_columns %>
<td><%=h rentable.send(column.name) %></td>
<% end %>
--- /dev/null
+class CreateGames < ActiveRecord::Migration
+ def self.up
+ create_table :games do |t|
+ t.column :title, :string
+ t.column :platform, :integer
+ t.column :genre, :integer
+ t.column :rentable_id, :integer
+ end
+ end
+
+ def self.down
+ drop_table :games
+ end
+end
# migrations feature of ActiveRecord to incrementally modify your database, and
# then regenerate this schema definition.
-ActiveRecord::Schema.define(:version => 5) do
+ActiveRecord::Schema.define(:version => 6) do
create_table "coitems", :force => true do |t|
t.column "customer_id", :integer
t.column "debt", :decimal, :precision => 8, :scale => 2, :default => 0.0
end
+ create_table "games", :force => true do |t|
+ t.column "title", :string
+ t.column "platform", :integer
+ t.column "genre", :integer
+ t.column "rentable_id", :integer
+ end
+
create_table "rentables", :force => true do |t|
t.column "genre", :string
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 'game_controller'
+
+# Re-raise errors caught by the controller.
+class GameController; def rescue_action(e) raise e end; end
+
+class GameControllerTest < Test::Unit::TestCase
+ fixtures :games
+
+ def setup
+ @controller = GameController.new
+ @request = ActionController::TestRequest.new
+ @response = ActionController::TestResponse.new
+
+ @first_id = games(: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(:games)
+ end
+
+ def test_show
+ get :show, :id => @first_id
+
+ assert_response :success
+ assert_template 'show'
+
+ assert_not_nil assigns(:game)
+ assert assigns(:game).valid?
+ end
+
+ def test_new
+ get :new
+
+ assert_response :success
+ assert_template 'new'
+
+ assert_not_nil assigns(:game)
+ end
+
+ def test_create
+ num_games = Game.count
+
+ post :create, :game => {}
+
+ assert_response :redirect
+ assert_redirected_to :action => 'list'
+
+ assert_equal num_games + 1, Game.count
+ end
+
+ def test_edit
+ get :edit, :id => @first_id
+
+ assert_response :success
+ assert_template 'edit'
+
+ assert_not_nil assigns(:game)
+ assert assigns(:game).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 {
+ Game.find(@first_id)
+ }
+
+ post :destroy, :id => @first_id
+ assert_response :redirect
+ assert_redirected_to :action => 'list'
+
+ assert_raise(ActiveRecord::RecordNotFound) {
+ Game.find(@first_id)
+ }
+ end
+end
--- /dev/null
+require File.dirname(__FILE__) + '/../test_helper'
+
+class GameTest < Test::Unit::TestCase
+ fixtures :games
+
+ # Replace this with your real tests.
+ def test_truth
+ assert true
+ end
+end