maxkpage (owner)

Forks

Revisions

gist: 98679 Download_button fork
public
Public Clone URL: git://gist.github.com/98679.git
Embed All Files: show embed
Text only #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
ActionController::Routing::Routes.draw do |map|
  map.resources :fans
 
  map.resources :league_players
  
  
  map.logout '/logout', :controller => 'sessions', :action => 'destroy'
  map.login '/login', :controller => 'sessions', :action => 'new'
  map.check '/check', :controller => 'sessions', :action => 'create'
  
  map.resource :session
 
  map.with_options(:path_prefix => ":slug") do |slug|
    slug.root :controller => "teams", :action => "show"
    slug.resources :players
    slug.resources :leagues
    slug.resources :games
    slug.resources :admins
    slug.resources :users
    slug.resources :game_players
  end
  
  map.register '/register', :controller => 'users', :action => 'create'
  map.signup '/signup', :controller => 'users', :action => 'new'
 
 
  map.resources :field_positions
  map.resources :pictures
  map.resources :game_players
  map.resources :weather_conditions
  map.resources :game_types
  map.resources :outcomes
  map.resources :games
  map.resources :location_types
  map.resources :sponsors
  map.resources :locations
  map.resources :levels
  map.resources :leagues
  map.resources :player_types
  map.resources :players
  map.resources :age_groups
  map.resources :teams
 
  map.connect ':controller/:action/:id'
  map.connect ':controller/:action/:id.:format'
 
end
 
#####################################
 
class GamesController < ApplicationController
  
  before_filter :load_team
  
  # GET /games
  # GET /games.xml
  def index
    @games = Game.find(:all)
 
    respond_to do |format|
      format.html # index.html.erb
      format.xml { render :xml => @games }
    end
  end
 
  # GET /games/1
  # GET /games/1.xml
  def show
    @game = Game.find(params[:id])
    @league = League.find(@game.league_id)
    @team = Team.first(:conditions => ["id = ?", @league.team_id])
    
    respond_to do |format|
      format.html # show.html.erb
      format.xml { render :xml => @game }
    end
  end
 
  # GET /games/new
  # GET /games/new.xml
  def new
    @game = Game.new
 
    respond_to do |format|
      format.html # new.html.erb
      format.xml { render :xml => @game }
    end
  end
 
  # GET /games/1/edit
  def edit
    @game = Game.find(params[:id])
  end
  
  def edit_stats
    @game = Game.find(params[:id])
    
    @game_player = GamePlayer.new
    
    respond_to do |format|
      format.html # new.html.erb
      format.xml { render :xml => @game_player }
    end
  end
 
  # POST /games
  # POST /games.xml
  def create
    @game = Game.new(params[:game])
 
    respond_to do |format|
      if @game.save
        flash[:notice] = 'Game was successfully created.'
        
        @league = League.find(@game.league_id)
        @team = Team.find(@league.team_id)
        
        format.html { redirect_to("/#{@team.slug}/games/#{@game.to_param}") }
        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
 
  # PUT /games/1
  # PUT /games/1.xml
  def update
    @game = Game.find(params[:id])
 
    respond_to do |format|
      if @game.update_attributes(params[:game])
        flash[:notice] = 'Game was successfully updated.'
        
          @league = League.find(@game.league_id)
          @team = Team.find(@league.team_id)
 
          format.html { redirect_to("/#{@team.slug}/games/#{@game.to_param}") }
        
        format.xml { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml { render :xml => @game.errors, :status => :unprocessable_entity }
      end
    end
  end
 
  # DELETE /games/1
  # DELETE /games/1.xml
  def destroy
    @game = Game.find(params[:id])
    @league = League.find(@game.league_id)
    @game.destroy
 
    respond_to do |format|
      format.html { redirect_to(games_path) }
      format.xml { head :ok }
    end
  end
  
  private
    def load_team
      @team = Team.find_by_slug(params[:slug])
      raise ActiveRecord::RecordNotFound unless @team
    end
  
end