prompts_controller.rb
4.36 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
class PromptsController < InheritedResources::Base
respond_to :xml, :json
actions :show
belongs_to :question
has_scope :voted_on_by
before_filter :authenticate
# To record a vote
# required parameters - prompt id, ordinality, visitor_identifer?
# optional params - visitor_identifier, appearance_lookup
# After recording vote, next prompt display parameters:
# same as in show - :with_prompt, with_appearance, with visitor_stats, etc
def vote
@question = current_user.questions.find(params[:question_id])
@prompt = @question.prompts.find(params[:id])
vote_options = params[:vote] || {}
vote_options.merge!(:prompt => @prompt, :question => @question)
successful = object= current_user.record_vote(vote_options)
optional_information = []
if params[:next_prompt]
begin
params[:next_prompt].merge!(:with_prompt => true) # We always want to get the next possible prompt
@question_optional_information = @question.get_optional_information(params[:next_prompt])
rescue RuntimeError
respond_to do |format|
format.xml { render :xml => @prompt.to_xml, :status => :conflict and return}
end
end
object = @question.prompts.find(@question_optional_information.delete(:picked_prompt_id))
@question_optional_information.each do |key, value|
optional_information << Proc.new { |options| options[:builder].tag!(key, value)}
end
end
respond_to do |format|
if !successful.nil?
format.xml { render :xml => object.to_xml(:procs => optional_information , :methods => [:left_choice_text, :right_choice_text]), :status => :ok }
format.json { render :json => object.to_json(:procs => optional_information, :methods => [:left_choice_text, :right_choice_text]), :status => :ok }
else
format.xml { render :xml => @prompt.to_xml, :status => :unprocessable_entity }
format.json { render :json => @prompt.to_xml, :status => :unprocessable_entity }
end
end
end
def skip
logger.info "#{current_user.inspect} is skipping."
@question = current_user.questions.find(params[:question_id])
@prompt = @question.prompts.find(params[:id])
skip_options = params[:skip] || {}
skip_options.merge!(:prompt => @prompt, :question => @question)
successful = response = current_user.record_skip(skip_options)
optional_information = []
if params[:next_prompt]
begin
params[:next_prompt].merge!(:with_prompt => true) # We always want to get the next possible prompt
@question_optional_information = @question.get_optional_information(params[:next_prompt])
rescue RuntimeError
respond_to do |format|
format.xml { render :xml => @prompt.to_xml, :status => :conflict and return}
end
end
response = @question.prompts.find(@question_optional_information.delete(:picked_prompt_id))
@question_optional_information.each do |key, value|
optional_information << Proc.new { |options| options[:builder].tag!(key, value)}
end
end
respond_to do |format|
if !successful.nil?
format.xml { render :xml => response.to_xml(:procs => optional_information , :methods => [:left_choice_text, :right_choice_text]), :status => :ok }
format.json { render :json => response.to_json(:procs => optional_information, :methods => [:left_choice_text, :right_choice_text]), :status => :ok }
else
format.xml { render :xml => @prompt.to_xml, :status => :unprocessable_entity }
format.json { render :json => @prompt.to_xml, :status => :unprocessable_entity }
end
end
end
def show
@question = current_user.questions.find(params[:question_id])
@prompt = @question.prompts.find(params[:id], :include => [:left_choice ,:right_choice ])
show! do |format|
format.xml { render :xml => @prompt.to_xml(:methods => [:left_choice_text, :right_choice_text])}
format.json { render :json => @prompt.to_json(:methods => [:left_choice_text, :right_choice_text])}
end
end
protected
def begin_of_association_chain
current_user.questions.find(params[:question_id])
end
def collection
if params[:choice_id].blank?
@prompts
else
end_of_association_chain.with_choice_id(params[:choice_id])
end
end
end