Commit 119bad2239ae82841810b9a84f1c85c686912b8e

Authored by Luke Baker
1 parent 6a94e874

optimize creation of seed ideas

we have some callbacks on choice creation, that we don't need to run
after every single choice when creating them in a batch.  Instead we can
just make sure the last choice in the batch runs the required callbacks.
Showing 2 changed files with 13 additions and 6 deletions   Show diff stats
app/models/choice.rb
@@ -26,17 +26,22 @@ class Choice < ActiveRecord::Base @@ -26,17 +26,22 @@ class Choice < ActiveRecord::Base
26 26
27 attr_protected :prompts_count, :wins, :losses, :score, :prompts_on_the_right_count, :prompts_on_the_left_count 27 attr_protected :prompts_count, :wins, :losses, :score, :prompts_on_the_right_count, :prompts_on_the_left_count
28 attr_readonly :question_id 28 attr_readonly :question_id
  29 + attr_accessor :part_of_batch_create
29 30
30 def update_questions_counter 31 def update_questions_counter
31 - self.question.update_attribute(:inactive_choices_count, self.question.choices.inactive.length) 32 + unless part_of_batch_create
  33 + self.question.update_attribute(:inactive_choices_count, self.question.choices.inactive.length)
  34 + end
32 end 35 end
33 36
34 # if changing a choice to active, we want to regenerate prompts 37 # if changing a choice to active, we want to regenerate prompts
35 def update_prompt_queue 38 def update_prompt_queue
36 - if self.changed.include?('active') && self.active?  
37 - self.question.mark_prompt_queue_for_refill  
38 - if self.question.choices.size - self.question.inactive_choices_count > 1 && self.question.uses_catchup?  
39 - self.question.send_later :add_prompt_to_queue 39 + unless part_of_batch_create
  40 + if self.changed.include?('active') && self.active?
  41 + self.question.mark_prompt_queue_for_refill
  42 + if self.question.choices.size - self.question.inactive_choices_count > 1 && self.question.uses_catchup?
  43 + self.question.send_later :add_prompt_to_queue
  44 + end
40 end 45 end
41 end 46 end
42 end 47 end
app/models/question.rb
@@ -45,7 +45,9 @@ class Question < ActiveRecord::Base @@ -45,7 +45,9 @@ class Question < ActiveRecord::Base
45 def create_choices_from_ideas 45 def create_choices_from_ideas
46 if ideas && ideas.any? 46 if ideas && ideas.any?
47 ideas.each do |idea| 47 ideas.each do |idea|
48 - choices.create!(:creator => self.creator, :active => true, :data => idea.squish.strip) 48 + # all but last is considered part of batch create, so the
  49 + # last one will fire things that only need to be run at the end
  50 + choices.create!(:creator => self.creator, :active => true, :data => idea.squish.strip, :part_of_batch_create => idea != ideas.last, :question => self)
49 end 51 end
50 end 52 end
51 end 53 end