Commit ce01c5713fc24753b8318fddd74493465ebdae85

Authored by Pius Uzamere
1 parent 782e8f5c

clicks are being recorded, as are votes. also question is being shown properly …

…and prompts are being retrieved
app/controllers/clicks_controller.rb
@@ -40,7 +40,9 @@ class ClicksController < ApplicationController @@ -40,7 +40,9 @@ class ClicksController < ApplicationController
40 # POST /clicks 40 # POST /clicks
41 # POST /clicks.xml 41 # POST /clicks.xml
42 def create 42 def create
43 - @click = Click.new(params[:click]) 43 + authenticate
  44 + p = params[:click].except(:sid).merge(:visitor_id => current_user.visitors.find_or_create_by_identifier(params[:click][:sid]).id)
  45 + @click = Click.new(p)
44 46
45 respond_to do |format| 47 respond_to do |format|
46 if @click.save 48 if @click.save
app/controllers/prompts_controller.rb
1 -class PromptsController < ApplicationController  
2 - # GET /prompts  
3 - # GET /prompts.xml  
4 - def index  
5 - @prompts = Prompt.all  
6 - 1 +class PromptsController < InheritedResources::Base
  2 + respond_to :xml, :json
  3 + actions :show, :index
  4 + belongs_to :question
  5 + has_scope :active, :boolean => true, :only => :index
  6 +
  7 + has_scope :voted_on_by
  8 + #before_filter :authenticate
  9 +
  10 +
  11 + def activate
  12 + # turning off auth for now: @question = current_user.questions.find(params[:question_id])
  13 + @question = Question.find(params[:question_id])
  14 + @prompt = @question.prompts.find(params[:id])
7 respond_to do |format| 15 respond_to do |format|
8 - format.html # index.html.erb  
9 - format.xml { render :xml => @prompts } 16 + if @prompt.activate!
  17 + format.xml { render :xml => @choice.to_xml, :status => :created }
  18 + format.json { render :json => @choice.to_json, :status => :created }
  19 + else
  20 + format.xml { render :xml => @choice.errors, :status => :unprocessable_entity }
  21 + format.json { render :json => @choice.to_json }
  22 + end
10 end 23 end
11 end 24 end
12 -  
13 - # GET /prompts/1  
14 - # GET /prompts/1.xml  
15 - def show  
16 - @prompt = Prompt.find(params[:id])  
17 - 25 +
  26 + def vote
  27 + #NOT IMPLEMENTED
  28 + @question = Question.find(params[:question_id])
  29 + @prompt = @question.prompts.find(params[:id])
  30 + @choices = @prompt.choices.active
  31 + @choice = @choices[params[:index]]
18 respond_to do |format| 32 respond_to do |format|
19 - format.html # show.html.erb  
20 - format.xml { render :xml => @prompt } 33 + format.xml { render :xml => @choice.to_xml }
  34 + format.json { render :json => @choice.to_xml }
21 end 35 end
22 end 36 end
23 -  
24 - # GET /prompts/new  
25 - # GET /prompts/new.xml  
26 - def new  
27 - @prompt = Prompt.new  
28 - 37 +
  38 + def vote_left
  39 + vote_direction(:left)
  40 + end
  41 +
  42 +
  43 + def vote_right
  44 + vote_direction(:right)
  45 + end
  46 +
  47 +
  48 + def vote_direction(direction)
  49 + authenticate
  50 +
  51 + logger.info "#{current_user.inspect} is voting #{direction}."
  52 + @question = Question.find(params[:question_id])
  53 + @prompt = @question.prompts.find(params[:id])
  54 + case direction
  55 + when :left
  56 + successful = c = current_user.record_vote(params['params']['auto'], @prompt, 0)
  57 + when :right
  58 + successful = c = current_user.record_vote(params['params']['auto'], @prompt, 1)
  59 + else
  60 + raise "need to specify either ':left' or ':right' as a direction"
  61 + end
  62 +
  63 +
29 respond_to do |format| 64 respond_to do |format|
30 - format.html # new.html.erb  
31 - format.xml { render :xml => @prompt } 65 + if successful
  66 + format.xml { render :xml => @question.picked_prompt.to_xml(:methods => [:left_choice_text, :right_choice_text, :left_choice_id, :right_choice_id]), :status => :ok }
  67 + format.json { render :json => @question.picked_prompt.to_json(:methods => [:left_choice_text, :right_choice_text, :left_choice_id, :right_choice_id]), :status => :ok }
  68 + else
  69 + format.xml { render :xml => c, :status => :unprocessable_entity }
  70 + format.json { render :json => c, :status => :unprocessable_entity }
  71 + end
32 end 72 end
33 end 73 end
34 -  
35 - # GET /prompts/1/edit  
36 - def edit  
37 - @prompt = Prompt.find(params[:id])  
38 - end  
39 -  
40 - # POST /prompts  
41 - # POST /prompts.xml  
42 - def create  
43 - @prompt = Prompt.new(params[:prompt])  
44 - 74 +
  75 +
  76 +
  77 +
  78 +
  79 + def suspend
  80 + @question = current_user.questions.find(params[:question_id])
  81 + @prompt = @question.prompts.find(params[:id])
45 respond_to do |format| 82 respond_to do |format|
46 - if @prompt.save  
47 - flash[:notice] = 'Prompt was successfully created.'  
48 - format.html { redirect_to(@prompt) }  
49 - format.xml { render :xml => @prompt, :status => :created, :location => @prompt } 83 + if @prompt.suspend!
  84 + format.xml { render :xml => @choice.to_xml, :status => :created }
  85 + format.json { render :json => @choice.to_json, :status => :created }
50 else 86 else
51 - format.html { render :action => "new" }  
52 - format.xml { render :xml => @prompt.errors, :status => :unprocessable_entity } 87 + format.xml { render :xml => @choice.errors, :status => :unprocessable_entity }
  88 + format.json { render :json => @choice.to_json }
53 end 89 end
54 end 90 end
55 end 91 end
56 -  
57 - # PUT /prompts/1  
58 - # PUT /prompts/1.xml  
59 - def update  
60 - @prompt = Prompt.find(params[:id])  
61 - 92 +
  93 +
  94 +
  95 + def skip
  96 + voter = User.auto_create_user_object_from_sid(params['params']['auto'])
  97 + logger.info "#{voter.inspect} is skipping."
  98 + @question = Question.find(params[:question_id])
  99 + @prompt = @question.prompts.find(params[:id])
62 respond_to do |format| 100 respond_to do |format|
63 - if @prompt.update_attributes(params[:prompt])  
64 - flash[:notice] = 'Prompt was successfully updated.'  
65 - format.html { redirect_to(@prompt) }  
66 - format.xml { head :ok } 101 + if @skip = voter.skip(@prompt)
  102 + format.xml { render :xml => @question.picked_prompt.to_xml(:methods => [:left_choice_text, :right_choice_text]), :status => :ok }
  103 + format.json { render :json => @question.picked_prompt.to_json, :status => :ok }
67 else 104 else
68 - format.html { render :action => "edit" }  
69 - format.xml { render :xml => @prompt.errors, :status => :unprocessable_entity } 105 + format.xml { render :xml => c, :status => :unprocessable_entity }
  106 + format.json { render :json => c, :status => :unprocessable_entity }
70 end 107 end
71 end 108 end
72 end 109 end
  110 +
73 111
74 - # DELETE /prompts/1  
75 - # DELETE /prompts/1.xml  
76 - def destroy  
77 - @prompt = Prompt.find(params[:id])  
78 - @prompt.destroy  
79 -  
80 - respond_to do |format|  
81 - format.html { redirect_to(prompts_url) }  
82 - format.xml { head :ok } 112 + # GET /prompts
  113 + # ==== Return
  114 + # Array of length n. Prompts matching parameters
  115 + # ==== Options (params)
  116 + # question_id<String>:: Converted to integer. Must be greater than 0 and
  117 + # belong to the current user. Must belong to user.
  118 + # item_ids<String>:: Comma seperated list of items to include. May only
  119 + # include commas and digits. Must belong to user. Optional value.
  120 + # data<String>:: Flag for whether to include item data. Data included
  121 + # if value is not nil.
  122 + # ==== Raises
  123 + # PermissionError:: If question or any item doesn't belong to current user.
  124 +
  125 + def index
  126 + # turning off auth for now: @question = current_user.questions.find(params[:question_id])
  127 + #authenticate
  128 + @question = Question.find(params[:question_id])
  129 + @prompts = @question.prompts
  130 + #raise @question.inspect
  131 + index! do |format|
  132 + if !params[:voter_id].blank?
  133 + format.xml { render :xml => User.find(params[:voter_id]).prompts_voted_on.to_xml(:include => [:items, :votes],
  134 + :methods => [ :active_items_count,
  135 + :all_items_count,
  136 + :votes_count ]) }
  137 + format.json { render :json => User.find(params[:voter_id]).prompts_voted_on.to_json(:include => [:items, :votes],
  138 + :methods => [ :active_items_count,
  139 + :all_items_count,
  140 + :votes_count ]) }
  141 + else
  142 + format.xml { render :xml => params[:data].blank? ?
  143 + @prompts.to_xml :
  144 + @prompts.to_xml(:include => [:items])
  145 + }
  146 + format.json { render :json => params[:data].blank? ? @prompts.to_json : @prompts.to_json(:include => [:items]) }
  147 + end
83 end 148 end
84 end 149 end
85 -end 150 +
  151 + def show
  152 + @question = Question.find(params[:question_id])
  153 + @prompt = @question.prompts.find(params[:id])
  154 + show! do |format|
  155 + format.xml { render :xml => @prompt.to_xml(:methods => [:left_choice_text, :right_choice_text])}
  156 + format.json { render :json => @prompt.to_json(:methods => [:left_choice_text, :right_choice_text])}
  157 + end
  158 + end
  159 +
  160 + def single
  161 + @question = current_user.questions.find(params[:question_id])
  162 + @prompt = @question.prompts.pick
  163 + show! do |format|
  164 + format.xml { render :xml => @prompt.to_xml}
  165 + format.json { render :json => @prompt.to_json}
  166 + end
  167 + end
  168 +
  169 + #
  170 + # def index
  171 + # index! do |format|
  172 + # format.xml { params[:data].blank? ? @prompts.to_xml : @prompts.to_xml(:include => [:items]) }
  173 + # format.json { params[:data].blank? ? @prompts.to_json : @prompts.to_json(:include => [:items]) }
  174 + # end
  175 + # end
  176 +
  177 + protected
  178 + def begin_of_association_chain
  179 + current_user.questions.find(params[:question_id])
  180 + end
  181 +
  182 + def collection
  183 + if params[:choice_id].blank?
  184 + @prompts
  185 + else
  186 + end_of_association_chain.with_choice_id(params[:choice_id])
  187 + end
  188 + end
  189 +end
86 \ No newline at end of file 190 \ No newline at end of file
app/models/question.rb
@@ -19,6 +19,10 @@ class Question &lt; ActiveRecord::Base @@ -19,6 +19,10 @@ class Question &lt; ActiveRecord::Base
19 def item_count 19 def item_count
20 choice_count 20 choice_count
21 end 21 end
  22 +
  23 + def prompt_count
  24 + Prompt.count(:all, :conditions => {:question_id => id})
  25 + end
22 26
23 def choice_count 27 def choice_count
24 Choice.count(:all, :conditions => {:question_id => id}) 28 Choice.count(:all, :conditions => {:question_id => id})
@@ -32,6 +36,12 @@ class Question &lt; ActiveRecord::Base @@ -32,6 +36,12 @@ class Question &lt; ActiveRecord::Base
32 prompts[rand(prompts.count-1)]#Prompt.find(picked_prompt_id) 36 prompts[rand(prompts.count-1)]#Prompt.find(picked_prompt_id)
33 end 37 end
34 38
  39 + def picked_prompt_id
  40 + lambda {@picked ||= prompts(true)[rand(prompts.count-1)].id}.call
  41 +
  42 + lambda { prompts(true)[rand(prompts.count-1)].id }.call
  43 + end
  44 +
35 def left_choice_text(prompt = nil) 45 def left_choice_text(prompt = nil)
36 prompt ||= prompts.first#prompts.pick 46 prompt ||= prompts.first#prompts.pick
37 picked_prompt.left_choice.item.data 47 picked_prompt.left_choice.item.data
config/routes.rb
1 ActionController::Routing::Routes.draw do |map| 1 ActionController::Routing::Routes.draw do |map|
2 map.resources :clicks 2 map.resources :clicks
3 -  
4 - map.resources :prompts  
5 -  
6 - map.resources :items  
7 -  
8 - map.resources :choices  
9 -  
10 - map.resources :visitors  
11 -  
12 - map.resources :questions  
13 - 3 + map.resources :questions, :member => { :activate => :put, :suspend => :put} do |question|
  4 + question.resources :items
  5 + question.resources :prompts, :member => {:vote_left => :post, :vote_right => :post, :skip => :post, :vote => :post},
  6 + :collection => {:single => :get, :index => :get}
  7 + question.resources :choices, :member => { :activate => :put, :suspend => :put, :update_from_abroad => :put }, :collection => {:create_from_abroad => :post}
  8 + end
  9 + map.resources :algorithms
  10 + map.connect "/questions/:question_id/prompts/:id/vote/:index", :controller => 'prompts', :action => 'vote'
  11 +
  12 + map.root :controller => "home"
  13 +
  14 +
  15 +
  16 + map.learn '/learn', :controller => 'home', :action => 'learn'
  17 + map.api '/api', :controller => 'home', :action => 'api'
  18 + map.about '/about', :controller => 'home', :action => 'about'
14 map.root :controller => "clearance/sessions", :action => "new" 19 map.root :controller => "clearance/sessions", :action => "new"
15 20
16 # rake routes 21 # rake routes
db/migrate/20091203195125_add_what_was_clicked_to_clicks.rb 0 → 100644
@@ -0,0 +1,9 @@ @@ -0,0 +1,9 @@
  1 +class AddWhatWasClickedToClicks < ActiveRecord::Migration
  2 + def self.up
  3 + add_column :clicks, :what_was_clicked, :string
  4 + end
  5 +
  6 + def self.down
  7 + remove_column :clicks, :what_was_clicked
  8 + end
  9 +end
spec/controllers/clicks_controller_spec.rb
1 require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') 1 require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2 2
3 describe ClicksController do 3 describe ClicksController do
  4 +
  5 + def sign_in_as(user)
  6 + @controller.current_user = user
  7 + return user
  8 + end
  9 +
  10 + before(:each) do
  11 + sign_in_as(@user = Factory(:email_confirmed_user))
  12 + end
4 13
5 def mock_click(stubs={}) 14 def mock_click(stubs={})
6 @mock_click ||= mock_model(Click, stubs) 15 @mock_click ||= mock_model(Click, stubs)