Commit 08c534cab26097b3c06d6568f6b704947cc03d49
1 parent
1a386d05
Exists in
master
and in
1 other branch
Add a new method visitors#votes that returns the choice info and winner
for every vote a visitor has
Showing
2 changed files
with
28 additions
and
1 deletions
Show diff stats
app/controllers/visitors_controller.rb
... | ... | @@ -27,4 +27,31 @@ class VisitorsController < InheritedResources::Base |
27 | 27 | end |
28 | 28 | end |
29 | 29 | |
30 | + def votes | |
31 | + @visitor = Visitor.find_by_identifier!(params[:id]) | |
32 | + votes = @visitor.votes(:include => [:choice, :loser_choice, :prompt]).order_by {|v| v.created_at} | |
33 | + response = [] | |
34 | + | |
35 | + votes.each do |vote| | |
36 | + winner = (vote.prompt.left_choice_id == vote.choice_id ? 'left' : 'right') | |
37 | + if vote.choice_id == vote.prompt.left_choice_id | |
38 | + left_choice = vote.choice | |
39 | + right_choice = vote.loser_choice | |
40 | + else | |
41 | + left_choice = vote.loser_choice | |
42 | + right_choice = vote.choice | |
43 | + end | |
44 | + vote_response = { | |
45 | + :winner => winner, | |
46 | + :id => vote.id, | |
47 | + :left_choice_id => left_choice.id, | |
48 | + :left_choice_data => left_choice.data, | |
49 | + :right_choice_id => right_choice.id, | |
50 | + :right_choice_data => right_choice.data | |
51 | + } | |
52 | + response << vote_response | |
53 | + end | |
54 | + | |
55 | + render :json => {:votes => response}.to_json | |
56 | + end | |
30 | 57 | end | ... | ... |
config/routes.rb
1 | 1 | ActionController::Routing::Routes.draw do |map| |
2 | 2 | #map.resources :clicks |
3 | 3 | map.resources :densities |
4 | - map.resources :visitors, :collection => {:objects_by_session_ids => :post} | |
4 | + map.resources :visitors, :collection => {:objects_by_session_ids => :post}, :member => {:votes => :get} | |
5 | 5 | map.resources :questions, :member => { :object_info_totals_by_date => :get, |
6 | 6 | :object_info_by_visitor_id => :get, |
7 | 7 | :export => :post, | ... | ... |