Add the Gameplatform MVC to handle the different game platforms
authorIra W. Snyder <devel@irasnyder.com>
Sat, 24 Nov 2007 01:25:23 +0000 (17:25 -0800)
committerIra W. Snyder <devel@irasnyder.com>
Sat, 24 Nov 2007 01:25:23 +0000 (17:25 -0800)
Signed-off-by: Ira W. Snyder <devel@irasnyder.com>
18 files changed:
app/controllers/gameplatform_controller.rb [new file with mode: 0644]
app/helpers/gameplatform_helper.rb [new file with mode: 0644]
app/models/game.rb
app/models/gameplatform.rb [new file with mode: 0644]
app/views/game/_form.rhtml
app/views/game/list.rhtml
app/views/gameplatform/_form.rhtml [new file with mode: 0644]
app/views/gameplatform/edit.rhtml [new file with mode: 0644]
app/views/gameplatform/list.rhtml [new file with mode: 0644]
app/views/gameplatform/new.rhtml [new file with mode: 0644]
app/views/gameplatform/show.rhtml [new file with mode: 0644]
app/views/layouts/gameplatform.rhtml [new file with mode: 0644]
db/development.sqlite3
db/migrate/025_create_gameplatforms.rb [new file with mode: 0644]
db/schema.rb
test/fixtures/gameplatforms.yml [new file with mode: 0644]
test/functional/gameplatform_controller_test.rb [new file with mode: 0644]
test/unit/gameplatform_test.rb [new file with mode: 0644]

diff --git a/app/controllers/gameplatform_controller.rb b/app/controllers/gameplatform_controller.rb
new file mode 100644 (file)
index 0000000..30aadf9
--- /dev/null
@@ -0,0 +1,51 @@
+class GameplatformController < 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
+    @gameplatform_pages, @gameplatforms = paginate :gameplatforms, :per_page => 10
+  end
+
+  def show
+    @gameplatform = Gameplatform.find(params[:id])
+  end
+
+  def new
+    @gameplatform = Gameplatform.new
+  end
+
+  def create
+    @gameplatform = Gameplatform.new(params[:gameplatform])
+    if @gameplatform.save
+      flash[:notice] = 'Gameplatform was successfully created.'
+      redirect_to :action => 'list'
+    else
+      render :action => 'new'
+    end
+  end
+
+  def edit
+    @gameplatform = Gameplatform.find(params[:id])
+  end
+
+  def update
+    @gameplatform = Gameplatform.find(params[:id])
+    if @gameplatform.update_attributes(params[:gameplatform])
+      flash[:notice] = 'Gameplatform was successfully updated.'
+      redirect_to :action => 'show', :id => @gameplatform
+    else
+      render :action => 'edit'
+    end
+  end
+
+  def destroy
+    Gameplatform.find(params[:id]).destroy
+    redirect_to :action => 'list'
+  end
+end
diff --git a/app/helpers/gameplatform_helper.rb b/app/helpers/gameplatform_helper.rb
new file mode 100644 (file)
index 0000000..a8bb6d0
--- /dev/null
@@ -0,0 +1,2 @@
+module GameplatformHelper
+end
index 15bf8e0..ed7f30b 100644 (file)
@@ -6,6 +6,10 @@ class Game < Rentable
     return Gamegenre.find(game_genre).name
   end
 
+  def game_platform
+    return Gameplatform.find(platform).name
+  end
+
   def calculated_price
     # FIXME: generate this based on day of week, newrelease
     day_of_week = Time.now.to_date.wday
diff --git a/app/models/gameplatform.rb b/app/models/gameplatform.rb
new file mode 100644 (file)
index 0000000..340470f
--- /dev/null
@@ -0,0 +1,2 @@
+class Gameplatform < ActiveRecord::Base
+end
index b65208f..69c3904 100644 (file)
@@ -8,7 +8,7 @@
 <%= check_box 'game', 'newrelease' %></p>
 
 <p><label for="game_platform">Platform</label><br/>
-<%= text_field 'game', 'platform'  %></p>
+<%= select 'game', 'platform', Gameplatform.find(:all).collect {|c| [c.name.to_s, c.id] } %></p>
 
 <p><label for="game_genre">Genre</label><br/>
 <%= select 'game', 'game_genre', Gamegenre.find(:all).collect {|c| [c.name.to_s, c.id] } %></p>
index fce55da..d35c1eb 100644 (file)
@@ -17,7 +17,7 @@
     <td><%=h game.title %></td>
     <td><%=h game.newrelease %></td>
     <td><%=h game.genre %></td>
-    <td>FIXME</td>
+    <td><%=h game.game_platform %></td>
     <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>
diff --git a/app/views/gameplatform/_form.rhtml b/app/views/gameplatform/_form.rhtml
new file mode 100644 (file)
index 0000000..7ea8d9b
--- /dev/null
@@ -0,0 +1,7 @@
+<%= error_messages_for 'gameplatform' %>
+
+<!--[form:gameplatform]-->
+<p><label for="gameplatform_name">Name</label><br/>
+<%= text_field 'gameplatform', 'name'  %></p>
+<!--[eoform:gameplatform]-->
+
diff --git a/app/views/gameplatform/edit.rhtml b/app/views/gameplatform/edit.rhtml
new file mode 100644 (file)
index 0000000..5eb3cbd
--- /dev/null
@@ -0,0 +1,9 @@
+<h1>Editing gameplatform</h1>
+
+<% form_tag :action => 'update', :id => @gameplatform do %>
+  <%= render :partial => 'form' %>
+  <%= submit_tag 'Edit' %>
+<% end %>
+
+<%= link_to 'Show', :action => 'show', :id => @gameplatform %> |
+<%= link_to 'Back', :action => 'list' %>
diff --git a/app/views/gameplatform/list.rhtml b/app/views/gameplatform/list.rhtml
new file mode 100644 (file)
index 0000000..e2be35d
--- /dev/null
@@ -0,0 +1,27 @@
+<h1>Listing gameplatforms</h1>
+
+<table>
+  <tr>
+  <% for column in Gameplatform.content_columns %>
+    <th><%= column.human_name %></th>
+  <% end %>
+  </tr>
+  
+<% for gameplatform in @gameplatforms %>
+  <tr>
+  <% for column in Gameplatform.content_columns %>
+    <td><%=h gameplatform.send(column.name) %></td>
+  <% end %>
+    <td><%= link_to 'Show', :action => 'show', :id => gameplatform %></td>
+    <td><%= link_to 'Edit', :action => 'edit', :id => gameplatform %></td>
+    <td><%= link_to 'Destroy', { :action => 'destroy', :id => gameplatform }, :confirm => 'Are you sure?', :method => :post %></td>
+  </tr>
+<% end %>
+</table>
+
+<%= link_to 'Previous page', { :page => @gameplatform_pages.current.previous } if @gameplatform_pages.current.previous %>
+<%= link_to 'Next page', { :page => @gameplatform_pages.current.next } if @gameplatform_pages.current.next %> 
+
+<br />
+
+<%= link_to 'New gameplatform', :action => 'new' %>
diff --git a/app/views/gameplatform/new.rhtml b/app/views/gameplatform/new.rhtml
new file mode 100644 (file)
index 0000000..7341fb4
--- /dev/null
@@ -0,0 +1,8 @@
+<h1>New gameplatform</h1>
+
+<% form_tag :action => 'create' do %>
+  <%= render :partial => 'form' %>
+  <%= submit_tag "Create" %>
+<% end %>
+
+<%= link_to 'Back', :action => 'list' %>
diff --git a/app/views/gameplatform/show.rhtml b/app/views/gameplatform/show.rhtml
new file mode 100644 (file)
index 0000000..12b8ce5
--- /dev/null
@@ -0,0 +1,8 @@
+<% for column in Gameplatform.content_columns %>
+<p>
+  <b><%= column.human_name %>:</b> <%=h @gameplatform.send(column.name) %>
+</p>
+<% end %>
+
+<%= link_to 'Edit', :action => 'edit', :id => @gameplatform %> |
+<%= link_to 'Back', :action => 'list' %>
diff --git a/app/views/layouts/gameplatform.rhtml b/app/views/layouts/gameplatform.rhtml
new file mode 100644 (file)
index 0000000..8abc654
--- /dev/null
@@ -0,0 +1,17 @@
+<!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>Gameplatform: <%= controller.action_name %></title>
+  <%= stylesheet_link_tag 'scaffold' %>
+</head>
+<body>
+
+<p style="color: green"><%= flash[:notice] %></p>
+
+<%= yield  %>
+
+</body>
+</html>
index 6285c9d..92a8e4b 100644 (file)
Binary files a/db/development.sqlite3 and b/db/development.sqlite3 differ
diff --git a/db/migrate/025_create_gameplatforms.rb b/db/migrate/025_create_gameplatforms.rb
new file mode 100644 (file)
index 0000000..854dce8
--- /dev/null
@@ -0,0 +1,11 @@
+class CreateGameplatforms < ActiveRecord::Migration
+  def self.up
+    create_table :gameplatforms do |t|
+      t.column :name, :string, :null => false
+    end
+  end
+
+  def self.down
+    drop_table :gameplatforms
+  end
+end
index d372187..ef94c3e 100644 (file)
@@ -2,7 +2,7 @@
 # migrations feature of ActiveRecord to incrementally modify your database, and
 # then regenerate this schema definition.
 
-ActiveRecord::Schema.define(:version => 24) do
+ActiveRecord::Schema.define(:version => 25) do
 
   create_table "bitems", :force => true do |t|
     t.column "customer_id",    :integer, :null => false
@@ -36,6 +36,10 @@ ActiveRecord::Schema.define(:version => 24) do
     t.column "name", :string, :null => false
   end
 
+  create_table "gameplatforms", :force => true do |t|
+    t.column "name", :string, :null => false
+  end
+
   create_table "medias", :force => true do |t|
     t.column "name", :string, :null => false
   end
diff --git a/test/fixtures/gameplatforms.yml b/test/fixtures/gameplatforms.yml
new file mode 100644 (file)
index 0000000..b49c4eb
--- /dev/null
@@ -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/gameplatform_controller_test.rb b/test/functional/gameplatform_controller_test.rb
new file mode 100644 (file)
index 0000000..f170664
--- /dev/null
@@ -0,0 +1,92 @@
+require File.dirname(__FILE__) + '/../test_helper'
+require 'gameplatform_controller'
+
+# Re-raise errors caught by the controller.
+class GameplatformController; def rescue_action(e) raise e end; end
+
+class GameplatformControllerTest < Test::Unit::TestCase
+  fixtures :gameplatforms
+
+  def setup
+    @controller = GameplatformController.new
+    @request    = ActionController::TestRequest.new
+    @response   = ActionController::TestResponse.new
+
+    @first_id = gameplatforms(: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(:gameplatforms)
+  end
+
+  def test_show
+    get :show, :id => @first_id
+
+    assert_response :success
+    assert_template 'show'
+
+    assert_not_nil assigns(:gameplatform)
+    assert assigns(:gameplatform).valid?
+  end
+
+  def test_new
+    get :new
+
+    assert_response :success
+    assert_template 'new'
+
+    assert_not_nil assigns(:gameplatform)
+  end
+
+  def test_create
+    num_gameplatforms = Gameplatform.count
+
+    post :create, :gameplatform => {}
+
+    assert_response :redirect
+    assert_redirected_to :action => 'list'
+
+    assert_equal num_gameplatforms + 1, Gameplatform.count
+  end
+
+  def test_edit
+    get :edit, :id => @first_id
+
+    assert_response :success
+    assert_template 'edit'
+
+    assert_not_nil assigns(:gameplatform)
+    assert assigns(:gameplatform).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 {
+      Gameplatform.find(@first_id)
+    }
+
+    post :destroy, :id => @first_id
+    assert_response :redirect
+    assert_redirected_to :action => 'list'
+
+    assert_raise(ActiveRecord::RecordNotFound) {
+      Gameplatform.find(@first_id)
+    }
+  end
+end
diff --git a/test/unit/gameplatform_test.rb b/test/unit/gameplatform_test.rb
new file mode 100644 (file)
index 0000000..a796e92
--- /dev/null
@@ -0,0 +1,10 @@
+require File.dirname(__FILE__) + '/../test_helper'
+
+class GameplatformTest < Test::Unit::TestCase
+  fixtures :gameplatforms
+
+  # Replace this with your real tests.
+  def test_truth
+    assert true
+  end
+end