about summary refs log tree commit diff
path: root/app/models
diff options
context:
space:
mode:
authorThibaut Girka <thib@sitedethib.com>2019-03-08 00:57:13 +0100
committerThibaut Girka <thib@sitedethib.com>2019-03-08 00:57:13 +0100
commit5528719bc9fd4e8dca0e5e51de183a6dd27b4d55 (patch)
tree30e615140e1afbe8d7d8986fe4743d1e4aeb7a03 /app/models
parent38e9c66f34d7054357a3de63834f182734d43d75 (diff)
parent3aaac4f134eb092baeb0ba5979bdb3abd702a4ee (diff)
Merge branch 'master' into glitch-soc/merge-upstream
Diffstat (limited to 'app/models')
-rw-r--r--app/models/poll.rb16
1 files changed, 10 insertions, 6 deletions
diff --git a/app/models/poll.rb b/app/models/poll.rb
index da2e25e71..09f0b65ec 100644
--- a/app/models/poll.rb
+++ b/app/models/poll.rb
@@ -28,7 +28,7 @@ class Poll < ApplicationRecord
 
   validates :options, presence: true
   validates :expires_at, presence: true, if: :local?
-  validates_with PollValidator, if: :local?
+  validates_with PollValidator, on: :create, if: :local?
 
   scope :attached, -> { where.not(status_id: nil) }
   scope :unattached, -> { where(status_id: nil) }
@@ -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