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 2 belongs_to :voter, :class_name => "Visitor", :foreign_key => 'voter_id'
3 3 belongs_to :prompt
4 4 belongs_to :question
5   -
6 5 belongs_to :answerable, :polymorphic => true
7 6  
  7 + default_scope :conditions => {:valid_record => true}
  8 +
8 9 def answered?
9 10 !self.answerable_id.nil?
10 11 end
... ...
app/models/question.rb
... ... @@ -144,13 +144,9 @@ class Question < ActiveRecord::Base
144 144 if params[:with_appearance] && visitor_identifier.present?
145 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 150 @prompt = choose_prompt(:algorithm => params[:algorithm])
155 151 @appearance = current_user.record_appearance(visitor, @prompt)
156 152 else
... ... @@ -163,13 +159,8 @@ class Question < ActiveRecord::Base
163 159 num_future = params[:future_prompts][:number].to_i rescue 1
164 160 num_future.times do |number|
165 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 164 @future_prompt = choose_prompt(:algorithm => params[:algorithm])
174 165 @future_appearance = current_user.record_appearance(visitor, @future_prompt)
175 166 else
... ... @@ -597,6 +588,23 @@ class Question < ActiveRecord::Base
597 588 filename
598 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 610 end
... ...
db/schema.rb
... ... @@ -9,7 +9,7 @@
9 9 #
10 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 14 create_table "appearances", :force => true do |t|
15 15 t.integer "voter_id"
... ... @@ -21,6 +21,8 @@ ActiveRecord::Schema.define(:version => 20100714160406) do
21 21 t.datetime "updated_at"
22 22 t.integer "answerable_id"
23 23 t.string "answerable_type"
  24 + t.boolean "valid_record", :default => true
  25 + t.string "validity_information"
24 26 end
25 27  
26 28 add_index "appearances", ["lookup"], :name => "index_appearances_on_lookup"
... ...