2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-11-19 21:50:00 +08:00
|
|
|
class Poll < ActiveRecord::Base
|
|
|
|
# because we want to use the 'type' column and don't want to use STI
|
|
|
|
self.inheritance_column = nil
|
|
|
|
|
2020-12-22 07:38:59 +08:00
|
|
|
belongs_to :post, -> { with_deleted }
|
2018-11-19 21:50:00 +08:00
|
|
|
|
2019-01-31 00:45:50 +08:00
|
|
|
has_many :poll_options, -> { order(:id) }, dependent: :destroy
|
2018-11-19 21:50:00 +08:00
|
|
|
has_many :poll_votes
|
|
|
|
|
2021-11-26 23:34:07 +08:00
|
|
|
enum type: { regular: 0, multiple: 1, number: 2 }, _scopes: false
|
2023-01-07 04:42:16 +08:00
|
|
|
|
2021-11-26 23:34:07 +08:00
|
|
|
enum status: { open: 0, closed: 1 }, _scopes: false
|
2023-01-07 04:42:16 +08:00
|
|
|
|
2021-11-26 23:34:07 +08:00
|
|
|
enum results: { always: 0, on_vote: 1, on_close: 2, staff_only: 3 }, _scopes: false
|
2023-01-07 04:42:16 +08:00
|
|
|
|
2018-11-19 21:50:00 +08:00
|
|
|
enum visibility: { secret: 0, everyone: 1 }, _scopes: false
|
2023-01-07 04:42:16 +08:00
|
|
|
|
2019-11-26 01:51:01 +08:00
|
|
|
enum chart_type: { bar: 0, pie: 1 }, _scopes: false
|
|
|
|
|
2018-12-31 17:48:30 +08:00
|
|
|
validates :min, numericality: { allow_nil: true, only_integer: true, greater_than_or_equal_to: 0 }
|
2018-11-19 21:50:00 +08:00
|
|
|
validates :max, numericality: { allow_nil: true, only_integer: true, greater_than: 0 }
|
|
|
|
validates :step, numericality: { allow_nil: true, only_integer: true, greater_than: 0 }
|
|
|
|
|
|
|
|
def is_closed?
|
|
|
|
closed? || (close_at && close_at <= Time.zone.now)
|
|
|
|
end
|
|
|
|
|
|
|
|
def can_see_results?(user)
|
2019-08-16 02:27:18 +08:00
|
|
|
return !!user&.staff? if staff_only?
|
2020-03-21 02:29:00 +08:00
|
|
|
!!(always? || (on_vote? && (is_me?(user) || has_voted?(user))) || is_closed?)
|
|
|
|
end
|
|
|
|
|
|
|
|
def is_me?(user)
|
2020-04-28 21:43:09 +08:00
|
|
|
user && post && post.user&.id == user&.id
|
2018-11-19 21:50:00 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def has_voted?(user)
|
2020-09-05 23:14:45 +08:00
|
|
|
user&.id && poll_votes.where(user_id: user.id).exists?
|
2018-11-19 21:50:00 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def can_see_voters?(user)
|
|
|
|
everyone? && can_see_results?(user)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: polls
|
|
|
|
#
|
2019-05-13 23:37:21 +08:00
|
|
|
# id :bigint not null, primary key
|
|
|
|
# post_id :bigint
|
2018-11-19 21:50:00 +08:00
|
|
|
# name :string default("poll"), not null
|
|
|
|
# close_at :datetime
|
|
|
|
# type :integer default("regular"), not null
|
|
|
|
# status :integer default("open"), not null
|
|
|
|
# results :integer default("always"), not null
|
|
|
|
# visibility :integer default("secret"), not null
|
|
|
|
# min :integer
|
|
|
|
# max :integer
|
|
|
|
# step :integer
|
|
|
|
# anonymous_voters :integer
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
2019-11-29 23:49:08 +08:00
|
|
|
# chart_type :integer default("bar"), not null
|
2020-01-28 20:30:04 +08:00
|
|
|
# groups :string
|
2020-10-02 15:21:24 +08:00
|
|
|
# title :string
|
2018-11-19 21:50:00 +08:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# index_polls_on_post_id (post_id)
|
|
|
|
# index_polls_on_post_id_and_name (post_id,name) UNIQUE
|
|
|
|
#
|
2019-05-13 23:37:21 +08:00
|
|
|
# Foreign Keys
|
|
|
|
#
|
|
|
|
# fk_rails_... (post_id => posts.id)
|
|
|
|
#
|