Commit 5f28f5b43b3040422c02b0b148acdd7f1b430945

Authored by Dhruv Kapadia
1 parent 94dfcc16

Refactoring preloading code slightly, fixing edge case with deactivated

prompts
app/models/appearance.rb
@@ -2,9 +2,10 @@ class Appearance < ActiveRecord::Base @@ -2,9 +2,10 @@ class Appearance < ActiveRecord::Base
2 belongs_to :voter, :class_name => "Visitor", :foreign_key => 'voter_id' 2 belongs_to :voter, :class_name => "Visitor", :foreign_key => 'voter_id'
3 belongs_to :prompt 3 belongs_to :prompt
4 belongs_to :question 4 belongs_to :question
5 -  
6 belongs_to :answerable, :polymorphic => true 5 belongs_to :answerable, :polymorphic => true
7 6
  7 + default_scope :conditions => {:valid_record => true}
  8 +
8 def answered? 9 def answered?
9 !self.answerable_id.nil? 10 !self.answerable_id.nil?
10 end 11 end
app/models/question.rb
@@ -144,13 +144,9 @@ class Question < ActiveRecord::Base @@ -144,13 +144,9 @@ class Question < ActiveRecord::Base
144 if params[:with_appearance] && visitor_identifier.present? 144 if params[:with_appearance] && visitor_identifier.present?
145 visitor = current_user.visitors.find_or_create_by_identifier(visitor_identifier) 145 visitor = current_user.visitors.find_or_create_by_identifier(visitor_identifier)
146 146
147 - last_appearance = visitor.appearances.find(:first,  
148 - :conditions => {:question_id => self.id,  
149 - :answerable_id => nil  
150 - },  
151 - :order => 'id ASC',  
152 - :limit => 1)  
153 - if last_appearance.nil?|| last_appearance.answered? || !last_appearance.prompt.active? 147 + last_appearance = get_first_unanswered_appearance(visitor)
  148 +
  149 + if last_appearance.nil?
154 @prompt = choose_prompt(:algorithm => params[:algorithm]) 150 @prompt = choose_prompt(:algorithm => params[:algorithm])
155 @appearance = current_user.record_appearance(visitor, @prompt) 151 @appearance = current_user.record_appearance(visitor, @prompt)
156 else 152 else
@@ -163,13 +159,8 @@ class Question < ActiveRecord::Base @@ -163,13 +159,8 @@ class Question < ActiveRecord::Base
163 num_future = params[:future_prompts][:number].to_i rescue 1 159 num_future = params[:future_prompts][:number].to_i rescue 1
164 num_future.times do |number| 160 num_future.times do |number|
165 offset = number + 1 161 offset = number + 1
166 - last_appearance = visitor.appearances.find(:first,  
167 - :conditions => {:question_id => self.id,  
168 - :answerable_id => nil  
169 - },  
170 - :order => 'id ASC',  
171 - :offset => offset)  
172 - if last_appearance.nil?|| last_appearance.answered? || !last_appearance.prompt.active? 162 + last_appearance = get_first_unanswered_appearance(visitor, offset)
  163 + if last_appearance.nil?
173 @future_prompt = choose_prompt(:algorithm => params[:algorithm]) 164 @future_prompt = choose_prompt(:algorithm => params[:algorithm])
174 @future_appearance = current_user.record_appearance(visitor, @future_prompt) 165 @future_appearance = current_user.record_appearance(visitor, @future_prompt)
175 else 166 else
@@ -597,6 +588,23 @@ class Question < ActiveRecord::Base @@ -597,6 +588,23 @@ class Question < ActiveRecord::Base
597 filename 588 filename
598 end 589 end
599 590
  591 + def get_first_unanswered_appearance(visitor, offset=0)
  592 + last_appearance = visitor.appearances.find(:first,
  593 + :conditions => {:question_id => self.id,
  594 + :answerable_id => nil
  595 + },
  596 + :order => 'id ASC',
  597 + :offset => offset)
  598 + if last_appearance && !last_appearance.prompt.active?
  599 + last_appearance.valid_record = false
  600 + last_appearance.validity_information = "Deactivated Prompt"
  601 + last_appearance.save
  602 +
  603 + return get_first_unanswered_appearance(visitor)
  604 + end
  605 + last_appearance
  606 + end
  607 +
600 608
601 609
602 end 610 end
@@ -9,7 +9,7 @@ @@ -9,7 +9,7 @@
9 # 9 #
10 # It's strongly recommended to check this file into your version control system. 10 # It's strongly recommended to check this file into your version control system.
11 11
12 -ActiveRecord::Schema.define(:version => 20100714160406) do 12 +ActiveRecord::Schema.define(:version => 20100716121029) do
13 13
14 create_table "appearances", :force => true do |t| 14 create_table "appearances", :force => true do |t|
15 t.integer "voter_id" 15 t.integer "voter_id"
@@ -21,6 +21,8 @@ ActiveRecord::Schema.define(:version => 20100714160406) do @@ -21,6 +21,8 @@ ActiveRecord::Schema.define(:version => 20100714160406) do
21 t.datetime "updated_at" 21 t.datetime "updated_at"
22 t.integer "answerable_id" 22 t.integer "answerable_id"
23 t.string "answerable_type" 23 t.string "answerable_type"
  24 + t.boolean "valid_record", :default => true
  25 + t.string "validity_information"
24 end 26 end
25 27
26 add_index "appearances", ["lookup"], :name => "index_appearances_on_lookup" 28 add_index "appearances", ["lookup"], :name => "index_appearances_on_lookup"