diff --git a/app/models/choice.rb b/app/models/choice.rb index 4309a75..dee5488 100644 --- a/app/models/choice.rb +++ b/app/models/choice.rb @@ -52,7 +52,7 @@ class Choice < ActiveRecord::Base after_create :generate_prompts def before_create - puts "just got inside choice#before_create. is set to active? #{self.active?}" + #puts "just got inside choice#before_create. is set to active? #{self.active?}" unless item @item = Item.create!(:creator => creator, :data => data) self.item = @item @@ -61,10 +61,10 @@ class Choice < ActiveRecord::Base self.score = 50.0 end unless self.active? - puts "this choice was not specifically set to active, so we are now asking if we should auto-activate" + #puts "this choice was not specifically set to active, so we are now asking if we should auto-activate" self.active = question.should_autoactivate_ideas? ? true : false - puts "should question autoactivate? #{question.should_autoactivate_ideas?}" - puts "will this choice be active? #{self.active}" + #puts "should question autoactivate? #{question.should_autoactivate_ideas?}" + #puts "will this choice be active? #{self.active}" end return true #so active record will save end diff --git a/app/models/question.rb b/app/models/question.rb index a810624..f289792 100644 --- a/app/models/question.rb +++ b/app/models/question.rb @@ -20,10 +20,17 @@ class Question < ActiveRecord::Base has_many :skips has_many :densities has_many :appearances - - #comment out to run bt import script! - after_save :ensure_at_least_two_choices + attr_accessor :ideas + after_create :create_choices_from_ideas + def create_choices_from_ideas + if ideas.any? + ideas.each do |idea| + item = Item.create!(:data => idea.squish.strip, :creator => self.creator) + choices.create!(:item => item, :creator => self.creator, :active => true, :data => idea.squish.strip) + end + end + end def item_count choices.size @@ -284,19 +291,6 @@ class Question < ActiveRecord::Base validates_presence_of :site, :on => :create, :message => "can't be blank" validates_presence_of :creator, :on => :create, :message => "can't be blank" - def ensure_at_least_two_choices - the_ideas = (self.ideas.blank? || self.ideas.empty?) ? ['sample idea 1', 'sample idea 2'] : self.ideas - the_ideas << 'sample choice' if the_ideas.length < 2 - if self.choices.empty? - the_ideas.each { |choice_text| - item = Item.create!({:data => choice_text, :creator => creator}) - puts item.inspect - choice = choices.create!(:item => item, :creator => creator, :active => true, :data => choice_text) - puts choice.inspect - } - end - end - def density # slow code, only to be run by cron job once at night diff --git a/spec/models/question_spec.rb b/spec/models/question_spec.rb index 66ddf2f..0c70d3b 100644 --- a/spec/models/question_spec.rb +++ b/spec/models/question_spec.rb @@ -13,7 +13,7 @@ describe Question do it {should validate_presence_of :creator} before(:each) do - @aoi_clone = Factory.create(:user, :email => "pius@alum.mit.edu", :password => "password", :password_confirmation => "password", :id => 8) + @aoi_clone = Factory.create(:user,:password => "password", :password_confirmation => "password", :id => 8) @valid_attributes = { :site => @aoi_clone, :creator => @aoi_clone.default_visitor @@ -21,44 +21,49 @@ describe Question do } @question = @aoi_clone.create_question("foobarbaz", {:name => 'foo'}) + @question.it_should_autoactivate_ideas = true + @question.save! + + 2.times.each do |num| + @aoi_clone.create_choice("visitor identifier", @question, {:data => num.to_s, :local_identifier => "example"}) + end + + @question.reload + end + + it "should have 2 active choices" do + @question.choices.active.reload.size.should == 2 + end it "should create a new instance given valid attributes" do Question.create!(@valid_attributes) end - it "should be creatable by a user" do + it "should not create two default choices if none are provided" do q = @aoi_clone.create_question("foobarbaz", {:name => 'foo'}) - end - - it "should create two default choices if none are provided" do - q = @aoi_clone.create_question("foobarbaz", {:name => 'foo'}) - q.choices(true).size.should == 2 + q.choices(true).size.should == 0 end it "should generate prompts after choices are added" do - q = @aoi_clone.create_question("foobarbaz", {:name => 'foo'}) - q.prompts(true).size.should == 2 + @question.prompts(true).size.should == 2 end it "should choose an active prompt randomly" do - q = @aoi_clone.create_question("foobarbaz", {:name => 'foo'}) - prompt = q.picked_prompt + prompt = @question.picked_prompt prompt.active?.should == true end it "should choose an active prompt using catchup algorithm" do - q = @aoi_clone.create_question("foobarbaz", {:name => 'foo'}) - prompt = q.catchup_choose_prompt + prompt = @question.catchup_choose_prompt prompt.active?.should == true end - it "should return nil if there is no possible prompt to choose" do - q = @aoi_clone.create_question("foobarbaz", {:name => 'foo'}) - q.choices.first.deactivate! - q.reload - q.choose_prompt.should be_nil + it "should raise runtime exception if there is no possible prompt to choose" do + @question.choices.first.deactivate! + @question.reload + lambda { @question.choose_prompt}.should raise_error(RuntimeError) end @@ -121,6 +126,7 @@ describe Question do 100.times.each do |num| user.create_choice("visitor identifier", @catchup_q, {:data => num.to_s, :local_identifier => "exmaple"}) end + @catchup_q.reload end @@ -131,11 +137,11 @@ describe Question do it "should choose an active prompt using catchup algorithm on a large number of choices" do @catchup_q.reload - # Sanity check, 2 extra choices are autocreated when empty question created - @catchup_q.choices.size.should == 102 + # Sanity check + @catchup_q.choices.size.should == 100 #the catchup algorithm depends on all prompts being generated automatically - @catchup_q.prompts.size.should == 102 **2 - 102 + @catchup_q.prompts.size.should == 100 **2 - 100 prompt = @catchup_q.catchup_choose_prompt prompt.active?.should == true -- libgit2 0.21.2