Skip to content
Browse files

* added skeleton for basic game support

  • Loading branch information...
1 parent 502d245 commit 369ed061023b0aea0a811fee36eeee493ec88f42 @hone committed
View
87 app/controllers/games_controller.rb
@@ -0,0 +1,87 @@
+class GamesController < ApplicationController
+ WHITELIST = [:show, :index]
+
+ before_filter :login_required
+ before_filter( :except => WHITELIST ) {|c| c.send( "authorized_to_edit?", c.action_name, c.params[:id] ) }
+ before_filter :find_game, :only => [:show, :edit, :update, :destroy]
+
+ def index
+ @games = Game.paginate :page => params[:page], :order => 'created_at DESC'
+
+ respond_to do |format|
+ format.html
+ format.xml { render :xml => @games }
+ end
+ end
+
+ def new
+ @game = Game.new
+
+ respond_to do |format|
+ format.html
+ format.xml { redner :xml => @user }
+ end
+ end
+
+ def create
+ @game = Game.new( params[:game] )
+
+ respond_to do |format|
+ if @game.save
+ flash[:notice] = "Game was successfully created."
+ format.html { redirect_to( @game ) }
+ format.xml { render :xml => @game, :status => :created, :location => @game }
+ else
+ format.html { render :action => 'new' }
+ format.xml { render :xml => @game.errors, :status => :unprocessable_entity }
+ end
+ end
+ end
+
+ def show
+ respond_to do |format|
+ format.html
+ format.xml { render :xml => @game }
+ end
+ end
+
+ def edit
+ respond_to do |format|
+ format.html
+ format.xml { render :xml => @game }
+ end
+ end
+
+ def update
+ respond_to do |format|
+ if @game.update_attributes( params[:game] )
+ flash[:notice] = 'Update was successful.'
+ format.html { redirect_to( @game ) }
+ format.xml { head :ok }
+ else
+ format.html { render :action => "edit" }
+ format.xml { render :xml => @game.errors, :status => :unprocessable_entity }
+ end
+ end
+ end
+
+ def destroy
+ @game.destroy
+
+ respond_to do |format|
+ format.html { redirect_to( @games_url ) }
+ format.xml { head :ok }
+ end
+ end
+
+ private
+ # authorization check to see if allowed to change games, maybe should add a new auth check for deleting?
+ def authorized_to_edit?( action_name, game_id )
+ # TODO HACK HACK HACK, return true for now
+ true
+ end
+
+ def find_game
+ @game = Game.find( params[:id] )
+ end
+end
View
1 app/controllers/users_controller.rb
@@ -6,6 +6,7 @@ class UsersController < ApplicationController
# render new.rhtml
def new
@user = User.new
+ @user.password = @user.password_confirmation = nil
respond_to do |format|
format.html
View
2 app/helpers/games_helper.rb
@@ -0,0 +1,2 @@
+module GamesHelper
+end
View
4 app/models/game.rb
@@ -0,0 +1,4 @@
+class Game < ActiveRecord::Base
+ validates_presence_of :name
+ validates_presence_of :abbreviation
+end
View
42 app/views/games/_form.html.erb
@@ -0,0 +1,42 @@
+<% form_for( @game ) do |f| %>
+ <%= f.error_messages %>
+
+ <p>
+ <%= f.label :name %><br />
+ <%= f.text_field :name %>
+ </p>
+
+ <p>
+ <%= f.label :abbreviation %><br />
+ <%= f.text_field :abbreviation %>
+ </p>
+
+ <p>
+ <%= f.label :forum_thread_link %><br />
+ <%= f.text_field :forum_thread_link %>
+ </p>
+
+ <p>
+ <%= f.label :game_night_thread_link %><br />
+ <%= f.text_field :game_night_thread_link %>
+ </p>
+
+ <p>
+ <%= f.label :clan_thread_link %><br />
+ <%= f.text_field :clan_thread_link %>
+ </p>
+
+ <p>
+ <%= f.label :online_leaderboards_link %><br />
+ <%= f.text_field :online_leaderboards_link %>
+ </p>
+
+ <p>
+ <%= f.label :official_game_link %><br />
+ <%= f.text_field :official_game_link %>
+ </p>
+
+ <p>
+ <%= f.submit button_name %>
+ </p>
+<% end %>
View
2 app/views/games/create.html.erb
@@ -0,0 +1,2 @@
+<h1>Games#create</h1>
+<p>Find me in app/views/games/create.html.erb</p>
View
2 app/views/games/destroy.html.erb
@@ -0,0 +1,2 @@
+<h1>Games#destroy</h1>
+<p>Find me in app/views/games/destroy.html.erb</p>
View
5 app/views/games/edit.html.erb
@@ -0,0 +1,5 @@
+<h1>Editing Game - <%= h( @game.name ) %></h1>
+
+<%= render :partial => 'form', :locals => { :button_name => "Update" } %>
+
+<%= link_to 'Show', @game %> | <%= link_to 'Back', games_path %>
View
23 app/views/games/index.html.erb
@@ -0,0 +1,23 @@
+<h1>Listing games</h1>
+
+<table>
+ <tr>
+ <th>Name</th>
+ <th>Abbreviation</th>
+ </tr>
+
+<% for game in @games %>
+ <tr>
+ <td><%= link_to h( game.name ), game %></td>
+ <td><%= h( game.abbreviation ) %></td>
+ </tr>
+<% end -%>
+</table>
+
+<br />
+
+<%= will_paginate @games %>
+
+<br />
+
+<%= link_to 'New game', new_game_path %>
View
5 app/views/games/new.html.erb
@@ -0,0 +1,5 @@
+<h1>Enter a new game</h1>
+
+<%= render :partial => 'form', :locals => { :button_name => "Create" } %>
+
+<%= link_to 'Back', games_path %>
View
39 app/views/games/show.html.erb
@@ -0,0 +1,39 @@
+<h1><%= h( @game.name ) %></h1>
+
+<p>
+ <b>Name:</b>
+ <%= h( @game.name ) %>
+</p>
+
+<p>
+ <b>Abbreviation:</b>
+ <%= h( @game.abbreviation ) %>
+</p>
+
+<p>
+ <b>Forum Thread Link</b>
+ <%= link_to 'link', h( @game.forum_thread_link ) %>
+</p>
+
+<p>
+ <b>Game Night Thread Link</b>
+ <%= link_to 'link', h( @game.game_night_thread_link ) %>
+</p>
+
+<p>
+ <b>Clan Thread Link</b>
+ <%= link_to 'link', h( @game.clan_thread_link ) %>
+</p>
+
+<p>
+ <b>Online Leaderboards Link</b>
+ <%= link_to 'link', h( @game.online_leaderboards_link ) %>
+</p>
+
+<p>
+ <b>Official Game Link</b>
+ <%= link_to 'link', h( @game.official_game_link ) %>
+</p>
+
+<%= link_to 'Edit', edit_game_path( @game ) %> |
+<%= link_to 'Back', games_path %>
View
2 app/views/games/update.html.erb
@@ -0,0 +1,2 @@
+<h1>Games#update</h1>
+<p>Find me in app/views/games/update.html.erb</p>
View
1 app/views/users/new.html.erb
@@ -1,5 +1,4 @@
<h1>Sign up as a new user</h1>
-<% @user.password = @user.password_confirmation = nil %>
<%= error_messages_for :user %>
<% form_for :user, :url => users_path do |f| -%>
View
2 config/environment.rb
@@ -74,3 +74,5 @@
# Please note that observers generated using script/generate observer need to have an _observer suffix
# config.active_record.observers = :cacher, :garbage_collector, :forum_observer
end
+
+require 'will_paginate' # pagination plugin
View
1 config/routes.rb
@@ -4,6 +4,7 @@
map.register '/register', :controller => 'users', :action => 'create'
map.signup '/signup', :controller => 'users', :action => 'new'
map.resources :users
+ map.resources :games
map.resource :session
View
19 db/migrate/20081215073305_create_games.rb
@@ -0,0 +1,19 @@
+class CreateGames < ActiveRecord::Migration
+ def self.up
+ create_table :games do |t|
+ t.string :name
+ t.string :abbreviation
+ t.string :forum_thread_link
+ t.string :game_night_thread_link
+ t.string :clan_thread_link
+ t.string :online_leaderboards_link
+ t.string :official_game_link
+
+ t.timestamps
+ end
+ end
+
+ def self.down
+ drop_table :games
+ end
+end
View
17 test/fixtures/games.yml
@@ -0,0 +1,17 @@
+# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
+
+one:
+ name: MyString
+ abbreviation: MyString
+ forum_thread_link: MyString
+ clan_thread_link: MyString
+ online_leaderboards_link: MyString
+ official_game_link: MyString
+
+two:
+ name: MyString
+ abbreviation: MyString
+ forum_thread_link: MyString
+ clan_thread_link: MyString
+ online_leaderboards_link: MyString
+ official_game_link: MyString
View
8 test/functional/games_controller_test.rb
@@ -0,0 +1,8 @@
+require 'test_helper'
+
+class GamesControllerTest < ActionController::TestCase
+ # Replace this with your real tests.
+ test "the truth" do
+ assert true
+ end
+end
View
32 test/unit/game_test.rb
@@ -0,0 +1,32 @@
+require 'test_helper'
+
+class GameTest < ActiveSupport::TestCase
+
+ test "should create game" do
+ assert_difference 'Game.count' do
+ game = create_game
+ assert !game.new_record?, "#{game.errors.full_messages.to_sentence}"
+ end
+ end
+
+ test "name can't be blank" do
+ assert_no_difference 'Game.count' do
+ game = create_game( :name => nil )
+ assert game.errors.on(:name)
+ end
+ end
+
+ test "abbreviation can't be blank" do
+ assert_no_difference 'Game.count' do
+ game = create_game( :abbreviation => nil )
+ assert game.errors.on(:abbreviation)
+ end
+ end
+
+ private
+ def create_game(options = {})
+ record = Game.new({ :name => "Resistance 2", :abbreviation => "R2", :forum_thread_link => "http://www.cheapassgamer.com/forums/showthread.php?t=167600" }.merge(options))
+ record.save
+ record
+ end
+end

0 comments on commit 369ed06

Please sign in to comment.
Something went wrong with that request. Please try again.