Unverified Commit 30cf1bb2 authored by azul's avatar azul
Browse files

proper namespace for polls

parent 74e7c1c2
class Possible < ActiveRecord::Base
class Poll::Possible < ActiveRecord::Base
self.table_name = 'possibles'
acts_as_list
belongs_to :poll
......
class RankingPoll < Poll
has_many :votes, foreign_key: :votable_id, class_name: "RankingVote", dependent: :delete_all
class Poll::RankingPoll < Poll
has_many :votes,
foreign_key: :votable_id,
class_name: "RankingVote",
dependent: :delete_all
def vote(user, picks)
votes.by_user(user).delete_all
......
class RankingVote < Vote
validates_numericality_of :value, greater_than_or_equal_to: 0, message: "has to be bigger than 0"
class Poll::RankingVote < Poll::Vote
validates_numericality_of :value,
greater_than_or_equal_to: 0,
message: "has to be bigger than 0"
validates_presence_of :possible, on: :create, message: "can't be blank"
end
\ No newline at end of file
end
class RatingPoll < Poll
has_many :votes, foreign_key: :votable_id, class_name: "RatingVote", dependent: :delete_all
end
\ No newline at end of file
class Poll::RatingPoll < Poll
has_many :votes,
foreign_key: :votable_id,
class_name: "RatingVote",
dependent: :delete_all
end
class RatingVote < Vote
class Poll::RatingVote < Poll::Vote
validates_numericality_of :value, only_integer: true, message: "can only be whole number."
validates_inclusion_of :value, in: -2..2, message: "can only be between -2 and 2."
validates_presence_of :possible, on: :create, message: "can't be blank"
end
\ No newline at end of file
end
class RequestVote < Vote
class Poll::RequestVote < Poll::Vote
REJECT = 0
APPROVE = 1
validates_format_of :votable_type, with: /Request/
validates_inclusion_of :value, in: [REJECT, APPROVE], message: "has to be 0 (reject) or 1 (approve)"
validates_inclusion_of :value,
in: [REJECT, APPROVE],
message: "has to be 0 (reject) or 1 (approve)"
def self.approved
where(value: APPROVE)
......
class Vote < ActiveRecord::Base
class Poll::Vote < ActiveRecord::Base
self.table_name = 'votes'
validates_presence_of :votable_id
belongs_to :possible
belongs_to :possible, class_name: 'Poll::Possible'
belongs_to :user
belongs_to :votable, polymorphic: :true
......
......@@ -57,7 +57,10 @@ class Request < ActiveRecord::Base
# when a period of time has passed
# 'ignore' is another vote that could be use by otherwise non-votable requests
# so that each person has a distinct 'ignore'/'non-ignore' state
has_many :votes, as: :votable, class_name: "RequestVote", dependent: :delete_all
has_many :votes,
as: :votable,
class_name: "Poll::RequestVote",
dependent: :delete_all
validates_presence_of :created_by
validates_presence_of :recipient, if: :recipient_required?
......
......@@ -58,7 +58,9 @@ module User::Pages
# some page data objects belong to users.
# These need has many relationships so they get cleaned up if a user
# is destroyed.
has_many :votes, dependent: :destroy
has_many :votes,
dependent: :destroy,
class_name: 'Poll::Vote'
end
end
......
......@@ -23,7 +23,7 @@ module Crabgrass
config.autoload_paths << "#{Rails.root}/lib"
config.autoload_paths << "#{Rails.root}/app/models"
config.autoload_paths += %w(chat profile poll requests mailers notice).
config.autoload_paths += %w(chat profile requests mailers notice).
collect { |dir| "#{Rails.root}/app/models/#{dir}" }
config.autoload_paths << "#{Rails.root}/app/permissions"
config.autoload_paths << "#{Rails.root}/app/helpers/classes"
......
......@@ -15,7 +15,7 @@ class RankedVotePage < Page
#
def create_poll
unless self.data_id
self.data = RankingPoll.new
self.data = Poll::RankingPoll.new
end
return true # ensure we don't halt on this callback
end
......
......@@ -16,7 +16,7 @@ class RateManyPage < Page
#
def create_poll
unless self.data_id
self.data = RatingPoll.new
self.data = Poll::RatingPoll.new
end
return true # ensure we don't halt on this callback
end
......
......@@ -13,7 +13,7 @@ class PollTest < ActiveSupport::TestCase
def test_vote_destroyed_on_user_destruction
dolphin = users(:dolphin)
poll = RatingPoll.create
poll = Poll::RatingPoll.create
p1 = poll.possibles.create(name: 'smashed')
v1 = poll.votes.create user: dolphin,
possible: p1,
......@@ -25,7 +25,7 @@ class PollTest < ActiveSupport::TestCase
# we might have old votes around where the users do not exist anymore but
# the votes have not been destroyed properly. These should not count.
def test_vote_without_user_not_counted
poll = RankingPoll.create
poll = Poll::RankingPoll.create
p1 = poll.possibles.create(name: 'smashed')
v1 = poll.votes.create user_id: 100,
possible: p1,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment