2008-10-01 02:19:35 -04:00
|
|
|
class Vote < ActiveRecord::Base
|
|
|
|
belongs_to :photo
|
|
|
|
belongs_to :user
|
|
|
|
|
2008-10-11 04:22:58 -04:00
|
|
|
validates_presence_of :photo_id
|
2008-11-22 21:43:39 -05:00
|
|
|
validate :photo_id_cannot_be_zero
|
2008-11-23 02:42:00 -05:00
|
|
|
validates_uniqueness_of :user_id, :scope => :photo_id, :if => lambda { |x| x.session_id.to_s.empty? }
|
|
|
|
validates_uniqueness_of :session_id, :scope => :photo_id, :if => lambda { |x| !x.session_id.to_s.empty? }
|
2008-10-17 18:03:28 -04:00
|
|
|
validates_presence_of :session_id, :if => lambda { |x| x.user_id.to_s.empty? }
|
|
|
|
validates_presence_of :user_id, :if => lambda { |x| x.session_id.to_s.empty? }
|
2008-10-11 04:22:58 -04:00
|
|
|
|
|
|
|
attr_protected :user_id
|
|
|
|
attr_protected :session_id
|
2008-10-01 02:19:35 -04:00
|
|
|
|
2008-10-11 20:16:22 -04:00
|
|
|
after_create :update_photo_stats
|
|
|
|
after_destroy :update_photo_stats
|
|
|
|
|
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
|
2008-10-11 04:22:58 -04:00
|
|
|
|
|
|
|
##
|
|
|
|
# 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) }
|
2008-10-17 18:03:28 -04:00
|
|
|
if user.respond_to?('user_name')
|
2008-10-11 04:22:58 -04:00
|
|
|
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
|
|
|
|
|
|
|
|
##
|
2008-10-11 20:16:22 -04:00
|
|
|
# Does a quick find and collect on the cast votes so you can find all voted
|
|
|
|
# photo ids.
|
|
|
|
#
|
2008-10-11 04:22:58 -04:00
|
|
|
def self.voted_photo_ids(user)
|
2008-10-17 18:03:28 -04:00
|
|
|
c = if user.respond_to?('user_name')
|
2008-10-11 04:22:58 -04:00
|
|
|
"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
|
|
|
|
|
2008-11-22 21:43:39 -05:00
|
|
|
##
|
|
|
|
# Checks to make sure the photo_id is not zero. There will never be a zero
|
|
|
|
# id of a Photo.
|
|
|
|
#
|
|
|
|
def photo_id_cannot_be_zero
|
|
|
|
self.errors.add :photo_id, 'cannot be zero' if self.photo_id.to_i == 0
|
|
|
|
end
|
|
|
|
|
2008-10-11 20:16:22 -04:00
|
|
|
##
|
|
|
|
# Recalc the stats in the Photo for specific stats don't have to query the
|
|
|
|
# database.
|
|
|
|
#
|
|
|
|
def update_photo_stats
|
|
|
|
v = if self.frozen?
|
|
|
|
-1
|
|
|
|
else
|
|
|
|
1
|
|
|
|
end
|
|
|
|
self.photo.votes_count += v
|
|
|
|
if self.zero?
|
|
|
|
self.photo.zero_votes += v
|
|
|
|
else
|
|
|
|
self.photo.one_votes += v
|
|
|
|
end
|
|
|
|
self.photo.save
|
|
|
|
end
|
2008-10-01 02:19:35 -04:00
|
|
|
end
|