This repository has been archived on 2020-05-27. You can view files and clone it, but cannot push or open issues/pull-requests.
binaryattraction/app/models/vote.rb

79 lines
1.8 KiB
Ruby
Raw Normal View History

2008-10-01 02:19:35 -04:00
class Vote < ActiveRecord::Base
belongs_to :photo
belongs_to :user
validate :unique_for_user
validates_presence_of :photo_id
attr_protected :user_id
attr_protected :session_id
2008-10-01 02:19:35 -04:00
##
# Checks if this vote is anonymous, or not an authenticated User vote.
#
def anonymous?
self.user_id.nil?
end
##
# Convert this Vote to a number.
#
def to_i
self.vote? ? 1 : 0
end
##
# Is this a 'no' vote.
#
def zero?
self.to_i == 0
end
##
# Is this a 'yes' vote.
#
def one?
self.to_i == 1
end
##
# Checks if a user has voted for a Photo. If you pass a User for user it will
# check for an authenticated user, else it will look for an anonymous vote.
#
def self.voted_for?(photo, user)
c = [ 'photo_id = :pid' ]
v = { :pid => (photo.respond_to?('id') ? photo.id : photo) }
if user.respond_to?('id')
c << 'user_id = :uid'
v[:uid] = user.id
else
c << 'session_id = :uid'
v[:uid] = user
end
self.find :first, :conditions => [ c.join(' AND '), v ]
end
##
# Does a quick find and collect on the cast votes so you can find a
def self.voted_photo_ids(user)
c = if user.respond_to?('id')
"votes.user_id = #{user.id}"
else
"votes.session_id = '#{user}'"
end
self.find(:all, :conditions => c, :select => 'photo_id').collect { |v| v.photo_id }
end
protected
def unique_for_user
if self.user.to_s.empty? and self.session_id.empty?
self.errors.add(:vote, 'must have an owner')
elsif self.user and Vote.voted_for?(self.photo, self.user)
self.errors.add(:vote, 'has already been collected for this photo')
elsif self.session_id and Vote.voted_for?(self.photo, self.session_id)
self.errors.add(:anonymous, 'vote has already been collected')
end
end
2008-10-01 02:19:35 -04:00
end