From f96a0f6949f29078cd2c5e3c81d3ac48ac710fbe Mon Sep 17 00:00:00 2001 From: "Ira W. Snyder" Date: Fri, 23 Nov 2007 17:25:23 -0800 Subject: [PATCH] Add the Gameplatform MVC to handle the different game platforms Signed-off-by: Ira W. Snyder --- app/controllers/gameplatform_controller.rb | 51 ++++++++++ app/helpers/gameplatform_helper.rb | 2 + app/models/game.rb | 4 + app/models/gameplatform.rb | 2 + app/views/game/_form.rhtml | 2 +- app/views/game/list.rhtml | 2 +- app/views/gameplatform/_form.rhtml | 7 ++ app/views/gameplatform/edit.rhtml | 9 ++ app/views/gameplatform/list.rhtml | 27 +++++ app/views/gameplatform/new.rhtml | 8 ++ app/views/gameplatform/show.rhtml | 8 ++ app/views/layouts/gameplatform.rhtml | 17 ++++ db/development.sqlite3 | Bin 23552 -> 24576 bytes db/migrate/025_create_gameplatforms.rb | 11 +++ db/schema.rb | 6 +- test/fixtures/gameplatforms.yml | 5 + .../gameplatform_controller_test.rb | 92 ++++++++++++++++++ test/unit/gameplatform_test.rb | 10 ++ 18 files changed, 260 insertions(+), 3 deletions(-) create mode 100644 app/controllers/gameplatform_controller.rb create mode 100644 app/helpers/gameplatform_helper.rb create mode 100644 app/models/gameplatform.rb create mode 100644 app/views/gameplatform/_form.rhtml create mode 100644 app/views/gameplatform/edit.rhtml create mode 100644 app/views/gameplatform/list.rhtml create mode 100644 app/views/gameplatform/new.rhtml create mode 100644 app/views/gameplatform/show.rhtml create mode 100644 app/views/layouts/gameplatform.rhtml create mode 100644 db/migrate/025_create_gameplatforms.rb create mode 100644 test/fixtures/gameplatforms.yml create mode 100644 test/functional/gameplatform_controller_test.rb create mode 100644 test/unit/gameplatform_test.rb diff --git a/app/controllers/gameplatform_controller.rb b/app/controllers/gameplatform_controller.rb new file mode 100644 index 0000000..30aadf9 --- /dev/null +++ b/app/controllers/gameplatform_controller.rb @@ -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 index 0000000..a8bb6d0 --- /dev/null +++ b/app/helpers/gameplatform_helper.rb @@ -0,0 +1,2 @@ +module GameplatformHelper +end diff --git a/app/models/game.rb b/app/models/game.rb index 15bf8e0..ed7f30b 100644 --- a/app/models/game.rb +++ b/app/models/game.rb @@ -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 index 0000000..340470f --- /dev/null +++ b/app/models/gameplatform.rb @@ -0,0 +1,2 @@ +class Gameplatform < ActiveRecord::Base +end diff --git a/app/views/game/_form.rhtml b/app/views/game/_form.rhtml index b65208f..69c3904 100644 --- a/app/views/game/_form.rhtml +++ b/app/views/game/_form.rhtml @@ -8,7 +8,7 @@ <%= check_box 'game', 'newrelease' %>


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

+<%= select 'game', 'platform', Gameplatform.find(:all).collect {|c| [c.name.to_s, c.id] } %>


<%= select 'game', 'game_genre', Gamegenre.find(:all).collect {|c| [c.name.to_s, c.id] } %>

diff --git a/app/views/game/list.rhtml b/app/views/game/list.rhtml index fce55da..d35c1eb 100644 --- a/app/views/game/list.rhtml +++ b/app/views/game/list.rhtml @@ -17,7 +17,7 @@ <%=h game.title %> <%=h game.newrelease %> <%=h game.genre %> - FIXME + <%=h game.game_platform %> <%= 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 %> diff --git a/app/views/gameplatform/_form.rhtml b/app/views/gameplatform/_form.rhtml new file mode 100644 index 0000000..7ea8d9b --- /dev/null +++ b/app/views/gameplatform/_form.rhtml @@ -0,0 +1,7 @@ +<%= error_messages_for 'gameplatform' %> + + +


+<%= text_field 'gameplatform', 'name' %>

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

Editing gameplatform

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

Listing gameplatforms

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

New gameplatform

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

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

+<% 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 index 0000000..8abc654 --- /dev/null +++ b/app/views/layouts/gameplatform.rhtml @@ -0,0 +1,17 @@ + + + + + + Gameplatform: <%= controller.action_name %> + <%= stylesheet_link_tag 'scaffold' %> + + + +

<%= flash[:notice] %>

+ +<%= yield %> + + + diff --git a/db/development.sqlite3 b/db/development.sqlite3 index 6285c9da9bc9b6b78fc636db9a756dba0e6f619a..92a8e4bc4bfcf1a9b6ae618b060cd56ebc3a303e 100644 GIT binary patch delta 1204 zcmb7^&u<$=6vyYy+t|`3juTt`kvdh!w60|lm-UaBP({Lyyq8C;>t9kGH-kZTZ(xD=rwa`vfor~V9%PlWJiqR~34H-eHLbUPcgeGh^pzLQMAqQhC$^0tIi zwHt!n;yZ#>T}KEvq@Q}q()9-Idvdp?B8&acjR$rklg*`%Mn1Bv1<93#4^r@{a74pn zH&=|k8G|i*pQsf=`l|gUZ+la$TngzQWsO2!I=rYx^N`}OT6)GX(#nY6 zFwdA7_+IDJ*tdTB4@Sx1}QbdPkuDIMPnwE28)J7lR2->=_{FF|xP<=q(Fp+mS@deZ ziDJFi(U>-(;R}NQ;2YIRFM8wJA2_0GNMdtB@$kLg%h-EU7y-i%&L_GZ4e0ffzcPDG zL%d$>PpAgH{l#ze5e+hg<}e0NAk*!{q1?f9!3)VGbSf_2G(}w<(yqzfn(Z{2XN-&G zwMuEd!q!W3OBFUK`ETx&dr$5x2F~|?6G42`Cw+x~;Q@B>Cw!!MzsCAd5XtyzIUGQe zP#5b8Ys+REQjQHFNv@jQYr5Q(w#9N|YG-FW*-=Dtbs%P(B1u(SyQ+bXO9cQ)^mZZt EFKkdXdH?_b delta 305 zcmZoTz}T>Zae|Z(<9h~F;IdKYGCRKnFHn@3S%`sIh false + end + end + + def self.down + drop_table :gameplatforms + end +end diff --git a/db/schema.rb b/db/schema.rb index d372187..ef94c3e 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 => 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 index 0000000..b49c4eb --- /dev/null +++ b/test/fixtures/gameplatforms.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/gameplatform_controller_test.rb b/test/functional/gameplatform_controller_test.rb new file mode 100644 index 0000000..f170664 --- /dev/null +++ b/test/functional/gameplatform_controller_test.rb @@ -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 index 0000000..a796e92 --- /dev/null +++ b/test/unit/gameplatform_test.rb @@ -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 -- 2.25.1