Commit ebe6df82a570ba5e221f01ba6e9d30d57dbebd3e

Authored by Pius Uzamere
1 parent d91f45a6

only pick a prompt if both of its choices are active

app/models/choice.rb
... ... @@ -11,6 +11,7 @@ class Choice < ActiveRecord::Base
11 11 has_many :votes, :as => :voteable
12 12 has_many :prompts_on_the_left, :class_name => "Prompt", :foreign_key => "left_choice_id"
13 13 has_many :prompts_on_the_right, :class_name => "Prompt", :foreign_key => "right_choice_id"
  14 + named_scope :active, :conditions => { :active => true }
14 15  
15 16 attr_accessor :data
16 17  
... ...
app/models/prompt.rb
... ... @@ -16,13 +16,13 @@ class Prompt < ActiveRecord::Base
16 16 #named_scope :voted_on_by, :include => :choices, :conditions =>
17 17 #named_scope :voted_on_by, proc {|u| { :conditions => { :methodology => methodology } } }
18 18  
  19 + named_scope :active, :include => [:left_choice, :right_choice], :conditions => { 'left_choice.active' => true, 'right_choice.active' => true }
  20 +
  21 +
19 22 def self.voted_on_by(u)
20 23 select {|z| z.voted_on_by_user?(u)}
21 24 end
22 25  
23   -
24   - named_scope :visible, :include => :category, :conditions => { 'categories.hidden' => false }
25   -
26 26 validates_presence_of :left_choice, :on => :create, :message => "can't be blank"
27 27 validates_presence_of :right_choice, :on => :create, :message => "can't be blank"
28 28  
... ... @@ -45,7 +45,11 @@ class Prompt < ActiveRecord::Base
45 45 def right_choice_id
46 46 right_choice.id
47 47 end
48   -
  48 +
  49 + def active?
  50 + left_choice.active? and right_choice.active?
  51 + end
  52 +
49 53  
50 54 def right_choice_text(prompt = nil)
51 55 right_choice.item.data
... ...
app/models/question.rb
... ... @@ -22,12 +22,15 @@ class Question < ActiveRecord::Base
22 22 end
23 23  
24 24 def picked_prompt
25   - #prompts[rand(prompts.count-1)]#Prompt.find(picked_prompt_id)
26   - Prompt.find(rand(prompts_count-1))
  25 + begin
  26 + return p = Prompt.find(rand(prompts_count-1))
  27 + end until p.active?
27 28 end
28 29  
29 30 def picked_prompt_id
30   - rand(prompts_count-1)
  31 + begin
  32 + return i = rand(prompts_count-1)
  33 + end until Prompt.find(i).active?
31 34 end
32 35  
33 36 def left_choice_text(prompt = nil)
... ...