Commit 439818b83cdb98156b848610b657328de7a4add6
1 parent
6e9fe757
Exists in
master
and in
1 other branch
Get rid of mandatory two choices
Showing
3 changed files
with
41 additions
and
41 deletions
Show diff stats
app/models/choice.rb
@@ -52,7 +52,7 @@ class Choice < ActiveRecord::Base | @@ -52,7 +52,7 @@ class Choice < ActiveRecord::Base | ||
52 | 52 | ||
53 | after_create :generate_prompts | 53 | after_create :generate_prompts |
54 | def before_create | 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 | unless item | 56 | unless item |
57 | @item = Item.create!(:creator => creator, :data => data) | 57 | @item = Item.create!(:creator => creator, :data => data) |
58 | self.item = @item | 58 | self.item = @item |
@@ -61,10 +61,10 @@ class Choice < ActiveRecord::Base | @@ -61,10 +61,10 @@ class Choice < ActiveRecord::Base | ||
61 | self.score = 50.0 | 61 | self.score = 50.0 |
62 | end | 62 | end |
63 | unless self.active? | 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 | self.active = question.should_autoactivate_ideas? ? true : false | 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 | end | 68 | end |
69 | return true #so active record will save | 69 | return true #so active record will save |
70 | end | 70 | end |
app/models/question.rb
@@ -20,10 +20,17 @@ class Question < ActiveRecord::Base | @@ -20,10 +20,17 @@ class Question < ActiveRecord::Base | ||
20 | has_many :skips | 20 | has_many :skips |
21 | has_many :densities | 21 | has_many :densities |
22 | has_many :appearances | 22 | has_many :appearances |
23 | - | ||
24 | - #comment out to run bt import script! | ||
25 | - after_save :ensure_at_least_two_choices | 23 | + |
26 | attr_accessor :ideas | 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 | def item_count | 35 | def item_count |
29 | choices.size | 36 | choices.size |
@@ -284,19 +291,6 @@ class Question < ActiveRecord::Base | @@ -284,19 +291,6 @@ class Question < ActiveRecord::Base | ||
284 | validates_presence_of :site, :on => :create, :message => "can't be blank" | 291 | validates_presence_of :site, :on => :create, :message => "can't be blank" |
285 | validates_presence_of :creator, :on => :create, :message => "can't be blank" | 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 | def density | 294 | def density |
301 | # slow code, only to be run by cron job once at night | 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,7 +13,7 @@ describe Question do | ||
13 | it {should validate_presence_of :creator} | 13 | it {should validate_presence_of :creator} |
14 | 14 | ||
15 | before(:each) do | 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 | @valid_attributes = { | 17 | @valid_attributes = { |
18 | :site => @aoi_clone, | 18 | :site => @aoi_clone, |
19 | :creator => @aoi_clone.default_visitor | 19 | :creator => @aoi_clone.default_visitor |
@@ -21,44 +21,49 @@ describe Question do | @@ -21,44 +21,49 @@ describe Question do | ||
21 | } | 21 | } |
22 | 22 | ||
23 | @question = @aoi_clone.create_question("foobarbaz", {:name => 'foo'}) | 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 | end | 34 | end |
35 | + | ||
36 | + it "should have 2 active choices" do | ||
37 | + @question.choices.active.reload.size.should == 2 | ||
38 | + end | ||
26 | 39 | ||
27 | it "should create a new instance given valid attributes" do | 40 | it "should create a new instance given valid attributes" do |
28 | Question.create!(@valid_attributes) | 41 | Question.create!(@valid_attributes) |
29 | end | 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 | q = @aoi_clone.create_question("foobarbaz", {:name => 'foo'}) | 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 | end | 47 | end |
39 | 48 | ||
40 | it "should generate prompts after choices are added" do | 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 | end | 51 | end |
44 | 52 | ||
45 | it "should choose an active prompt randomly" do | 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 | prompt.active?.should == true | 55 | prompt.active?.should == true |
49 | end | 56 | end |
50 | 57 | ||
51 | it "should choose an active prompt using catchup algorithm" do | 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 | prompt.active?.should == true | 60 | prompt.active?.should == true |
55 | end | 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 | end | 68 | end |
64 | 69 | ||
@@ -121,6 +126,7 @@ describe Question do | @@ -121,6 +126,7 @@ describe Question do | ||
121 | 100.times.each do |num| | 126 | 100.times.each do |num| |
122 | user.create_choice("visitor identifier", @catchup_q, {:data => num.to_s, :local_identifier => "exmaple"}) | 127 | user.create_choice("visitor identifier", @catchup_q, {:data => num.to_s, :local_identifier => "exmaple"}) |
123 | end | 128 | end |
129 | + @catchup_q.reload | ||
124 | end | 130 | end |
125 | 131 | ||
126 | 132 | ||
@@ -131,11 +137,11 @@ describe Question do | @@ -131,11 +137,11 @@ describe Question do | ||
131 | 137 | ||
132 | it "should choose an active prompt using catchup algorithm on a large number of choices" do | 138 | it "should choose an active prompt using catchup algorithm on a large number of choices" do |
133 | @catchup_q.reload | 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 | #the catchup algorithm depends on all prompts being generated automatically | 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 | prompt = @catchup_q.catchup_choose_prompt | 146 | prompt = @catchup_q.catchup_choose_prompt |
141 | prompt.active?.should == true | 147 | prompt.active?.should == true |