You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
2.7 KiB

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