From ce01c5713fc24753b8318fddd74493465ebdae85 Mon Sep 17 00:00:00 2001 From: Pius Uzamere Date: Thu, 3 Dec 2009 15:49:09 -0500 Subject: [PATCH] clicks are being recorded, as are votes. also question is being shown properly and prompts are being retrieved --- app/controllers/clicks_controller.rb | 4 +++- app/controllers/prompts_controller.rb | 230 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------------------------------------------------- app/models/question.rb | 10 ++++++++++ config/routes.rb | 27 ++++++++++++++++----------- db/migrate/20091203195125_add_what_was_clicked_to_clicks.rb | 9 +++++++++ spec/controllers/clicks_controller_spec.rb | 9 +++++++++ 6 files changed, 214 insertions(+), 75 deletions(-) create mode 100644 db/migrate/20091203195125_add_what_was_clicked_to_clicks.rb diff --git a/app/controllers/clicks_controller.rb b/app/controllers/clicks_controller.rb index 9ddf4a6..4f3562a 100644 --- a/app/controllers/clicks_controller.rb +++ b/app/controllers/clicks_controller.rb @@ -40,7 +40,9 @@ class ClicksController < ApplicationController # POST /clicks # POST /clicks.xml def create - @click = Click.new(params[:click]) + authenticate + p = params[:click].except(:sid).merge(:visitor_id => current_user.visitors.find_or_create_by_identifier(params[:click][:sid]).id) + @click = Click.new(p) respond_to do |format| if @click.save diff --git a/app/controllers/prompts_controller.rb b/app/controllers/prompts_controller.rb index 46ecfd5..a151b0d 100644 --- a/app/controllers/prompts_controller.rb +++ b/app/controllers/prompts_controller.rb @@ -1,85 +1,189 @@ -class PromptsController < ApplicationController - # GET /prompts - # GET /prompts.xml - def index - @prompts = Prompt.all - +class PromptsController < InheritedResources::Base + respond_to :xml, :json + actions :show, :index + belongs_to :question + has_scope :active, :boolean => true, :only => :index + + has_scope :voted_on_by + #before_filter :authenticate + + + def activate + # turning off auth for now: @question = current_user.questions.find(params[:question_id]) + @question = Question.find(params[:question_id]) + @prompt = @question.prompts.find(params[:id]) respond_to do |format| - format.html # index.html.erb - format.xml { render :xml => @prompts } + if @prompt.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 - - # GET /prompts/1 - # GET /prompts/1.xml - def show - @prompt = Prompt.find(params[:id]) - + + def vote + #NOT IMPLEMENTED + @question = Question.find(params[:question_id]) + @prompt = @question.prompts.find(params[:id]) + @choices = @prompt.choices.active + @choice = @choices[params[:index]] respond_to do |format| - format.html # show.html.erb - format.xml { render :xml => @prompt } + format.xml { render :xml => @choice.to_xml } + format.json { render :json => @choice.to_xml } end end - - # GET /prompts/new - # GET /prompts/new.xml - def new - @prompt = Prompt.new - + + def vote_left + vote_direction(:left) + end + + + def vote_right + vote_direction(:right) + end + + + def vote_direction(direction) + authenticate + + logger.info "#{current_user.inspect} is voting #{direction}." + @question = Question.find(params[:question_id]) + @prompt = @question.prompts.find(params[:id]) + case direction + when :left + successful = c = current_user.record_vote(params['params']['auto'], @prompt, 0) + when :right + successful = c = current_user.record_vote(params['params']['auto'], @prompt, 1) + else + raise "need to specify either ':left' or ':right' as a direction" + end + + respond_to do |format| - format.html # new.html.erb - format.xml { render :xml => @prompt } + if successful + format.xml { render :xml => @question.picked_prompt.to_xml(:methods => [:left_choice_text, :right_choice_text, :left_choice_id, :right_choice_id]), :status => :ok } + format.json { render :json => @question.picked_prompt.to_json(:methods => [:left_choice_text, :right_choice_text, :left_choice_id, :right_choice_id]), :status => :ok } + else + format.xml { render :xml => c, :status => :unprocessable_entity } + format.json { render :json => c, :status => :unprocessable_entity } + end end end - - # GET /prompts/1/edit - def edit - @prompt = Prompt.find(params[:id]) - end - - # POST /prompts - # POST /prompts.xml - def create - @prompt = Prompt.new(params[:prompt]) - + + + + + + def suspend + @question = current_user.questions.find(params[:question_id]) + @prompt = @question.prompts.find(params[:id]) respond_to do |format| - if @prompt.save - flash[:notice] = 'Prompt was successfully created.' - format.html { redirect_to(@prompt) } - format.xml { render :xml => @prompt, :status => :created, :location => @prompt } + if @prompt.suspend! + format.xml { render :xml => @choice.to_xml, :status => :created } + format.json { render :json => @choice.to_json, :status => :created } else - format.html { render :action => "new" } - format.xml { render :xml => @prompt.errors, :status => :unprocessable_entity } + format.xml { render :xml => @choice.errors, :status => :unprocessable_entity } + format.json { render :json => @choice.to_json } end end end - - # PUT /prompts/1 - # PUT /prompts/1.xml - def update - @prompt = Prompt.find(params[:id]) - + + + + def skip + voter = User.auto_create_user_object_from_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 @prompt.update_attributes(params[:prompt]) - flash[:notice] = 'Prompt was successfully updated.' - format.html { redirect_to(@prompt) } - format.xml { head :ok } + 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.html { render :action => "edit" } - format.xml { render :xml => @prompt.errors, :status => :unprocessable_entity } + format.xml { render :xml => c, :status => :unprocessable_entity } + format.json { render :json => c, :status => :unprocessable_entity } end end end + - # DELETE /prompts/1 - # DELETE /prompts/1.xml - def destroy - @prompt = Prompt.find(params[:id]) - @prompt.destroy - - respond_to do |format| - format.html { redirect_to(prompts_url) } - format.xml { head :ok } + # GET /prompts + # ==== Return + # Array of length n. Prompts matching parameters + # ==== Options (params) + # question_id:: Converted to integer. Must be greater than 0 and + # belong to the current user. Must belong to user. + # item_ids:: Comma seperated list of items to include. May only + # include commas and digits. Must belong to user. Optional value. + # data:: Flag for whether to include item data. Data included + # if value is not nil. + # ==== Raises + # PermissionError:: If question or any item doesn't belong to current user. + + def index + # turning off auth for now: @question = current_user.questions.find(params[:question_id]) + #authenticate + @question = Question.find(params[:question_id]) + @prompts = @question.prompts + #raise @question.inspect + index! do |format| + if !params[:voter_id].blank? + format.xml { render :xml => User.find(params[:voter_id]).prompts_voted_on.to_xml(:include => [:items, :votes], + :methods => [ :active_items_count, + :all_items_count, + :votes_count ]) } + format.json { render :json => User.find(params[:voter_id]).prompts_voted_on.to_json(:include => [:items, :votes], + :methods => [ :active_items_count, + :all_items_count, + :votes_count ]) } + else + format.xml { render :xml => params[:data].blank? ? + @prompts.to_xml : + @prompts.to_xml(:include => [:items]) + } + format.json { render :json => params[:data].blank? ? @prompts.to_json : @prompts.to_json(:include => [:items]) } + end end end -end + + def show + @question = Question.find(params[:question_id]) + @prompt = @question.prompts.find(params[:id]) + 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 + + 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 index + # index! do |format| + # format.xml { params[:data].blank? ? @prompts.to_xml : @prompts.to_xml(:include => [:items]) } + # format.json { params[:data].blank? ? @prompts.to_json : @prompts.to_json(:include => [:items]) } + # 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 \ No newline at end of file diff --git a/app/models/question.rb b/app/models/question.rb index 74d0ec7..3a96c9f 100644 --- a/app/models/question.rb +++ b/app/models/question.rb @@ -19,6 +19,10 @@ class Question < ActiveRecord::Base def item_count choice_count end + + def prompt_count + Prompt.count(:all, :conditions => {:question_id => id}) + end def choice_count Choice.count(:all, :conditions => {:question_id => id}) @@ -32,6 +36,12 @@ class Question < ActiveRecord::Base prompts[rand(prompts.count-1)]#Prompt.find(picked_prompt_id) end + def picked_prompt_id + lambda {@picked ||= prompts(true)[rand(prompts.count-1)].id}.call + + lambda { prompts(true)[rand(prompts.count-1)].id }.call + end + def left_choice_text(prompt = nil) prompt ||= prompts.first#prompts.pick picked_prompt.left_choice.item.data diff --git a/config/routes.rb b/config/routes.rb index ce351f1..7405914 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,16 +1,21 @@ ActionController::Routing::Routes.draw do |map| map.resources :clicks - - map.resources :prompts - - map.resources :items - - map.resources :choices - - map.resources :visitors - - map.resources :questions - + map.resources :questions, :member => { :activate => :put, :suspend => :put} do |question| + question.resources :items + question.resources :prompts, :member => {:vote_left => :post, :vote_right => :post, :skip => :post, :vote => :post}, + :collection => {:single => :get, :index => :get} + question.resources :choices, :member => { :activate => :put, :suspend => :put, :update_from_abroad => :put }, :collection => {:create_from_abroad => :post} + end + map.resources :algorithms + map.connect "/questions/:question_id/prompts/:id/vote/:index", :controller => 'prompts', :action => 'vote' + + map.root :controller => "home" + + + + map.learn '/learn', :controller => 'home', :action => 'learn' + map.api '/api', :controller => 'home', :action => 'api' + map.about '/about', :controller => 'home', :action => 'about' map.root :controller => "clearance/sessions", :action => "new" # rake routes diff --git a/db/migrate/20091203195125_add_what_was_clicked_to_clicks.rb b/db/migrate/20091203195125_add_what_was_clicked_to_clicks.rb new file mode 100644 index 0000000..22ffc1c --- /dev/null +++ b/db/migrate/20091203195125_add_what_was_clicked_to_clicks.rb @@ -0,0 +1,9 @@ +class AddWhatWasClickedToClicks < ActiveRecord::Migration + def self.up + add_column :clicks, :what_was_clicked, :string + end + + def self.down + remove_column :clicks, :what_was_clicked + end +end diff --git a/spec/controllers/clicks_controller_spec.rb b/spec/controllers/clicks_controller_spec.rb index 48233b4..4f90c0d 100644 --- a/spec/controllers/clicks_controller_spec.rb +++ b/spec/controllers/clicks_controller_spec.rb @@ -1,6 +1,15 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') describe ClicksController do + + def sign_in_as(user) + @controller.current_user = user + return user + end + + before(:each) do + sign_in_as(@user = Factory(:email_confirmed_user)) + end def mock_click(stubs={}) @mock_click ||= mock_model(Click, stubs) -- libgit2 0.21.2