From 38c7e0fcef808dd6efdd00c5c9660de73c526b91 Mon Sep 17 00:00:00 2001 From: Coleman Date: Fri, 17 Oct 2008 17:03:28 -0500 Subject: [PATCH] anonymous user enhancements --- app/controllers/votes.rb | 8 +++++--- app/models/vote.rb | 22 ++++++---------------- 2 files changed, 11 insertions(+), 19 deletions(-) diff --git a/app/controllers/votes.rb b/app/controllers/votes.rb index 1ad2891..a1c0522 100644 --- a/app/controllers/votes.rb +++ b/app/controllers/votes.rb @@ -27,7 +27,7 @@ class Votes < Application @photo = Photo.find params[:photo_id] rescue nil @vote = Vote.new :photo_id => (@photo.id rescue true), :vote => (params[:one].to_s == 'true') if logged_in? - @vote.user = current_user + @vote.user_id = current_user.id else @vote.session_id = current_user end @@ -37,7 +37,7 @@ class Votes < Application else emsg = "The vote failed: " @vote.errors.each_full { |e| emsg += e + ' ' } - render emsg, :status => 401 + render emsg, :status => 401, :layout => false end end @@ -47,6 +47,8 @@ class Votes < Application # just a simple check to not allow you to vote on a photo twice. model # business logic will fail this, too, but just to make sure you don't... @photo = Photo.find params[:photo_id] if params[:photo_id] - @photo = Photo.next_available_votable_photo current_user if @photo.nil? or (logged_in? and current_user.voted_for?(@photo)) + if @photo.nil? or Vote.voted_for?(@photo, current_user) + @photo = Photo.next_available_votable_photo current_user + end end end diff --git a/app/models/vote.rb b/app/models/vote.rb index 4b10b9b..f244453 100644 --- a/app/models/vote.rb +++ b/app/models/vote.rb @@ -2,8 +2,11 @@ class Vote < ActiveRecord::Base belongs_to :photo belongs_to :user - validate :unique_for_user validates_presence_of :photo_id + validates_uniqueness_of :user_id, :scope => :photo_id + validates_uniqueness_of :session_id, :scope => :photo_id + 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? } attr_protected :user_id attr_protected :session_id @@ -46,7 +49,7 @@ class Vote < ActiveRecord::Base def self.voted_for?(photo, user) c = [ 'photo_id = :pid' ] v = { :pid => (photo.respond_to?('id') ? photo.id : photo) } - if user.respond_to?('id') + if user.respond_to?('user_name') c << 'user_id = :uid' v[:uid] = user.id else @@ -61,7 +64,7 @@ class Vote < ActiveRecord::Base # photo ids. # def self.voted_photo_ids(user) - c = if user.respond_to?('id') + c = if user.respond_to?('user_name') "votes.user_id = #{user.id}" else "votes.session_id = '#{user}'" @@ -71,19 +74,6 @@ class Vote < ActiveRecord::Base protected - ## - # Make sure you can't vote on a photo more than once. - # - 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 - ## # Recalc the stats in the Photo for specific stats don't have to query the # database.