/
challenges_controller.rb
107 lines (93 loc) · 2.59 KB
/
challenges_controller.rb
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
class ChallengesController < ApplicationController
before_filter :find_challenge, :except => [:index, :new, :create]
# GET /challenges
# GET /challenges.xml
def index
if logged_in?
@challenges = current_user.challenges.all
else
@challenges = Challenge.unassigned.all
end
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @challenges }
end
end
# GET /challenges/new
# GET /challenges/new.xml
def new
if logged_in?
@challenge = current_user.challenges.new
else
@challenge = Challenge.unassigned.new
end
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @challenge }
end
end
# POST /challenges
# POST /challenges.xml
def create
if logged_in?
@challenge = current_user.challenges.new(params[:challenge])
else
@challenge = Challenge.unassigned.new(params[:challenge])
end
respond_to do |format|
if @challenge.save
format.html { redirect_to(@challenge, :notice => 'Challenge was successfully created.') }
format.xml { render :xml => @challenge, :status => :created, :location => @challenge }
else
format.html { render :action => "new" }
format.xml { render :xml => @challenge.errors, :status => :unprocessable_entity }
end
end
end
# GET /challenges/1/edit
def edit
end
# GET /challenges/1
# GET /challenges/1.xml
def show
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @challenge }
end
end
# PUT /challenges/1
# PUT /challenges/1.xml
def update
respond_to do |format|
if @challenge.update_attributes(params[:challenge])
format.html { redirect_to(@challenge, :notice => 'Challenge was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @challenge.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /challenges/1
# DELETE /challenges/1.xml
def destroy
@challenge.destroy
respond_to do |format|
format.html { redirect_to(challenges_url) }
format.xml { head :ok }
end
end
private
def find_challenge
begin
if logged_in?
@challenge = current_user.challenges.find_by_id(params[:id])
else
@challenge = Challenge.unassigned.find(params[:id])
end
rescue ActiveRecord::RecordNotFound
flash[:error] = "Can't find that challenge!"
return redirect_to challenges_path
end
end
end