poll.rb 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. # frozen_string_literal: true
  2. # == Schema Information
  3. #
  4. # Table name: polls
  5. #
  6. # id :bigint(8) not null, primary key
  7. # account_id :bigint(8)
  8. # status_id :bigint(8)
  9. # expires_at :datetime
  10. # options :string default([]), not null, is an Array
  11. # cached_tallies :bigint(8) default([]), not null, is an Array
  12. # multiple :boolean default(FALSE), not null
  13. # hide_totals :boolean default(FALSE), not null
  14. # votes_count :bigint(8) default(0), not null
  15. # last_fetched_at :datetime
  16. # created_at :datetime not null
  17. # updated_at :datetime not null
  18. # lock_version :integer default(0), not null
  19. # voters_count :bigint(8)
  20. #
  21. class Poll < ApplicationRecord
  22. include Expireable
  23. belongs_to :account
  24. belongs_to :status
  25. has_many :votes, class_name: 'PollVote', inverse_of: :poll, dependent: :destroy
  26. has_many :notifications, as: :activity, dependent: :destroy
  27. validates :options, presence: true
  28. validates :expires_at, presence: true, if: :local?
  29. validates_with PollValidator, on: :create, if: :local?
  30. scope :attached, -> { where.not(status_id: nil) }
  31. scope :unattached, -> { where(status_id: nil) }
  32. before_validation :prepare_options, if: :local?
  33. before_validation :prepare_votes_count
  34. after_initialize :prepare_cached_tallies
  35. after_commit :reset_parent_cache, on: :update
  36. def loaded_options
  37. options.map.with_index { |title, key| Option.new(self, key.to_s, title, show_totals_now? ? cached_tallies[key] : nil) }
  38. end
  39. def possibly_stale?
  40. remote? && last_fetched_before_expiration? && time_passed_since_last_fetch?
  41. end
  42. def voted?(account)
  43. account.id == account_id || votes.where(account: account).exists?
  44. end
  45. def own_votes(account)
  46. votes.where(account: account).pluck(:choice)
  47. end
  48. delegate :local?, to: :account
  49. def remote?
  50. !local?
  51. end
  52. def emojis
  53. @emojis ||= CustomEmoji.from_text(options.join(' '), account.domain)
  54. end
  55. class Option < ActiveModelSerializers::Model
  56. attributes :id, :title, :votes_count, :poll
  57. def initialize(poll, id, title, votes_count)
  58. @poll = poll
  59. @id = id
  60. @title = title
  61. @votes_count = votes_count
  62. end
  63. end
  64. private
  65. def prepare_cached_tallies
  66. self.cached_tallies = options.map { 0 } if cached_tallies.empty?
  67. end
  68. def prepare_votes_count
  69. self.votes_count = cached_tallies.sum unless cached_tallies.empty?
  70. end
  71. def prepare_options
  72. self.options = options.map(&:strip).reject(&:blank?)
  73. end
  74. def reset_parent_cache
  75. return if status_id.nil?
  76. Rails.cache.delete("statuses/#{status_id}")
  77. end
  78. def last_fetched_before_expiration?
  79. last_fetched_at.nil? || expires_at.nil? || last_fetched_at < expires_at
  80. end
  81. def time_passed_since_last_fetch?
  82. last_fetched_at.nil? || last_fetched_at < 1.minute.ago
  83. end
  84. def show_totals_now?
  85. expired? || !hide_totals?
  86. end
  87. end