1
0

poll.rb 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. #
  20. class Poll < ApplicationRecord
  21. include Expireable
  22. belongs_to :account
  23. belongs_to :status
  24. has_many :votes, class_name: 'PollVote', inverse_of: :poll, dependent: :destroy
  25. has_many :notifications, as: :activity, dependent: :destroy
  26. validates :options, presence: true
  27. validates :expires_at, presence: true, if: :local?
  28. validates_with PollValidator, on: :create, if: :local?
  29. scope :attached, -> { where.not(status_id: nil) }
  30. scope :unattached, -> { where(status_id: nil) }
  31. before_validation :prepare_options
  32. before_validation :prepare_votes_count
  33. after_initialize :prepare_cached_tallies
  34. after_commit :reset_parent_cache, on: :update
  35. def loaded_options
  36. options.map.with_index { |title, key| Option.new(self, key.to_s, title, show_totals_now? ? cached_tallies[key] : nil) }
  37. end
  38. def possibly_stale?
  39. remote? && last_fetched_before_expiration? && time_passed_since_last_fetch?
  40. end
  41. def voted?(account)
  42. account.id == account_id || votes.where(account: account).exists?
  43. end
  44. delegate :local?, to: :account
  45. def remote?
  46. !local?
  47. end
  48. def emojis
  49. @emojis ||= CustomEmoji.from_text(options.join(' '), account.domain)
  50. end
  51. class Option < ActiveModelSerializers::Model
  52. attributes :id, :title, :votes_count, :poll
  53. def initialize(poll, id, title, votes_count)
  54. @poll = poll
  55. @id = id
  56. @title = title
  57. @votes_count = votes_count
  58. end
  59. end
  60. private
  61. def prepare_cached_tallies
  62. self.cached_tallies = options.map { 0 } if cached_tallies.empty?
  63. end
  64. def prepare_votes_count
  65. self.votes_count = cached_tallies.sum unless cached_tallies.empty?
  66. end
  67. def prepare_options
  68. self.options = options.map(&:strip).reject(&:blank?)
  69. end
  70. def reset_parent_cache
  71. return if status_id.nil?
  72. Rails.cache.delete("statuses/#{status_id}")
  73. end
  74. def last_fetched_before_expiration?
  75. last_fetched_at.nil? || expires_at.nil? || last_fetched_at < expires_at
  76. end
  77. def time_passed_since_last_fetch?
  78. last_fetched_at.nil? || last_fetched_at < 1.minute.ago
  79. end
  80. def show_totals_now?
  81. expired? || !hide_totals?
  82. end
  83. end