/
poll_participation.rb
66 lines (52 loc) · 1.49 KB
/
poll_participation.rb
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
56
57
58
59
60
61
62
63
64
65
66
class PollParticipation < ActiveRecord::Base
include Diaspora::Federated::Base
include Diaspora::Guid
include Diaspora::Relayable
belongs_to :poll
belongs_to :poll_answer, counter_cache: :vote_count
belongs_to :author, :class_name => 'Person', :foreign_key => :author_id
xml_attr :diaspora_handle
xml_attr :poll_answer_guid
xml_convention :underscore
validate :not_already_participated
def parent_class
Poll
end
def parent
self.poll
end
def poll_answer_guid
poll_answer.guid
end
def poll_answer_guid= new_poll_answer_guid
self.poll_answer = PollAnswer.where(:guid => new_poll_answer_guid).first
end
def parent= parent
self.poll = parent
end
def diaspora_handle
self.author.diaspora_handle
end
def diaspora_handle= nh
self.author = Person.find_or_fetch_by_identifier(nh)
end
def not_already_participated
return if poll.nil?
other_participations = PollParticipation.where(author_id: self.author.id, poll_id: self.poll.id).to_a-[self]
if other_participations.present?
self.errors.add(:poll, I18n.t("activerecord.errors.models.poll_participation.attributes.poll.already_participated"))
end
end
class Generator < Federated::Generator
def self.federated_class
PollParticipation
end
def initialize(person, target, poll_answer)
@poll_answer = poll_answer
super(person, target)
end
def relayable_options
{:poll => @target.poll, :poll_answer => @poll_answer}
end
end
end