Commit 439818b83cdb98156b848610b657328de7a4add6

Authored by Dhruv Kapadia
1 parent 6e9fe757

Get rid of mandatory two choices

app/models/choice.rb
... ... @@ -52,7 +52,7 @@ class Choice < ActiveRecord::Base
52 52  
53 53 after_create :generate_prompts
54 54 def before_create
55   - puts "just got inside choice#before_create. is set to active? #{self.active?}"
  55 + #puts "just got inside choice#before_create. is set to active? #{self.active?}"
56 56 unless item
57 57 @item = Item.create!(:creator => creator, :data => data)
58 58 self.item = @item
... ... @@ -61,10 +61,10 @@ class Choice < ActiveRecord::Base
61 61 self.score = 50.0
62 62 end
63 63 unless self.active?
64   - puts "this choice was not specifically set to active, so we are now asking if we should auto-activate"
  64 + #puts "this choice was not specifically set to active, so we are now asking if we should auto-activate"
65 65 self.active = question.should_autoactivate_ideas? ? true : false
66   - puts "should question autoactivate? #{question.should_autoactivate_ideas?}"
67   - puts "will this choice be active? #{self.active}"
  66 + #puts "should question autoactivate? #{question.should_autoactivate_ideas?}"
  67 + #puts "will this choice be active? #{self.active}"
68 68 end
69 69 return true #so active record will save
70 70 end
... ...
app/models/question.rb
... ... @@ -20,10 +20,17 @@ class Question < ActiveRecord::Base
20 20 has_many :skips
21 21 has_many :densities
22 22 has_many :appearances
23   -
24   - #comment out to run bt import script!
25   - after_save :ensure_at_least_two_choices
  23 +
26 24 attr_accessor :ideas
  25 + after_create :create_choices_from_ideas
  26 + def create_choices_from_ideas
  27 + if ideas.any?
  28 + ideas.each do |idea|
  29 + item = Item.create!(:data => idea.squish.strip, :creator => self.creator)
  30 + choices.create!(:item => item, :creator => self.creator, :active => true, :data => idea.squish.strip)
  31 + end
  32 + end
  33 + end
27 34  
28 35 def item_count
29 36 choices.size
... ... @@ -284,19 +291,6 @@ class Question < ActiveRecord::Base
284 291 validates_presence_of :site, :on => :create, :message => "can't be blank"
285 292 validates_presence_of :creator, :on => :create, :message => "can't be blank"
286 293  
287   - def ensure_at_least_two_choices
288   - the_ideas = (self.ideas.blank? || self.ideas.empty?) ? ['sample idea 1', 'sample idea 2'] : self.ideas
289   - the_ideas << 'sample choice' if the_ideas.length < 2
290   - if self.choices.empty?
291   - the_ideas.each { |choice_text|
292   - item = Item.create!({:data => choice_text, :creator => creator})
293   - puts item.inspect
294   - choice = choices.create!(:item => item, :creator => creator, :active => true, :data => choice_text)
295   - puts choice.inspect
296   - }
297   - end
298   - end
299   -
300 294 def density
301 295 # slow code, only to be run by cron job once at night
302 296  
... ...
spec/models/question_spec.rb
... ... @@ -13,7 +13,7 @@ describe Question do
13 13 it {should validate_presence_of :creator}
14 14  
15 15 before(:each) do
16   - @aoi_clone = Factory.create(:user, :email => "pius@alum.mit.edu", :password => "password", :password_confirmation => "password", :id => 8)
  16 + @aoi_clone = Factory.create(:user,:password => "password", :password_confirmation => "password", :id => 8)
17 17 @valid_attributes = {
18 18 :site => @aoi_clone,
19 19 :creator => @aoi_clone.default_visitor
... ... @@ -21,44 +21,49 @@ describe Question do
21 21 }
22 22  
23 23 @question = @aoi_clone.create_question("foobarbaz", {:name => 'foo'})
  24 + @question.it_should_autoactivate_ideas = true
  25 + @question.save!
  26 +
  27 + 2.times.each do |num|
  28 + @aoi_clone.create_choice("visitor identifier", @question, {:data => num.to_s, :local_identifier => "example"})
  29 + end
  30 +
  31 + @question.reload
  32 +
24 33  
25 34 end
  35 +
  36 + it "should have 2 active choices" do
  37 + @question.choices.active.reload.size.should == 2
  38 + end
26 39  
27 40 it "should create a new instance given valid attributes" do
28 41 Question.create!(@valid_attributes)
29 42 end
30 43  
31   - it "should be creatable by a user" do
  44 + it "should not create two default choices if none are provided" do
32 45 q = @aoi_clone.create_question("foobarbaz", {:name => 'foo'})
33   - end
34   -
35   - it "should create two default choices if none are provided" do
36   - q = @aoi_clone.create_question("foobarbaz", {:name => 'foo'})
37   - q.choices(true).size.should == 2
  46 + q.choices(true).size.should == 0
38 47 end
39 48  
40 49 it "should generate prompts after choices are added" do
41   - q = @aoi_clone.create_question("foobarbaz", {:name => 'foo'})
42   - q.prompts(true).size.should == 2
  50 + @question.prompts(true).size.should == 2
43 51 end
44 52  
45 53 it "should choose an active prompt randomly" do
46   - q = @aoi_clone.create_question("foobarbaz", {:name => 'foo'})
47   - prompt = q.picked_prompt
  54 + prompt = @question.picked_prompt
48 55 prompt.active?.should == true
49 56 end
50 57  
51 58 it "should choose an active prompt using catchup algorithm" do
52   - q = @aoi_clone.create_question("foobarbaz", {:name => 'foo'})
53   - prompt = q.catchup_choose_prompt
  59 + prompt = @question.catchup_choose_prompt
54 60 prompt.active?.should == true
55 61 end
56 62  
57   - it "should return nil if there is no possible prompt to choose" do
58   - q = @aoi_clone.create_question("foobarbaz", {:name => 'foo'})
59   - q.choices.first.deactivate!
60   - q.reload
61   - q.choose_prompt.should be_nil
  63 + it "should raise runtime exception if there is no possible prompt to choose" do
  64 + @question.choices.first.deactivate!
  65 + @question.reload
  66 + lambda { @question.choose_prompt}.should raise_error(RuntimeError)
62 67  
63 68 end
64 69  
... ... @@ -121,6 +126,7 @@ describe Question do
121 126 100.times.each do |num|
122 127 user.create_choice("visitor identifier", @catchup_q, {:data => num.to_s, :local_identifier => "exmaple"})
123 128 end
  129 + @catchup_q.reload
124 130 end
125 131  
126 132  
... ... @@ -131,11 +137,11 @@ describe Question do
131 137  
132 138 it "should choose an active prompt using catchup algorithm on a large number of choices" do
133 139 @catchup_q.reload
134   - # Sanity check, 2 extra choices are autocreated when empty question created
135   - @catchup_q.choices.size.should == 102
  140 + # Sanity check
  141 + @catchup_q.choices.size.should == 100
136 142  
137 143 #the catchup algorithm depends on all prompts being generated automatically
138   - @catchup_q.prompts.size.should == 102 **2 - 102
  144 + @catchup_q.prompts.size.should == 100 **2 - 100
139 145  
140 146 prompt = @catchup_q.catchup_choose_prompt
141 147 prompt.active?.should == true
... ...