2019-03-03 16:18:23 -05:00
|
|
|
# frozen_string_literal: true
|
2023-02-20 00:58:28 -05:00
|
|
|
|
2019-03-03 16:18:23 -05:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: poll_votes
|
|
|
|
#
|
|
|
|
# id :bigint(8) not null, primary key
|
|
|
|
# account_id :bigint(8)
|
|
|
|
# poll_id :bigint(8)
|
|
|
|
# choice :integer default(0), not null
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
2019-03-04 16:51:23 -05:00
|
|
|
# uri :string
|
2019-03-03 16:18:23 -05:00
|
|
|
#
|
|
|
|
|
|
|
|
class PollVote < ApplicationRecord
|
|
|
|
belongs_to :account
|
|
|
|
belongs_to :poll, inverse_of: :votes
|
|
|
|
|
|
|
|
validates :choice, presence: true
|
|
|
|
validates_with VoteValidator
|
|
|
|
|
|
|
|
after_create_commit :increment_counter_cache
|
|
|
|
|
2019-03-04 16:51:23 -05:00
|
|
|
delegate :local?, to: :account
|
2023-02-27 03:31:15 -05:00
|
|
|
delegate :multiple?, :expired?, to: :poll, prefix: true
|
2019-03-04 16:51:23 -05:00
|
|
|
|
2019-03-04 21:51:18 -05:00
|
|
|
def object_type
|
|
|
|
:vote
|
|
|
|
end
|
|
|
|
|
2019-03-03 16:18:23 -05:00
|
|
|
private
|
|
|
|
|
|
|
|
def increment_counter_cache
|
|
|
|
poll.cached_tallies[choice] = (poll.cached_tallies[choice] || 0) + 1
|
|
|
|
poll.save
|
2019-03-06 13:53:57 -05:00
|
|
|
rescue ActiveRecord::StaleObjectError
|
|
|
|
poll.reload
|
|
|
|
retry
|
2019-03-03 16:18:23 -05:00
|
|
|
end
|
|
|
|
end
|