Commit fc64b664bc816e559e0bdab91820e7f393c00c9e

Authored by Dhruv Kapadia
1 parent cfb88803

Removing create_from_abroad from choices controller

app/controllers/choices_controller.rb
@@ -38,43 +38,27 @@ class ChoicesController < InheritedResources::Base @@ -38,43 +38,27 @@ class ChoicesController < InheritedResources::Base
38 show! do |format| 38 show! do |format|
39 format.xml { 39 format.xml {
40 @choice.reload 40 @choice.reload
41 - # @choice.compute_score!  
42 - # @choice.reload  
43 render :xml => @choice.to_xml(:methods => [:item_data, :wins_plus_losses, :question_name])} 41 render :xml => @choice.to_xml(:methods => [:item_data, :wins_plus_losses, :question_name])}
44 format.json { render :json => @choice.to_json(:methods => [:data])} 42 format.json { render :json => @choice.to_json(:methods => [:data])}
45 end 43 end
46 end 44 end
47 -  
48 - def create_from_abroad  
49 - authenticate  
50 - #expire_page :action => :index  
51 - logger.info "inside create_from_abroad"  
52 -  
53 - @question = Question.find params[:question_id]  
54 45
55 - visitor_identifier = params['params']['auto']  
56 - visitor = current_user.visitors.find_or_create_by_identifier(visitor_identifier) 46 + def create
  47 +
  48 + visitor_identifier = params[:choice].delete(:visitor_identifier)
57 49
58 - respond_to do |format|  
59 - if @choice = current_user.create_choice(visitor_identifier, @question, {:data => params['params']['data'],  
60 - :local_identifier => params['params']['local_identifier']})  
61 - saved_choice_id = Proc.new { |options| options[:builder].tag!('saved_choice_id', @choice.id) }  
62 - choice_status = Proc.new { |options|  
63 - the_status = @choice.active? ? 'active' : 'inactive'  
64 - options[:builder].tag!('choice_status', the_status) }  
65 - logger.info "successfully saved the choice #{@choice.inspect}"  
66 -  
67 - visitor_votes = Proc.new { |options| options[:builder].tag!('visitor_votes', visitor.votes.count(:conditions => {:question_id => @question.id})) }  
68 - visitor_ideas = Proc.new { |options| options[:builder].tag!('visitor_ideas', visitor.items.count) }  
69 -  
70 - format.xml { render :xml => @choice.to_xml(:procs => [saved_choice_id, choice_status, visitor_votes, visitor_ideas]), :status => :ok }  
71 - # TODO: Why are we rendering a question here? Is the prompt being used later on?  
72 - format.json { render :json => @question.to_json(:procs => [saved_choice_id, choice_status]), :status => :ok }  
73 - else  
74 - format.xml { render :xml => @choice.errors, :status => :unprocessable_entity }  
75 - format.json { render :json => @choice.errors, :status => :unprocessable_entity }  
76 - end 50 + visitor = current_user.default_visitor
  51 + if visitor_identifier
  52 + visitor = current_user.visitors.find_or_create_by_identifier(visitor_identifier)
77 end 53 end
  54 + params[:choice].merge!(:creator => visitor)
  55 +
  56 + @question = current_user.questions.find(params[:question_id])
  57 + params[:choice].merge!(:question_id => @question.id)
  58 +
  59 +
  60 + @choice = Choice.new(params[:choice])
  61 + create!
78 end 62 end
79 63
80 def flag 64 def flag
app/models/choice.rb
@@ -33,18 +33,16 @@ class Choice < ActiveRecord::Base @@ -33,18 +33,16 @@ class Choice < ActiveRecord::Base
33 def lose! 33 def lose!
34 self.loss_count += 1 rescue (self.loss_count = 1) 34 self.loss_count += 1 rescue (self.loss_count = 1)
35 self.score = compute_score 35 self.score = compute_score
36 - save! 36 + self.save!
37 end 37 end
38 38
39 def win! 39 def win!
40 self.votes_count += 1 rescue (self.votes_count = 1) 40 self.votes_count += 1 rescue (self.votes_count = 1)
41 self.score = compute_score 41 self.score = compute_score
42 - save! 42 + self.save!
43 end 43 end
44 44
45 def wins_plus_losses 45 def wins_plus_losses
46 - #(prompts_on_the_left.collect(&:votes_count).sum + prompts_on_the_right.collect(&:votes_count).sum)  
47 - #Prompt.sum('votes_count', :conditions => "left_choice_id = #{id} OR right_choice_id = #{id}")  
48 wins + losses 46 wins + losses
49 end 47 end
50 48
config/routes.rb
@@ -15,7 +15,7 @@ ActionController::Routing::Routes.draw do |map| @@ -15,7 +15,7 @@ ActionController::Routing::Routes.draw do |map|
15 question.resources :items 15 question.resources :items
16 question.resources :prompts, :member => {:skip => :post, :vote => :post}, 16 question.resources :prompts, :member => {:skip => :post, :vote => :post},
17 :collection => {:single => :get, :index => :get} 17 :collection => {:single => :get, :index => :get}
18 - question.resources :choices, :member => {:flag => :put}, :collection => {:create_from_abroad => :post} 18 + question.resources :choices, :member => {:flag => :put}
19 end 19 end
20 map.resources :algorithms 20 map.resources :algorithms
21 map.connect "/questions/:question_id/prompts/:id/vote/:index", :controller => 'prompts', :action => 'vote' 21 map.connect "/questions/:question_id/prompts/:id/vote/:index", :controller => 'prompts', :action => 'vote'
spec/controllers/choices_controller_spec.rb
@@ -75,6 +75,31 @@ describe ChoicesController do @@ -75,6 +75,31 @@ describe ChoicesController do
75 75
76 assigns[:choice].should == mock_choice 76 assigns[:choice].should == mock_choice
77 end 77 end
  78 +
78 end 79 end
  80 +
  81 + describe "POST create" do
  82 + before(:each) do
  83 + @question = Factory.create(:aoi_question)
  84 + sign_in_as(@user = @question.site)
  85 + end
  86 +
  87 +
  88 + it "creates a choice" do
  89 + post :create, :question_id => @question.id, :choice => {:data => "blahblah"}
  90 + assigns[:choice].should_not be_nil
  91 + assigns[:choice].creator.should == @question.site.default_visitor
  92 + assigns[:choice].should_not be_active
  93 + assigns[:choice].should_not be_active
  94 + end
  95 +
  96 + it "creates a choice with a correct visitor creator" do
  97 + post :create, :question_id => @question.id, :choice => {:data => "blahblah", :visitor_identifier => "new user"}
  98 + assigns[:choice].should_not be_nil
  99 + assigns[:choice].creator.identifier.should == "new user"
  100 + assigns[:choice].should_not be_active
  101 + assigns[:choice].user_created.should == true
  102 + end
  103 + end
79 104
80 end 105 end