about summary refs log tree commit diff
path: root/app/models
diff options
context:
space:
mode:
authorEugen Rochko <eugen@zeonfederated.com>2019-03-07 22:53:47 +0100
committerGitHub <noreply@github.com>2019-03-07 22:53:47 +0100
commit054bbb3da21b2c76374eb921cba862adb8d5a0b3 (patch)
tree61e4ea5e735631b6e947ca00884588ca184e8ab6 /app/models
parent75cb93676b1dd41d3e47f62466c0c6430691a990 (diff)
Immediately display poll results to poll author (#10187)
* Immediately display poll results to poll author

* Refactor Poll#loaded_options and add Poll#voted? to improve DRYness
Diffstat (limited to 'app/models')
-rw-r--r--app/models/poll.rb14
1 files changed, 9 insertions, 5 deletions
diff --git a/app/models/poll.rb b/app/models/poll.rb
index da2e25e71..14a38026a 100644
--- a/app/models/poll.rb
+++ b/app/models/poll.rb
@@ -41,17 +41,17 @@ class Poll < ApplicationRecord
   after_commit :reset_parent_cache, on: :update
 
   def loaded_options
-    options.map.with_index { |title, key| Option.new(self, key.to_s, title, cached_tallies[key]) }
-  end
-
-  def unloaded_options
-    options.map.with_index { |title, key| Option.new(self, key.to_s, title, nil) }
+    options.map.with_index { |title, key| Option.new(self, key.to_s, title, show_totals_now? ? cached_tallies[key] : nil) }
   end
 
   def possibly_stale?
     remote? && last_fetched_before_expiration? && time_passed_since_last_fetch?
   end
 
+  def voted?(account)
+    account.id == account_id || votes.where(account: account).exists?
+  end
+
   delegate :local?, to: :account
 
   def remote?
@@ -95,4 +95,8 @@ class Poll < ApplicationRecord
   def time_passed_since_last_fetch?
     last_fetched_at.nil? || last_fetched_at < 1.minute.ago
   end
+
+  def show_totals_now?
+    expired? || !hide_totals?
+  end
 end