From d90fe8cd2196985384b873e656404a0bef56d929 Mon Sep 17 00:00:00 2001 From: Luke Baker Date: Wed, 19 Jan 2011 15:18:32 -0500 Subject: [PATCH] make updates to simple random choose prompt --- app/models/question.rb | 26 +++++++++++++++++--------- spec/models/question_spec.rb | 21 ++++++++++++++++++--- 2 files changed, 35 insertions(+), 12 deletions(-) diff --git a/app/models/question.rb b/app/models/question.rb index 0b8cf45..d17a6a4 100644 --- a/app/models/question.rb +++ b/app/models/question.rb @@ -57,7 +57,7 @@ class Question < ActiveRecord::Base next_prompt = self.pop_prompt_queue if next_prompt.nil? logger.info("DEBUG Catchup prompt cache miss! Nothing in prompt_queue") - next_prompt = self.picked_prompt + next_prompt = self.simple_random_choose_prompt record_prompt_cache_miss else record_prompt_cache_hit @@ -66,16 +66,16 @@ class Question < ActiveRecord::Base return next_prompt else #Standard choose prompt at random - return self.picked_prompt + return self.simple_random_choose_prompt end end #TODO: generalize for prompts of rank > 2 - def picked_prompt(rank = 2) - logger.info "inside Question#picked_prompt" + def simple_random_choose_prompt(rank = 2) + logger.info "inside Question#simple_random_choose_prompt" raise NotImplementedError.new("Sorry, we currently only support pairwise prompts. Rank of the prompt must be 2.") unless rank == 2 - choice_id_array = distinct_array_of_choice_ids(rank, true) + choice_id_array = distinct_array_of_choice_ids(:rank => rank, :only_active => true) prompts.find_or_create_by_left_choice_id_and_right_choice_id(choice_id_array[0], choice_id_array[1], :include => [:left_choice ,:right_choice ]) end @@ -334,13 +334,20 @@ class Question < ActiveRecord::Base end - def distinct_array_of_choice_ids(rank = 2, only_active = true) + def distinct_array_of_choice_ids(params={}) + params = { + :rank => 2, + :only_active => true + }.merge(params) + rank = params[:rank] + only_active = params[:only_active] count = (only_active) ? choices.active.count : choices.count found_choices = [] + # select only active choices? + conditions = (only_active) ? ['active = ?', true] : ['1=1'] + rank.times do - # select only active choices? - conditions = (only_active) ? ['active = ?', true] : [''] # if we've already found some, make sure we don't find them again if found_choices.count > 0 conditions[0] += ' AND id NOT IN (?)' @@ -350,13 +357,14 @@ class Question < ActiveRecord::Base found_choices.push choices.find(:first, :select => 'id', :conditions => conditions, + # rand generates value >= 0 and < param :offset => rand(count - found_choices.count)).id end return found_choices end def picked_prompt_id - picked_prompt.id + simple_random_choose_prompt.id end def self.voted_on_by(u) diff --git a/spec/models/question_spec.rb b/spec/models/question_spec.rb index 4901ce6..ce79d0c 100644 --- a/spec/models/question_spec.rb +++ b/spec/models/question_spec.rb @@ -37,10 +37,23 @@ describe Question do #end it "should choose an active prompt randomly" do - prompt = @question.picked_prompt + prompt = @question.simple_random_choose_prompt prompt.active?.should == true end + it "should randomly choose two active choices" do + 50.times do + choice_ids = @question.distinct_array_of_choice_ids(:rank => 2, :only_active => true) + choice_ids.count.should == 2 + choice_ids.uniq.count.should == 2 + choice_ids.each do |choice_id| + c = Choice.find(choice_id) + c.active?.should == true + end + end + + end + it "should choose an active prompt using catchup algorithm" do prompt = @question.catchup_choose_prompt prompt.active?.should == true @@ -321,7 +334,8 @@ describe Question do end 200.times.each do |num| - @p = @aoi_question.picked_prompt + @p = @aoi_question.simple_random_choose_prompt + @p.active?.should == true @a = user.record_appearance(visitor, @p) @@ -454,7 +468,8 @@ describe Question do {:data => "foo'bar", :local_identifier => "example creator"}) 40.times.each do |num| - @p = @aoi_question.picked_prompt + @p = @aoi_question.simple_random_choose_prompt + @p.active?.should == true @a = user.record_appearance(visitor, @p) -- libgit2 0.21.2