From fc64b664bc816e559e0bdab91820e7f393c00c9e Mon Sep 17 00:00:00 2001 From: Dhruv Kapadia Date: Tue, 8 Jun 2010 11:45:43 -0400 Subject: [PATCH] Removing create_from_abroad from choices controller --- app/controllers/choices_controller.rb | 44 ++++++++++++++------------------------------ app/models/choice.rb | 6 ++---- config/routes.rb | 2 +- spec/controllers/choices_controller_spec.rb | 25 +++++++++++++++++++++++++ 4 files changed, 42 insertions(+), 35 deletions(-) diff --git a/app/controllers/choices_controller.rb b/app/controllers/choices_controller.rb index e779e3e..dc0fa90 100644 --- a/app/controllers/choices_controller.rb +++ b/app/controllers/choices_controller.rb @@ -38,43 +38,27 @@ class ChoicesController < InheritedResources::Base 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 create_from_abroad - authenticate - #expire_page :action => :index - logger.info "inside create_from_abroad" - - @question = Question.find params[:question_id] - visitor_identifier = params['params']['auto'] - visitor = current_user.visitors.find_or_create_by_identifier(visitor_identifier) + def create + + visitor_identifier = params[:choice].delete(:visitor_identifier) - respond_to do |format| - if @choice = current_user.create_choice(visitor_identifier, @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}" - - visitor_votes = Proc.new { |options| options[:builder].tag!('visitor_votes', visitor.votes.count(:conditions => {:question_id => @question.id})) } - visitor_ideas = Proc.new { |options| options[:builder].tag!('visitor_ideas', visitor.items.count) } - - format.xml { render :xml => @choice.to_xml(:procs => [saved_choice_id, choice_status, visitor_votes, visitor_ideas]), :status => :ok } - # TODO: Why are we rendering a question here? Is the prompt being used later on? - 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 + visitor = current_user.default_visitor + if visitor_identifier + visitor = current_user.visitors.find_or_create_by_identifier(visitor_identifier) end + params[:choice].merge!(:creator => visitor) + + @question = current_user.questions.find(params[:question_id]) + params[:choice].merge!(:question_id => @question.id) + + + @choice = Choice.new(params[:choice]) + create! end def flag diff --git a/app/models/choice.rb b/app/models/choice.rb index bc426e5..30f35ef 100644 --- a/app/models/choice.rb +++ b/app/models/choice.rb @@ -33,18 +33,16 @@ class Choice < ActiveRecord::Base def lose! self.loss_count += 1 rescue (self.loss_count = 1) self.score = compute_score - save! + self.save! end def win! self.votes_count += 1 rescue (self.votes_count = 1) self.score = compute_score - save! + self.save! end def wins_plus_losses - #(prompts_on_the_left.collect(&:votes_count).sum + prompts_on_the_right.collect(&:votes_count).sum) - #Prompt.sum('votes_count', :conditions => "left_choice_id = #{id} OR right_choice_id = #{id}") wins + losses end diff --git a/config/routes.rb b/config/routes.rb index c65272d..279acc9 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -15,7 +15,7 @@ ActionController::Routing::Routes.draw do |map| question.resources :items question.resources :prompts, :member => {:skip => :post, :vote => :post}, :collection => {:single => :get, :index => :get} - question.resources :choices, :member => {:flag => :put}, :collection => {:create_from_abroad => :post} + question.resources :choices, :member => {:flag => :put} end map.resources :algorithms map.connect "/questions/:question_id/prompts/:id/vote/:index", :controller => 'prompts', :action => 'vote' diff --git a/spec/controllers/choices_controller_spec.rb b/spec/controllers/choices_controller_spec.rb index 08e2837..57696ab 100644 --- a/spec/controllers/choices_controller_spec.rb +++ b/spec/controllers/choices_controller_spec.rb @@ -75,6 +75,31 @@ describe ChoicesController do assigns[:choice].should == mock_choice end + end + + describe "POST create" do + before(:each) do + @question = Factory.create(:aoi_question) + sign_in_as(@user = @question.site) + end + + + it "creates a choice" do + post :create, :question_id => @question.id, :choice => {:data => "blahblah"} + assigns[:choice].should_not be_nil + assigns[:choice].creator.should == @question.site.default_visitor + assigns[:choice].should_not be_active + assigns[:choice].should_not be_active + end + + it "creates a choice with a correct visitor creator" do + post :create, :question_id => @question.id, :choice => {:data => "blahblah", :visitor_identifier => "new user"} + assigns[:choice].should_not be_nil + assigns[:choice].creator.identifier.should == "new user" + assigns[:choice].should_not be_active + assigns[:choice].user_created.should == true + end + end end -- libgit2 0.21.2