diff --git a/app/controllers/choices_controller.rb b/app/controllers/choices_controller.rb index ec52291..ebcaf8c 100644 --- a/app/controllers/choices_controller.rb +++ b/app/controllers/choices_controller.rb @@ -80,7 +80,7 @@ class ChoicesController < InheritedResources::Base visitor_ideas = Proc.new { |options| options[:builder].tag!('visitor_ideas', visitor.items.count) } format.xml { render :xml => @choice.to_xml(:procs => [saved_choice_id, choice_status, visitor_votes, visitor_ideas]), :status => :ok } - # format.xml { render :xml => @question.picked_prompt.to_xml(:methods => [:left_choice_text, :right_choice_text], :procs => [saved_choice_id, choice_status]), :status => :ok } + # TODO: Why are we rendering a question here? Is the prompt being used later on? format.json { render :json => @question.to_json(:procs => [saved_choice_id, choice_status]), :status => :ok } else format.xml { render :xml => @choice.errors, :status => :unprocessable_entity } @@ -98,7 +98,6 @@ class ChoicesController < InheritedResources::Base respond_to do |format| if @choice.activate! logger.info "successfully activated choice #{@choice.inspect}" - Question.update_counters(@question.id, :inactive_choices_count => -1) format.xml { render :xml => true } format.json { render :json => true } else @@ -121,7 +120,6 @@ class ChoicesController < InheritedResources::Base format.json { render :json => false } elsif @choice.deactivate! logger.info "successfully deactivated choice #{@choice.inspect}" - Question.update_counters(@question.id, :inactive_choices_count => 1 ) format.xml { render :xml => true } format.json { render :json => true } else @@ -197,7 +195,6 @@ class ChoicesController < InheritedResources::Base respond_to do |format| if @choice.deactivate! flag = Flag.create!(flag_params) - puts "I AM CREATING A FLAG FOR #{@choice.inspect}" format.xml { render :xml => @choice.to_xml, :status => :created } format.json { render :json => @choice.to_json, :status => :created } else diff --git a/app/controllers/prompts_controller.rb b/app/controllers/prompts_controller.rb index 022298a..f527219 100644 --- a/app/controllers/prompts_controller.rb +++ b/app/controllers/prompts_controller.rb @@ -67,9 +67,7 @@ class PromptsController < InheritedResources::Base #@prompt.choices.each(&:compute_score!) respond_to do |format| - if successful - - next_prompt = @question.choose_prompt + if successful && next_prompt = @question.choose_prompt visitor = current_user.visitors.find_or_create_by_identifier(visitor_identifier) @a = current_user.record_appearance(visitor, next_prompt) @@ -111,6 +109,7 @@ class PromptsController < InheritedResources::Base authenticate logger.info "#{current_user.inspect} is skipping." @question = Question.find(params[:question_id]) + @prompt = @question.prompts.find(params[:id]) #, :include => [{ :left_choice => :item }, { :right_choice => :item }]) time_viewed = params['params']['time_viewed'] @@ -125,23 +124,7 @@ class PromptsController < InheritedResources::Base respond_to do |format| - if @skip = current_user.record_skip(visitor_identifier, appearance_lookup, @prompt, time_viewed, :skip_reason => skip_reason) - - if @question.uses_catchup? - logger.info("Question #{@question.id} is using catchup algorithm!") - @next_prompt = @question.pop_prompt_queue - if @next_prompt.nil? - @question.record_prompt_cache_miss - logger.info("Catchup prompt cache miss! Nothing in prompt_queue") - @next_prompt = @question.catchup_choose_prompt - else - @question.record_prompt_cache_hit - end - @question.send_later :add_prompt_to_queue - else - @next_prompt = @question.picked_prompt - end - + if @skip = current_user.record_skip(visitor_identifier, appearance_lookup, @prompt, time_viewed, :skip_reason => skip_reason) && @next_prompt = @question.choose_prompt visitor = current_user.visitors.find_or_create_by_identifier(visitor_identifier) @a = current_user.record_appearance(visitor, @next_prompt) @@ -152,11 +135,11 @@ class PromptsController < InheritedResources::Base visitor_ideas = Proc.new { |options| options[:builder].tag!('visitor_ideas', visitor.items.count) } - format.xml { render :xml => @question.picked_prompt.to_xml(:procs => [appearance_id, visitor_votes, visitor_ideas],:methods => [:left_choice_text, :right_choice_text]), :status => :ok } - format.json { render :json => @question.picked_prompt.to_json, :status => :ok } + format.xml { render :xml => @next_prompt.to_xml(:procs => [appearance_id, visitor_votes, visitor_ideas],:methods => [:left_choice_text, :right_choice_text]), :status => :ok } + format.json { render :json => @next_prompt.to_json, :status => :ok } else - format.xml { render :xml => @skip, :status => :unprocessable_entity } - format.json { render :json => @skip, :status => :unprocessable_entity } + format.xml { render :xml => @prompt.to_xml, :status => :conflict} + format.json { render :json => @prompt.to_xml, :status => :conflict} end end end diff --git a/app/controllers/questions_controller.rb b/app/controllers/questions_controller.rb index a8b8eb7..89682d7 100644 --- a/app/controllers/questions_controller.rb +++ b/app/controllers/questions_controller.rb @@ -60,6 +60,17 @@ class QuestionsController < InheritedResources::Base @p = @question.choose_prompt(:algorithm => params[:algorithm]) + if @p.nil? + # could not find a prompt to choose + # I don't know the best http code for the api to respond, but 409 seems the closes + respond_to do |format| + format.xml { render :xml => @question, :status => :conflict and return} + format.json { render :json => @question, :status => :conflict and return} + end + + end + # @question.create_new_appearance - returns appearance, which we can then get the prompt from. + # At the very least add 'if create_appearance is true, # we sometimes request a question when no prompt is displayed # TODO It would be a good idea to find these places and treat them like barebones if !visitor_identifier.blank? diff --git a/app/models/choice.rb b/app/models/choice.rb index c09e903..08665c9 100644 --- a/app/models/choice.rb +++ b/app/models/choice.rb @@ -1,5 +1,4 @@ class Choice < ActiveRecord::Base - include Activation belongs_to :question, :counter_cache => true belongs_to :item @@ -103,6 +102,22 @@ class Choice < ActiveRecord::Base end + def activate! + (self.active = true) + self.save! + Question.update_counters(self.question_id, :inactive_choices_count => -1) + end + + def suspend! + (self.active = false) + self.save! + end + + def deactivate! + (self.active = false) + self.save! + Question.update_counters(self.question_id, :inactive_choices_count => 1) + end protected diff --git a/app/models/question.rb b/app/models/question.rb index c8cf524..964296e 100644 --- a/app/models/question.rb +++ b/app/models/question.rb @@ -31,6 +31,11 @@ class Question < ActiveRecord::Base def choose_prompt(options = {}) + # if there is one or fewer active choices, we won't be able to find a prompt + if self.choices_count - self.inactive_choices_count <= 1 + return nil + end + if self.uses_catchup? || options[:algorithm] == "catchup" logger.info("Question #{self.id} is using catchup algorithm!") next_prompt = self.pop_prompt_queue diff --git a/lib/activation.rb b/lib/activation.rb deleted file mode 100644 index 40e599c..0000000 --- a/lib/activation.rb +++ /dev/null @@ -1,16 +0,0 @@ -module Activation - def activate! - (self.active = true) - self.save! - end - - def suspend! - (self.active = false) - self.save! - end - - def deactivate! - (self.active = false) - self.save! - end -end \ No newline at end of file -- libgit2 0.21.2