REFACTOR: rename "total_votes" poll field to the more accurate "voters"

FEATURE: automagically load plugin's migrations
This commit is contained in:
Régis Hanol
2015-05-04 16:01:57 +02:00
parent 8f706f11cb
commit 86d7412f30
13 changed files with 58 additions and 39 deletions

View File

@ -14,15 +14,6 @@ VOTES_CUSTOM_FIELD ||= "polls-votes".freeze
after_initialize do
# PERF, avoids N+1 in serializer
TopicView.add_post_custom_fields_whitelister do |user|
POLLS_CUSTOM_FIELD
end
TopicView.add_post_custom_fields_whitelister do |user|
VOTES_CUSTOM_FIELD
end
# remove "Vote Now!" & "Show Results" links in emails
Email::Styles.register_plugin_style do |fragment|
fragment.css(".poll a.cast-votes, .poll a.toggle-results").each(&:remove)
@ -66,7 +57,7 @@ after_initialize do
vote = votes[poll_name] || []
# increment counters only when the user hasn't casted a vote yet
poll["total_votes"] += 1 if vote.size == 0
poll["voters"] += 1 if vote.size == 0
poll["options"].each do |option|
option["votes"] -= 1 if vote.include?(option["id"])
@ -125,7 +116,7 @@ after_initialize do
# extract polls
parsed.css("div.poll").each do |p|
poll = { "options" => [], "total_votes" => 0 }
poll = { "options" => [], "voters" => 0 }
# extract attributes
p.attributes.values.each do |attribute|
@ -217,9 +208,10 @@ after_initialize do
# only care when raw has changed!
return unless self.raw_changed?
extracted_polls = DiscoursePoll::Poll::extract(self.raw, self.topic_id)
polls = {}
extracted_polls = DiscoursePoll::Poll::extract(self.raw, self.topic_id)
extracted_polls.each do |poll|
# polls should have a unique name
if polls.has_key?(poll["name"])
@ -297,7 +289,7 @@ after_initialize do
next unless previous_polls.has_key?(poll_name)
next unless polls[poll_name]["options"].size == previous_polls[poll_name]["options"].size
polls[poll_name]["total_votes"] = previous_polls[poll_name]["total_votes"]
polls[poll_name]["voters"] = previous_polls[poll_name]["voters"]
for o in 0...polls[poll_name]["options"].size
polls[poll_name]["options"][o]["votes"] = previous_polls[poll_name]["options"][o]["votes"]
end