Commit 61ec511412dc33793880c3f8a112d775d30023c0
1 parent
29e218ba
Exists in
master
and in
1 other branch
slimmed down xml response, added controller support deactivation/activation
Showing
2 changed files
with
52 additions
and
12 deletions
Show diff stats
app/controllers/choices_controller.rb
| @@ -3,21 +3,29 @@ class ChoicesController < InheritedResources::Base | @@ -3,21 +3,29 @@ class ChoicesController < InheritedResources::Base | ||
| 3 | actions :show, :index, :create, :update | 3 | actions :show, :index, :create, :update |
| 4 | belongs_to :question | 4 | belongs_to :question |
| 5 | has_scope :active, :boolean => true, :only => :index | 5 | has_scope :active, :boolean => true, :only => :index |
| 6 | + #caches_page :index | ||
| 6 | 7 | ||
| 7 | def index | 8 | def index |
| 8 | if params[:limit] | 9 | if params[:limit] |
| 9 | @question = Question.find(params[:question_id])#, :include => :choices) | 10 | @question = Question.find(params[:question_id])#, :include => :choices) |
| 10 | @question.reload | 11 | @question.reload |
| 11 | @question.choices.each(&:compute_score!) | 12 | @question.choices.each(&:compute_score!) |
| 12 | - @choices = Choice.find(:all, :conditions => {:question_id => @question.id, :active => true}, :limit => params[:limit].to_i, :order => 'score DESC', :include => :item) | 13 | + unless params[:include_inactive] |
| 14 | + @choices = Choice.find(:all, :conditions => {:question_id => @question.id, :active => true}, :limit => params[:limit].to_i, :order => 'score DESC', :include => :item) | ||
| 15 | + else | ||
| 16 | + @choices = Choice.find(:all, :conditions => {:question_id => @question.id}, :limit => params[:limit].to_i, :order => 'score DESC', :include => :item) | ||
| 17 | + end | ||
| 13 | else | 18 | else |
| 14 | @question = Question.find(params[:question_id], :include => :choices) #eagerloads ALL choices | 19 | @question = Question.find(params[:question_id], :include => :choices) #eagerloads ALL choices |
| 15 | @question.choices.each(&:compute_score!) | 20 | @question.choices.each(&:compute_score!) |
| 16 | - @choices = @question.choices(true).active.find(:all, :include => :item) | 21 | + unless params[:include_inactive] |
| 22 | + @choices = @question.choices(true).active.find(:all, :include => :item) | ||
| 23 | + else | ||
| 24 | + @choices = @question.choices(true) | ||
| 25 | + end | ||
| 17 | end | 26 | end |
| 18 | index! do |format| | 27 | index! do |format| |
| 19 | - format.xml { render :xml => params[:data].blank? ? @choices.to_xml(:methods => [:item_data, :votes_count]) : @choices.to_xml(:include => [:items], :methods => [:data, :votes_count])} | ||
| 20 | - format.json { render :json => params[:data].blank? ? @choices.to_json : @choices.to_json(:include => [:items]) } | 28 | + format.xml { render :xml => @choices.to_xml(:only => [ :data, :score, :id ])} |
| 21 | end | 29 | end |
| 22 | 30 | ||
| 23 | end | 31 | end |
| @@ -45,6 +53,7 @@ class ChoicesController < InheritedResources::Base | @@ -45,6 +53,7 @@ class ChoicesController < InheritedResources::Base | ||
| 45 | 53 | ||
| 46 | def create_from_abroad | 54 | def create_from_abroad |
| 47 | authenticate | 55 | authenticate |
| 56 | + expire_page :action => :index | ||
| 48 | logger.info "inside create_from_abroad" | 57 | logger.info "inside create_from_abroad" |
| 49 | 58 | ||
| 50 | @question = Question.find params[:question_id] | 59 | @question = Question.find params[:question_id] |
| @@ -69,6 +78,7 @@ class ChoicesController < InheritedResources::Base | @@ -69,6 +78,7 @@ class ChoicesController < InheritedResources::Base | ||
| 69 | 78 | ||
| 70 | def update_from_abroad | 79 | def update_from_abroad |
| 71 | authenticate | 80 | authenticate |
| 81 | + expire_page :action => :index | ||
| 72 | @question = current_user.questions.find(params[:question_id]) | 82 | @question = current_user.questions.find(params[:question_id]) |
| 73 | @choice = @question.choices.find(params[:id]) | 83 | @choice = @question.choices.find(params[:id]) |
| 74 | 84 | ||
| @@ -85,8 +95,28 @@ class ChoicesController < InheritedResources::Base | @@ -85,8 +95,28 @@ class ChoicesController < InheritedResources::Base | ||
| 85 | end | 95 | end |
| 86 | end | 96 | end |
| 87 | 97 | ||
| 98 | + def deactivate_from_abroad | ||
| 99 | + authenticate | ||
| 100 | + expire_page :action => :index | ||
| 101 | + @question = current_user.questions.find(params[:question_id]) | ||
| 102 | + @choice = @question.choices.find(params[:id]) | ||
| 103 | + | ||
| 104 | + respond_to do |format| | ||
| 105 | + if @choice.deactivate! | ||
| 106 | + logger.info "successfully deactivated choice #{@choice.inspect}" | ||
| 107 | + format.xml { render :xml => true } | ||
| 108 | + format.json { render :json => true } | ||
| 109 | + else | ||
| 110 | + logger.info "failed to deactivate choice #{@choice.inspect}" | ||
| 111 | + format.xml { render :xml => @choice.to_xml(:methods => [:data, :votes_count, :wins_plus_losses])} | ||
| 112 | + format.json { render :json => @choice.to_json(:methods => [:data])} | ||
| 113 | + end | ||
| 114 | + end | ||
| 115 | + end | ||
| 116 | + | ||
| 88 | def activate | 117 | def activate |
| 89 | authenticate | 118 | authenticate |
| 119 | + expire_page :action => :index | ||
| 90 | @question = current_user.questions.find(params[:question_id]) | 120 | @question = current_user.questions.find(params[:question_id]) |
| 91 | @choice = @question.choices.find(params[:id]) | 121 | @choice = @question.choices.find(params[:id]) |
| 92 | respond_to do |format| | 122 | respond_to do |format| |
| @@ -103,6 +133,7 @@ class ChoicesController < InheritedResources::Base | @@ -103,6 +133,7 @@ class ChoicesController < InheritedResources::Base | ||
| 103 | 133 | ||
| 104 | def suspend | 134 | def suspend |
| 105 | authenticate | 135 | authenticate |
| 136 | + expire_page :action => :index | ||
| 106 | @question = current_user.questions.find(params[:question_id]) | 137 | @question = current_user.questions.find(params[:question_id]) |
| 107 | @choice = @question.choices.find(params[:id]) | 138 | @choice = @question.choices.find(params[:id]) |
| 108 | respond_to do |format| | 139 | respond_to do |format| |
app/controllers/questions_controller.rb
| @@ -5,15 +5,24 @@ class QuestionsController < InheritedResources::Base | @@ -5,15 +5,24 @@ class QuestionsController < InheritedResources::Base | ||
| 5 | 5 | ||
| 6 | def show | 6 | def show |
| 7 | @question = Question.find(params[:id]) | 7 | @question = Question.find(params[:id]) |
| 8 | - @p = @question.picked_prompt | ||
| 9 | - left_choice_text = Proc.new { |options| options[:builder].tag!('left_choice_text', @p.left_choice.item.data) } | ||
| 10 | - right_choice_text = Proc.new { |options| options[:builder].tag!('right_choice_text', @p.right_choice.item.data) } | ||
| 11 | - picked_prompt_id = Proc.new { |options| options[:builder].tag!('picked_prompt_id', @p.id) } | ||
| 12 | - show! do |format| | ||
| 13 | - session['prompts_ids'] ||= [] | ||
| 14 | - format.xml { | ||
| 15 | - render :xml => @question.to_xml(:methods => [:item_count], :procs => [left_choice_text, right_choice_text, picked_prompt_id]) | 8 | + unless params[:barebones] |
| 9 | + @p = @question.picked_prompt | ||
| 10 | + left_choice_text = Proc.new { |options| options[:builder].tag!('left_choice_text', @p.left_choice.item.data) } | ||
| 11 | + right_choice_text = Proc.new { |options| options[:builder].tag!('right_choice_text', @p.right_choice.item.data) } | ||
| 12 | + picked_prompt_id = Proc.new { |options| options[:builder].tag!('picked_prompt_id', @p.id) } | ||
| 13 | + show! do |format| | ||
| 14 | + session['prompts_ids'] ||= [] | ||
| 15 | + format.xml { | ||
| 16 | + render :xml => @question.to_xml(:methods => [:item_count], :procs => [left_choice_text, right_choice_text, picked_prompt_id]) | ||
| 17 | + } | ||
| 18 | + end | ||
| 19 | + else | ||
| 20 | + show! do |format| | ||
| 21 | + session['prompts_ids'] ||= [] | ||
| 22 | + format.xml { | ||
| 23 | + render :xml => @question.to_xml(:methods => [:item_count]) | ||
| 16 | } | 24 | } |
| 25 | + end | ||
| 17 | end | 26 | end |
| 18 | end | 27 | end |
| 19 | 28 |