Commit 635d0638c3b3fc841eaedcc7bd2dc8f604f3502b

Authored by Pius Uzamere
1 parent ce01c571

support for adding choices via the api

app/controllers/choices_controller.rb
1 -class ChoicesController < ApplicationController  
2 - # GET /choices  
3 - # GET /choices.xml  
4 - def index  
5 - @choices = Choice.all 1 +class ChoicesController < InheritedResources::Base
  2 + respond_to :xml, :json
  3 + actions :show, :index, :create, :update
  4 + belongs_to :question
  5 + has_scope :active, :boolean => true, :only => :index
  6 +
  7 +
  8 + def create_from_abroad
  9 + authenticate
  10 + logger.info "inside create_from_abroad"
  11 +
  12 + @question = Question.find params[:question_id]
  13 + # @visitor = Visitor.find_or_create_by_identifier(params['params']['sid'])
  14 + # @item = current_user.items.create({:data => params['params']['data'], :creator => @visitor}
  15 + # @choice = @question.choices.build(:item => @item, :creator => @visitor)
6 16
7 respond_to do |format| 17 respond_to do |format|
8 - format.html # index.html.erb  
9 - format.xml { render :xml => @choices }  
10 - end  
11 - end  
12 -  
13 - # GET /choices/1  
14 - # GET /choices/1.xml  
15 - def show  
16 - @choice = Choice.find(params[:id])  
17 -  
18 - respond_to do |format|  
19 - format.html # show.html.erb  
20 - format.xml { render :xml => @choice }  
21 - end  
22 - end  
23 -  
24 - # GET /choices/new  
25 - # GET /choices/new.xml  
26 - def new  
27 - @choice = Choice.new  
28 -  
29 - respond_to do |format|  
30 - format.html # new.html.erb  
31 - format.xml { render :xml => @choice }  
32 - end  
33 - end  
34 -  
35 - # GET /choices/1/edit  
36 - def edit  
37 - @choice = Choice.find(params[:id])  
38 - end  
39 -  
40 - # POST /choices  
41 - # POST /choices.xml  
42 - def create  
43 - @choice = Choice.new(params[:choice])  
44 -  
45 - respond_to do |format|  
46 - if @choice.save  
47 - flash[:notice] = 'Choice was successfully created.'  
48 - format.html { redirect_to(@choice) }  
49 - format.xml { render :xml => @choice, :status => :created, :location => @choice } 18 + if @choice = current_user.create_choice(params['params']['data'], @question, {:data => params['params']['data']})
  19 + saved_choice_id = Proc.new { |options| options[:builder].tag!('saved_choice_id', @choice.id) }
  20 + logger.info "successfully saved the choice #{@choice.inspect}"
  21 + format.xml { render :xml => @question.picked_prompt.to_xml(:methods => [:left_choice_text, :right_choice_text], :procs => [saved_choice_id]), :status => :ok }
  22 + format.json { render :json => @question.picked_prompt.to_json, :status => :ok }
50 else 23 else
51 - format.html { render :action => "new" }  
52 - format.xml { render :xml => @choice.errors, :status => :unprocessable_entity } 24 + format.xml { render :xml => @choice.errors, :status => :unprocessable_entity }
  25 + format.json { render :json => @choice.errors, :status => :unprocessable_entity }
53 end 26 end
54 end 27 end
55 end 28 end
56 -  
57 - # PUT /choices/1  
58 - # PUT /choices/1.xml  
59 - def update  
60 - @choice = Choice.find(params[:id])  
61 - 29 +
  30 +
  31 + def skip
  32 + voter = User.by_sid(params['params']['auto'])
  33 + logger.info "#{voter.inspect} is skipping."
  34 + @question = Question.find(params[:question_id])
  35 + @prompt = @question.prompts.find(params[:id])
62 respond_to do |format| 36 respond_to do |format|
63 - if @choice.update_attributes(params[:choice])  
64 - flash[:notice] = 'Choice was successfully updated.'  
65 - format.html { redirect_to(@choice) }  
66 - format.xml { head :ok } 37 + if @skip = voter.skip(@prompt)
  38 + format.xml { render :xml => @question.picked_prompt.to_xml(:methods => [:left_choice_text, :right_choice_text]), :status => :ok }
  39 + format.json { render :json => @question.picked_prompt.to_json, :status => :ok }
67 else 40 else
68 - format.html { render :action => "edit" }  
69 - format.xml { render :xml => @choice.errors, :status => :unprocessable_entity } 41 + format.xml { render :xml => c, :status => :unprocessable_entity }
  42 + format.json { render :json => c, :status => :unprocessable_entity }
70 end 43 end
71 end 44 end
72 end 45 end
73 -  
74 - # DELETE /choices/1  
75 - # DELETE /choices/1.xml  
76 - def destroy  
77 - @choice = Choice.find(params[:id])  
78 - @choice.destroy  
79 -  
80 - respond_to do |format|  
81 - format.html { redirect_to(choices_url) }  
82 - format.xml { head :ok }  
83 - end  
84 - end  
85 end 46 end
app/models/user.rb
@@ -3,6 +3,7 @@ class User &lt; ActiveRecord::Base @@ -3,6 +3,7 @@ class User &lt; ActiveRecord::Base
3 has_many :visitors, :class_name => "Visitor", :foreign_key => "site_id" 3 has_many :visitors, :class_name => "Visitor", :foreign_key => "site_id"
4 has_many :questions, :class_name => "Question", :foreign_key => "site_id" 4 has_many :questions, :class_name => "Question", :foreign_key => "site_id"
5 has_many :clicks, :class_name => "Click", :foreign_key => "site_id" 5 has_many :clicks, :class_name => "Click", :foreign_key => "site_id"
  6 + has_many :items, :class_name => "Item", :foreign_key => "site_id"
6 7
7 def default_visitor 8 def default_visitor
8 visitors.find(:first, :conditions => {:identifier => 'owner'}) 9 visitors.find(:first, :conditions => {:identifier => 'owner'})
@@ -17,11 +18,12 @@ class User &lt; ActiveRecord::Base @@ -17,11 +18,12 @@ class User &lt; ActiveRecord::Base
17 visitor = visitors.find_or_create_by_identifier(visitor_identifier) 18 visitor = visitors.find_or_create_by_identifier(visitor_identifier)
18 raise "Question not found" if question.nil? 19 raise "Question not found" if question.nil?
19 if visitor.owns?(question) 20 if visitor.owns?(question)
20 - choice = question.choices.create(choice_params.merge(:active => true)) 21 + choice = question.choices.create!(choice_params.merge(:active => true, :creator => visitor))
21 else 22 else
22 - choice = question.choices.create(choice_params.merge(:active => false)) 23 + choice = question.choices.create!(choice_params.merge(:active => false, :creator => visitor))
23 end 24 end
24 notify_question_owner_that_new_choice_has_been_added(choice) 25 notify_question_owner_that_new_choice_has_been_added(choice)
  26 + return choice
25 end 27 end
26 28
27 def record_vote(visitor_identifier, prompt, ordinality) 29 def record_vote(visitor_identifier, prompt, ordinality)
app/models/visitor.rb
@@ -3,6 +3,7 @@ class Visitor &lt; ActiveRecord::Base @@ -3,6 +3,7 @@ class Visitor &lt; ActiveRecord::Base
3 has_many :questions, :class_name => "Question", :foreign_key => "creator_id" 3 has_many :questions, :class_name => "Question", :foreign_key => "creator_id"
4 has_many :votes, :class_name => "Vote", :foreign_key => "voter_id" 4 has_many :votes, :class_name => "Vote", :foreign_key => "voter_id"
5 has_many :skips, :class_name => "Skip", :foreign_key => "skipper_id" 5 has_many :skips, :class_name => "Skip", :foreign_key => "skipper_id"
  6 + has_many :items, :class_name => "Item", :foreign_key => "creator_id"
6 has_many :clicks 7 has_many :clicks
7 validates_presence_of :site, :on => :create, :message => "can't be blank" 8 validates_presence_of :site, :on => :create, :message => "can't be blank"
8 validates_uniqueness_of :identifier, :on => :create, :message => "must be unique", :scope => :site_id 9 validates_uniqueness_of :identifier, :on => :create, :message => "must be unique", :scope => :site_id