2014-03-26 11:09:39 +00:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: polls
|
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
|
|
|
# end_date :datetime
|
2020-03-18 03:38:17 +00:00
|
|
|
# question :string(255)
|
|
|
|
# votes :integer default("0"), not null
|
2014-03-26 11:09:39 +00:00
|
|
|
# created_at :datetime
|
|
|
|
# updated_at :datetime
|
2020-03-18 03:38:17 +00:00
|
|
|
# user_id :integer
|
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# index_polls_on_user_id (user_id)
|
2014-03-26 11:09:39 +00:00
|
|
|
#
|
|
|
|
|
2014-03-23 00:22:25 +00:00
|
|
|
class Poll < ActiveRecord::Base
|
|
|
|
include Extra
|
|
|
|
|
2019-06-03 19:38:24 +00:00
|
|
|
default_scope -> { order("created_at DESC") }
|
|
|
|
|
2020-03-16 23:57:47 +00:00
|
|
|
#attr_protected :id, :updated_at, :created_at, :votes, :user_id
|
2014-03-23 00:22:25 +00:00
|
|
|
|
|
|
|
validates_length_of :question, :in => 1..50
|
|
|
|
#validates_datetime :end_date
|
|
|
|
|
2020-03-26 02:26:30 +00:00
|
|
|
belongs_to :user, :optional => true
|
2014-03-23 00:22:25 +00:00
|
|
|
has_many :options, :class_name => "Option", :dependent => :destroy
|
|
|
|
has_many :real_votes, :through => :options
|
|
|
|
|
|
|
|
accepts_nested_attributes_for :options, :allow_destroy => true
|
|
|
|
|
|
|
|
def voted? user
|
2020-03-17 01:03:02 +00:00
|
|
|
real_votes.where(user_id: user.id).count > 0
|
2014-03-23 00:22:25 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def can_create? cuser
|
|
|
|
cuser and cuser.admin?
|
|
|
|
end
|
|
|
|
|
|
|
|
def can_update? cuser
|
|
|
|
cuser and cuser.admin?
|
|
|
|
end
|
|
|
|
|
|
|
|
def can_destroy? cuser
|
|
|
|
cuser and cuser.admin?
|
|
|
|
end
|
2020-03-18 03:38:17 +00:00
|
|
|
|
|
|
|
def self.params(params, cuser)
|
|
|
|
params.require(:poll).permit(:end_date, :question)
|
|
|
|
end
|
2014-03-23 00:22:25 +00:00
|
|
|
end
|