From 61ec511412dc33793880c3f8a112d775d30023c0 Mon Sep 17 00:00:00 2001 From: Pius Uzamere Date: Thu, 28 Jan 2010 02:02:29 -0500 Subject: [PATCH] slimmed down xml response, added controller support deactivation/activation --- app/controllers/choices_controller.rb | 39 +++++++++++++++++++++++++++++++++++---- app/controllers/questions_controller.rb | 25 +++++++++++++++++-------- 2 files changed, 52 insertions(+), 12 deletions(-) diff --git a/app/controllers/choices_controller.rb b/app/controllers/choices_controller.rb index 103fd37..65c75b9 100644 --- a/app/controllers/choices_controller.rb +++ b/app/controllers/choices_controller.rb @@ -3,21 +3,29 @@ class ChoicesController < InheritedResources::Base actions :show, :index, :create, :update belongs_to :question has_scope :active, :boolean => true, :only => :index + #caches_page :index def index if params[:limit] @question = Question.find(params[:question_id])#, :include => :choices) @question.reload @question.choices.each(&:compute_score!) - @choices = Choice.find(:all, :conditions => {:question_id => @question.id, :active => true}, :limit => params[:limit].to_i, :order => 'score DESC', :include => :item) + unless params[:include_inactive] + @choices = Choice.find(:all, :conditions => {:question_id => @question.id, :active => true}, :limit => params[:limit].to_i, :order => 'score DESC', :include => :item) + else + @choices = Choice.find(:all, :conditions => {:question_id => @question.id}, :limit => params[:limit].to_i, :order => 'score DESC', :include => :item) + end else @question = Question.find(params[:question_id], :include => :choices) #eagerloads ALL choices @question.choices.each(&:compute_score!) - @choices = @question.choices(true).active.find(:all, :include => :item) + unless params[:include_inactive] + @choices = @question.choices(true).active.find(:all, :include => :item) + else + @choices = @question.choices(true) + end end index! do |format| - format.xml { render :xml => params[:data].blank? ? @choices.to_xml(:methods => [:item_data, :votes_count]) : @choices.to_xml(:include => [:items], :methods => [:data, :votes_count])} - format.json { render :json => params[:data].blank? ? @choices.to_json : @choices.to_json(:include => [:items]) } + format.xml { render :xml => @choices.to_xml(:only => [ :data, :score, :id ])} end end @@ -45,6 +53,7 @@ class ChoicesController < InheritedResources::Base def create_from_abroad authenticate + expire_page :action => :index logger.info "inside create_from_abroad" @question = Question.find params[:question_id] @@ -69,6 +78,7 @@ class ChoicesController < InheritedResources::Base def update_from_abroad authenticate + expire_page :action => :index @question = current_user.questions.find(params[:question_id]) @choice = @question.choices.find(params[:id]) @@ -85,8 +95,28 @@ class ChoicesController < InheritedResources::Base end end + def deactivate_from_abroad + authenticate + expire_page :action => :index + @question = current_user.questions.find(params[:question_id]) + @choice = @question.choices.find(params[:id]) + + respond_to do |format| + if @choice.deactivate! + logger.info "successfully deactivated choice #{@choice.inspect}" + format.xml { render :xml => true } + format.json { render :json => true } + else + logger.info "failed to deactivate choice #{@choice.inspect}" + format.xml { render :xml => @choice.to_xml(:methods => [:data, :votes_count, :wins_plus_losses])} + format.json { render :json => @choice.to_json(:methods => [:data])} + end + end + end + def activate authenticate + expire_page :action => :index @question = current_user.questions.find(params[:question_id]) @choice = @question.choices.find(params[:id]) respond_to do |format| @@ -103,6 +133,7 @@ class ChoicesController < InheritedResources::Base def suspend authenticate + expire_page :action => :index @question = current_user.questions.find(params[:question_id]) @choice = @question.choices.find(params[:id]) respond_to do |format| diff --git a/app/controllers/questions_controller.rb b/app/controllers/questions_controller.rb index efea8a5..e94e637 100644 --- a/app/controllers/questions_controller.rb +++ b/app/controllers/questions_controller.rb @@ -5,15 +5,24 @@ class QuestionsController < InheritedResources::Base def show @question = Question.find(params[:id]) - @p = @question.picked_prompt - left_choice_text = Proc.new { |options| options[:builder].tag!('left_choice_text', @p.left_choice.item.data) } - right_choice_text = Proc.new { |options| options[:builder].tag!('right_choice_text', @p.right_choice.item.data) } - picked_prompt_id = Proc.new { |options| options[:builder].tag!('picked_prompt_id', @p.id) } - show! do |format| - session['prompts_ids'] ||= [] - format.xml { - render :xml => @question.to_xml(:methods => [:item_count], :procs => [left_choice_text, right_choice_text, picked_prompt_id]) + unless params[:barebones] + @p = @question.picked_prompt + left_choice_text = Proc.new { |options| options[:builder].tag!('left_choice_text', @p.left_choice.item.data) } + right_choice_text = Proc.new { |options| options[:builder].tag!('right_choice_text', @p.right_choice.item.data) } + picked_prompt_id = Proc.new { |options| options[:builder].tag!('picked_prompt_id', @p.id) } + show! do |format| + session['prompts_ids'] ||= [] + format.xml { + render :xml => @question.to_xml(:methods => [:item_count], :procs => [left_choice_text, right_choice_text, picked_prompt_id]) + } + end + else + show! do |format| + session['prompts_ids'] ||= [] + format.xml { + render :xml => @question.to_xml(:methods => [:item_count]) } + end end end -- libgit2 0.21.2