From 7f26973835fc8deef03d25d751e92e4c633ae464 Mon Sep 17 00:00:00 2001 From: "Ira W. Snyder" Date: Wed, 21 Nov 2007 17:37:31 -0800 Subject: [PATCH] Add the game mvc Signed-off-by: Ira W. Snyder --- app/controllers/game_controller.rb | 55 ++++++++++++++ app/helpers/game_helper.rb | 2 + app/models/game.rb | 7 ++ app/models/rentable.rb | 1 + app/views/game/_form.rhtml | 13 ++++ app/views/game/edit.rhtml | 9 +++ app/views/game/list.rhtml | 27 +++++++ app/views/game/new.rhtml | 8 +++ app/views/game/show.rhtml | 8 +++ app/views/layouts/game.rhtml | 17 +++++ app/views/rentable/list.rhtml | 4 -- db/development.sqlite3 | Bin 7168 -> 9216 bytes db/migrate/006_create_games.rb | 14 ++++ db/schema.rb | 9 ++- test/fixtures/games.yml | 5 ++ test/functional/game_controller_test.rb | 92 ++++++++++++++++++++++++ test/unit/game_test.rb | 10 +++ 17 files changed, 276 insertions(+), 5 deletions(-) create mode 100644 app/controllers/game_controller.rb create mode 100644 app/helpers/game_helper.rb create mode 100644 app/models/game.rb create mode 100644 app/views/game/_form.rhtml create mode 100644 app/views/game/edit.rhtml create mode 100644 app/views/game/list.rhtml create mode 100644 app/views/game/new.rhtml create mode 100644 app/views/game/show.rhtml create mode 100644 app/views/layouts/game.rhtml create mode 100644 db/migrate/006_create_games.rb create mode 100644 test/fixtures/games.yml create mode 100644 test/functional/game_controller_test.rb create mode 100644 test/unit/game_test.rb diff --git a/app/controllers/game_controller.rb b/app/controllers/game_controller.rb new file mode 100644 index 0000000..50f6a95 --- /dev/null +++ b/app/controllers/game_controller.rb @@ -0,0 +1,55 @@ +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 diff --git a/app/helpers/game_helper.rb b/app/helpers/game_helper.rb new file mode 100644 index 0000000..34198e3 --- /dev/null +++ b/app/helpers/game_helper.rb @@ -0,0 +1,2 @@ +module GameHelper +end diff --git a/app/models/game.rb b/app/models/game.rb new file mode 100644 index 0000000..9880b04 --- /dev/null +++ b/app/models/game.rb @@ -0,0 +1,7 @@ +class Game < ActiveRecord::Base + has_one :rentable + + validates_presence_of :title + validates_presence_of :genre + validates_presence_of :platform +end diff --git a/app/models/rentable.rb b/app/models/rentable.rb index 0674d8e..12e7df7 100644 --- a/app/models/rentable.rb +++ b/app/models/rentable.rb @@ -1,6 +1,7 @@ class Rentable < ActiveRecord::Base has_many :coitem belongs_to :video + belongs_to :game #validates_presence_of :genre end diff --git a/app/views/game/_form.rhtml b/app/views/game/_form.rhtml new file mode 100644 index 0000000..a0ab990 --- /dev/null +++ b/app/views/game/_form.rhtml @@ -0,0 +1,13 @@ +<%= error_messages_for 'game' %> + + +


+<%= text_field 'game', 'title' %>

+ +


+<%= text_field 'game', 'platform' %>

+ +


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

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

Editing game

+ +<% 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' %> diff --git a/app/views/game/list.rhtml b/app/views/game/list.rhtml new file mode 100644 index 0000000..f1317de --- /dev/null +++ b/app/views/game/list.rhtml @@ -0,0 +1,27 @@ +

Listing games

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

New game

+ +<% form_tag :action => 'create' do %> + <%= render :partial => 'form' %> + <%= submit_tag "Create" %> +<% end %> + +<%= link_to 'Back', :action => 'list' %> diff --git a/app/views/game/show.rhtml b/app/views/game/show.rhtml new file mode 100644 index 0000000..b3c282f --- /dev/null +++ b/app/views/game/show.rhtml @@ -0,0 +1,8 @@ +<% for column in Game.content_columns %> +

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

+<% end %> + +<%= link_to 'Edit', :action => 'edit', :id => @game %> | +<%= link_to 'Back', :action => 'list' %> diff --git a/app/views/layouts/game.rhtml b/app/views/layouts/game.rhtml new file mode 100644 index 0000000..44cd5a7 --- /dev/null +++ b/app/views/layouts/game.rhtml @@ -0,0 +1,17 @@ + + + + + + Game: <%= controller.action_name %> + <%= stylesheet_link_tag 'scaffold' %> + + + +

<%= flash[:notice] %>

+ +<%= yield %> + + + diff --git a/app/views/rentable/list.rhtml b/app/views/rentable/list.rhtml index 0fe063d..39883c5 100644 --- a/app/views/rentable/list.rhtml +++ b/app/views/rentable/list.rhtml @@ -2,7 +2,6 @@ - <% for column in Rentable.content_columns %> <% end %> @@ -10,9 +9,6 @@ <% for rentable in @rentables %> - <% for column in Rentable.content_columns %> <% end %> diff --git a/db/development.sqlite3 b/db/development.sqlite3 index 7a033ca35baa6394dd29b336b54ec58ab1376363..09fe25df8eed8f8f0e5053b277ee7632a731b354 100644 GIT binary patch delta 251 zcmZp$Xz-XIEttT73b-cf*l;m2{|1S2GXLIK*u%`k&bo=^KQklSCJq)BE@tMB49vfo zzcGK@%*XSXnT45|nPKx+etr%f7KSqn3}+ann3R}#Cl>CRyh++`^C~G0#>x97Ur#*wD~JPKL;NxgC7I)CZ<)4`x(wK_)ToInY>1+elnl*r^!lEuQy9caxhLl HBjE=CVy+!y diff --git a/db/migrate/006_create_games.rb b/db/migrate/006_create_games.rb new file mode 100644 index 0000000..75279c0 --- /dev/null +++ b/db/migrate/006_create_games.rb @@ -0,0 +1,14 @@ +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 diff --git a/db/schema.rb b/db/schema.rb index ef81ab8..544fb07 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 => 5) do +ActiveRecord::Schema.define(:version => 6) do create_table "coitems", :force => true do |t| t.column "customer_id", :integer @@ -19,6 +19,13 @@ ActiveRecord::Schema.define(:version => 5) do 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 diff --git a/test/fixtures/games.yml b/test/fixtures/games.yml new file mode 100644 index 0000000..b49c4eb --- /dev/null +++ b/test/fixtures/games.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/game_controller_test.rb b/test/functional/game_controller_test.rb new file mode 100644 index 0000000..39816b0 --- /dev/null +++ b/test/functional/game_controller_test.rb @@ -0,0 +1,92 @@ +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 diff --git a/test/unit/game_test.rb b/test/unit/game_test.rb new file mode 100644 index 0000000..52eab46 --- /dev/null +++ b/test/unit/game_test.rb @@ -0,0 +1,10 @@ +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 -- 2.25.1
Title<%= column.human_name %>
- <%=h Video.find(:first, :conditions => "rentable_id='#{rentable.id}'").title.to_s if Video.find(:first, :conditions => "rentable_id='#{rentable.id}'") %> - <%=h rentable.send(column.name) %>