From 635d0638c3b3fc841eaedcc7bd2dc8f604f3502b Mon Sep 17 00:00:00 2001 From: Pius Uzamere Date: Thu, 3 Dec 2009 16:19:59 -0500 Subject: [PATCH] support for adding choices via the api --- app/controllers/choices_controller.rb | 107 ++++++++++++++++++++++++++++++++++------------------------------------------------------------------------- app/models/user.rb | 6 ++++-- app/models/visitor.rb | 1 + 3 files changed, 39 insertions(+), 75 deletions(-) diff --git a/app/controllers/choices_controller.rb b/app/controllers/choices_controller.rb index 3aab8ea..c8220ff 100644 --- a/app/controllers/choices_controller.rb +++ b/app/controllers/choices_controller.rb @@ -1,85 +1,46 @@ -class ChoicesController < ApplicationController - # GET /choices - # GET /choices.xml - def index - @choices = Choice.all +class ChoicesController < InheritedResources::Base + respond_to :xml, :json + actions :show, :index, :create, :update + belongs_to :question + has_scope :active, :boolean => true, :only => :index + + + def create_from_abroad + authenticate + logger.info "inside create_from_abroad" + + @question = Question.find params[:question_id] + # @visitor = Visitor.find_or_create_by_identifier(params['params']['sid']) + # @item = current_user.items.create({:data => params['params']['data'], :creator => @visitor} + # @choice = @question.choices.build(:item => @item, :creator => @visitor) respond_to do |format| - format.html # index.html.erb - format.xml { render :xml => @choices } - end - end - - # GET /choices/1 - # GET /choices/1.xml - def show - @choice = Choice.find(params[:id]) - - respond_to do |format| - format.html # show.html.erb - format.xml { render :xml => @choice } - end - end - - # GET /choices/new - # GET /choices/new.xml - def new - @choice = Choice.new - - respond_to do |format| - format.html # new.html.erb - format.xml { render :xml => @choice } - end - end - - # GET /choices/1/edit - def edit - @choice = Choice.find(params[:id]) - end - - # POST /choices - # POST /choices.xml - def create - @choice = Choice.new(params[:choice]) - - respond_to do |format| - if @choice.save - flash[:notice] = 'Choice was successfully created.' - format.html { redirect_to(@choice) } - format.xml { render :xml => @choice, :status => :created, :location => @choice } + if @choice = current_user.create_choice(params['params']['data'], @question, {:data => params['params']['data']}) + saved_choice_id = Proc.new { |options| options[:builder].tag!('saved_choice_id', @choice.id) } + logger.info "successfully saved the choice #{@choice.inspect}" + format.xml { render :xml => @question.picked_prompt.to_xml(:methods => [:left_choice_text, :right_choice_text], :procs => [saved_choice_id]), :status => :ok } + format.json { render :json => @question.picked_prompt.to_json, :status => :ok } else - format.html { render :action => "new" } - format.xml { render :xml => @choice.errors, :status => :unprocessable_entity } + format.xml { render :xml => @choice.errors, :status => :unprocessable_entity } + format.json { render :json => @choice.errors, :status => :unprocessable_entity } end end end - - # PUT /choices/1 - # PUT /choices/1.xml - def update - @choice = Choice.find(params[:id]) - + + + def skip + voter = User.by_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 @choice.update_attributes(params[:choice]) - flash[:notice] = 'Choice was successfully updated.' - format.html { redirect_to(@choice) } - 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 => @choice.errors, :status => :unprocessable_entity } + format.xml { render :xml => c, :status => :unprocessable_entity } + format.json { render :json => c, :status => :unprocessable_entity } end end end - - # DELETE /choices/1 - # DELETE /choices/1.xml - def destroy - @choice = Choice.find(params[:id]) - @choice.destroy - - respond_to do |format| - format.html { redirect_to(choices_url) } - format.xml { head :ok } - end - end end diff --git a/app/models/user.rb b/app/models/user.rb index 2524583..9701773 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -3,6 +3,7 @@ class User < ActiveRecord::Base has_many :visitors, :class_name => "Visitor", :foreign_key => "site_id" has_many :questions, :class_name => "Question", :foreign_key => "site_id" has_many :clicks, :class_name => "Click", :foreign_key => "site_id" + has_many :items, :class_name => "Item", :foreign_key => "site_id" def default_visitor visitors.find(:first, :conditions => {:identifier => 'owner'}) @@ -17,11 +18,12 @@ class User < ActiveRecord::Base visitor = visitors.find_or_create_by_identifier(visitor_identifier) raise "Question not found" if question.nil? if visitor.owns?(question) - choice = question.choices.create(choice_params.merge(:active => true)) + choice = question.choices.create!(choice_params.merge(:active => true, :creator => visitor)) else - choice = question.choices.create(choice_params.merge(:active => false)) + choice = question.choices.create!(choice_params.merge(:active => false, :creator => visitor)) end notify_question_owner_that_new_choice_has_been_added(choice) + return choice end def record_vote(visitor_identifier, prompt, ordinality) diff --git a/app/models/visitor.rb b/app/models/visitor.rb index 9fcd1f6..bd50353 100644 --- a/app/models/visitor.rb +++ b/app/models/visitor.rb @@ -3,6 +3,7 @@ class Visitor < ActiveRecord::Base has_many :questions, :class_name => "Question", :foreign_key => "creator_id" has_many :votes, :class_name => "Vote", :foreign_key => "voter_id" has_many :skips, :class_name => "Skip", :foreign_key => "skipper_id" + has_many :items, :class_name => "Item", :foreign_key => "creator_id" has_many :clicks validates_presence_of :site, :on => :create, :message => "can't be blank" validates_uniqueness_of :identifier, :on => :create, :message => "must be unique", :scope => :site_id -- libgit2 0.21.2