2014-03-26 11:09:39 +00:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: votes
|
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
2020-03-18 03:38:17 +00:00
|
|
|
# votable_type :string(255)
|
|
|
|
# poll_id :integer
|
2014-03-26 11:09:39 +00:00
|
|
|
# user_id :integer
|
|
|
|
# votable_id :integer
|
2020-03-18 03:38:17 +00:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# index_votes_on_user_id (user_id)
|
|
|
|
# index_votes_on_votable_id_and_votable_type (votable_id,votable_type)
|
2014-03-26 11:09:39 +00:00
|
|
|
#
|
|
|
|
|
2014-03-23 00:22:25 +00:00
|
|
|
class Vote < ActiveRecord::Base
|
|
|
|
include Extra
|
|
|
|
|
2020-03-16 23:57:47 +00:00
|
|
|
#attr_protected :id, :updated_at, :created_at, :user_id
|
2014-03-23 00:22:25 +00:00
|
|
|
|
|
|
|
validates_uniqueness_of :user_id, :scope => :votable_id
|
|
|
|
validates_presence_of :user_id, :votable_id, :votable_type
|
|
|
|
|
2020-03-26 02:26:30 +00:00
|
|
|
belongs_to :user, :optional => true
|
|
|
|
belongs_to :votable, :polymorphic => true, :optional => true
|
2014-03-23 00:22:25 +00:00
|
|
|
|
|
|
|
after_create :increase_votes
|
|
|
|
after_destroy :decrease_votes
|
|
|
|
|
|
|
|
def increase_votes
|
|
|
|
if votable_type == "Option"
|
|
|
|
votable.poll.increment :votes
|
|
|
|
votable.poll.save
|
|
|
|
end
|
|
|
|
votable.increment :votes
|
|
|
|
votable.save
|
|
|
|
end
|
|
|
|
|
|
|
|
def decrease_votes
|
|
|
|
if votable_type == "Option"
|
|
|
|
votable.poll.decrement :votes
|
|
|
|
votable.poll.save
|
|
|
|
end
|
|
|
|
votable.decrement :votes
|
|
|
|
votable.save
|
|
|
|
end
|
|
|
|
|
|
|
|
def can_create? cuser
|
|
|
|
return false unless cuser
|
|
|
|
if votable_type == "Option"
|
|
|
|
if votable.poll.voted?(cuser)
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
elsif votable_type == "Gatherer" or votable_type == "GatherMap" or votable_type == "GatherServer"
|
2020-03-17 01:03:02 +00:00
|
|
|
return false unless votable.gather.users.exists? cuser.id
|
2014-03-23 00:22:25 +00:00
|
|
|
|
|
|
|
case votable_type
|
|
|
|
when "Gatherer" then
|
|
|
|
return false if votable.gather.status != Gather::STATE_VOTING
|
2020-03-17 01:03:02 +00:00
|
|
|
return false if votable.gather.gatherer_votes.where(user_id: user.id).count > 1
|
2014-03-23 00:22:25 +00:00
|
|
|
when "GatherMap" then
|
|
|
|
return false if votable.gather.status == Gather::STATE_FINISHED
|
2020-03-17 01:03:02 +00:00
|
|
|
return false if votable.gather.map_votes.where(user_id: user.id).count > 1
|
2014-03-23 00:22:25 +00:00
|
|
|
when "GatherServer" then
|
|
|
|
return false if votable.gather.status == Gather::STATE_FINISHED
|
2020-03-17 01:03:02 +00:00
|
|
|
return false if votable.gather.server_votes.where(user_id: user.id).count > 0
|
2014-03-23 00:22:25 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return true
|
|
|
|
end
|
2020-03-18 03:38:17 +00:00
|
|
|
|
|
|
|
def self.params(params, cuser)
|
|
|
|
params.require(:vote).permit(:votable_type, :votable_id, :poll_id, :user_id)
|
|
|
|
end
|
2014-03-23 00:22:25 +00:00
|
|
|
end
|