prompt.rb
1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
class Prompt < ActiveRecord::Base
#has_many :choices, :order => 'position DESC'
has_many :skips
has_many :votes
has_many :appearances
belongs_to :question, :counter_cache => true
belongs_to :left_choice, :class_name => "Choice", :foreign_key => "left_choice_id", :counter_cache => :prompts_on_the_left_count
belongs_to :right_choice, :class_name => "Choice", :foreign_key => "right_choice_id", :counter_cache => :prompts_on_the_right_count
validates_presence_of :left_choice, :on => :create, :message => "can't be blank"
validates_presence_of :right_choice, :on => :create, :message => "can't be blank"
named_scope :with_left_choice, lambda { |*args| {:conditions => ["left_choice_id = ?", (args.first.id)]} }
named_scope :with_right_choice, lambda { |*args| {:conditions => ["right_choice_id = ?", (args.first.id)]} }
named_scope :with_choice, lambda { |*args| {:conditions => ["(right_choice_id = ?) OR (left_choice_id = ?)", (args.first.id)]} }
named_scope :with_choice_id, lambda { |*args| {:conditions => ["(right_choice_id = ?) OR (left_choice_id = ?)", (args.first)]} }
#named_scope :voted_on_by, :include => :choices, :conditions =>
#named_scope :voted_on_by, proc {|u| { :conditions => { :methodology => methodology } } }
named_scope :active, :include => [:left_choice, :right_choice], :conditions => { 'left_choice.active' => true, 'right_choice.active' => true }
named_scope :ids_only, :select => 'id'
attr_protected :votes_count, :left_choice_id, :right_choice_id
attr_readonly :question_id
def self.voted_on_by(u)
select {|z| z.voted_on_by_user?(u)}
end
def choices
[left_choice, right_choice]
end
def voted_on_by_user?(u)
u.voted_for?(left_choice) || u.voted_for?(right_choice)
end
def left_choice_text(prompt = nil)
left_choice.data
end
def active?
left_choice.active? and right_choice.active?
end
def right_choice_text(prompt = nil)
right_choice.data
end
end