choices_controller.rb
6.59 KB
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
class ChoicesController < InheritedResources::Base
respond_to :xml, :json
actions :show, :index, :create, :update
belongs_to :question
has_scope :active, :boolean => true, :only => :index
#caches_page :index
def index
if params[:limit]
@question = Question.find(params[:question_id])#, :include => :choices)
@question.reload
@question.choices.each(&:compute_score!)
unless params[:include_inactive]
@choices = Choice.find(:all, :conditions => {:question_id => @question.id, :active => true}, :limit => params[:limit].to_i, :order => 'score DESC', :include => :item)
else
@choices = Choice.find(:all, :conditions => {:question_id => @question.id}, :limit => params[:limit].to_i, :order => 'score DESC', :include => :item)
end
else
@question = Question.find(params[:question_id], :include => :choices) #eagerloads ALL choices
@question.choices.each(&:compute_score!)
unless params[:include_inactive]
@choices = @question.choices(true).active.find(:all, :include => :item)
else
@choices = @question.choices(true)
end
end
index! do |format|
format.xml { render :xml => @choices.to_xml(:only => [ :data, :score, :id, :active ])}
end
end
def show
show! do |format|
format.xml {
@choice.reload
@choice.compute_score!
@choice.reload
render :xml => @choice.to_xml(:methods => [:item_data, :wins_plus_losses, :question_name])}
format.json { render :json => @choice.to_json(:methods => [:data])}
end
end
def single
@question = current_user.questions.find(params[:question_id])
@prompt = @question.prompts.pick
show! do |format|
format.xml { render :xml => @prompt.to_xml}
format.json { render :json => @prompt.to_json}
end
end
def create_from_abroad
authenticate
expire_page :action => :index
logger.info "inside create_from_abroad"
@question = Question.find params[:question_id]
respond_to do |format|
if @choice = current_user.create_choice(params['params']['data'], @question, {:data => params['params']['data'],
:local_identifier => params['params']['local_identifier']})
saved_choice_id = Proc.new { |options| options[:builder].tag!('saved_choice_id', @choice.id) }
choice_status = Proc.new { |options|
the_status = @choice.active? ? 'active' : 'inactive'
options[:builder].tag!('choice_status', the_status) }
logger.info "successfully saved the choice #{@choice.inspect}"
format.xml { render :xml => @choice.to_xml(:procs => [saved_choice_id, choice_status]), :status => :ok }
# format.xml { render :xml => @question.picked_prompt.to_xml(:methods => [:left_choice_text, :right_choice_text], :procs => [saved_choice_id, choice_status]), :status => :ok }
format.json { render :json => @question.to_json(:procs => [saved_choice_id, choice_status]), :status => :ok }
else
format.xml { render :xml => @choice.errors, :status => :unprocessable_entity }
format.json { render :json => @choice.errors, :status => :unprocessable_entity }
end
end
end
def update_from_abroad
authenticate
expire_page :action => :index
@question = current_user.questions.find(params[:question_id])
@choice = @question.choices.find(params[:id])
respond_to do |format|
if @choice.activate!
logger.info "successfully activated choice #{@choice.inspect}"
format.xml { render :xml => true }
format.json { render :json => true }
else
logger.info "failed to activate choice #{@choice.inspect}"
format.xml { render :xml => @choice.to_xml(:methods => [:data, :votes_count, :wins_plus_losses])}
format.json { render :json => @choice.to_json(:methods => [:data])}
end
end
end
def deactivate_from_abroad
authenticate
@question = current_user.questions.find(params[:question_id])
@choice = @question.choices.find(params[:id])
respond_to do |format|
if @question.choices.active.size < 3
logger.info "will not deactivate choice because that would lead to fewer than two active choices for the question, #{@question.name}"
format.xml { render(:xml => false) and return}
format.json { render :json => false }
elsif @choice.deactivate!
logger.info "successfully deactivated choice #{@choice.inspect}"
format.xml { render :xml => true }
format.json { render :json => true }
else
logger.info "failed to deactivate choice #{@choice.inspect}"
format.xml { render :xml => @choice.to_xml(:methods => [:data, :votes_count, :wins_plus_losses])}
format.json { render :json => @choice.to_json(:methods => [:data])}
end
end
end
def activate
authenticate
@question = current_user.questions.find(params[:question_id])
@choice = @question.choices.find(params[:id])
respond_to do |format|
if @choice.activate!
format.xml { render :xml => @choice.to_xml, :status => :created }
format.json { render :json => @choice.to_json, :status => :created }
else
format.xml { render :xml => @choice.errors, :status => :unprocessable_entity }
format.json { render :json => @choice.to_json }
end
end
end
def suspend
authenticate
@question = current_user.questions.find(params[:question_id])
@choice = @question.choices.find(params[:id])
respond_to do |format|
if @choice.suspend!
format.xml { render :xml => @choice.to_xml, :status => :created }
format.json { render :json => @choice.to_json, :status => :created }
else
format.xml { render :xml => @choice.errors, :status => :unprocessable_entity }
format.json { render :json => @choice.to_json }
end
end
end
def skip
voter = User.by_sid(params['params']['auto'])
logger.info "#{voter.inspect} is skipping."
@question = Question.find(params[:question_id])
@prompt = @question.prompts.find(params[:id])
respond_to do |format|
if @skip = voter.skip(@prompt)
format.xml { render :xml => @question.picked_prompt.to_xml(:methods => [:left_choice_text, :right_choice_text]), :status => :ok }
format.json { render :json => @question.picked_prompt.to_json, :status => :ok }
else
format.xml { render :xml => c, :status => :unprocessable_entity }
format.json { render :json => c, :status => :unprocessable_entity }
end
end
end
end