From 3537faabae938257acc652a3f748b56118d1aa28 Mon Sep 17 00:00:00 2001 From: Dhruv Kapadia Date: Wed, 31 Mar 2010 17:51:12 -0400 Subject: [PATCH] Added time_viewed field to vote model --- app/controllers/prompts_controller.rb | 6 ++++-- app/models/user.rb | 10 ++++++---- app/models/visitor.rb | 4 ++-- db/migrate/20100331213516_add_time_viewed_to_votes.rb | 9 +++++++++ 4 files changed, 21 insertions(+), 8 deletions(-) create mode 100644 db/migrate/20100331213516_add_time_viewed_to_votes.rb diff --git a/app/controllers/prompts_controller.rb b/app/controllers/prompts_controller.rb index c260a07..86781eb 100644 --- a/app/controllers/prompts_controller.rb +++ b/app/controllers/prompts_controller.rb @@ -51,12 +51,14 @@ class PromptsController < InheritedResources::Base logger.info "#{current_user.inspect} is voting #{direction} at #{Time.now}." @question = Question.find(params[:question_id]) @prompt = @question.prompts.find(params[:id]) + + time_viewed = params['params']['time_viewed'] case direction when :left - successful = c = current_user.record_vote(params['params']['auto'], @prompt, 0) + successful = c = current_user.record_vote(params['params']['auto'], @prompt, 0, time_viewed) when :right - successful = c = current_user.record_vote(params['params']['auto'], @prompt, 1) + successful = c = current_user.record_vote(params['params']['auto'], @prompt, 1, time_viewed) else raise "need to specify either ':left' or ':right' as a direction" end diff --git a/app/models/user.rb b/app/models/user.rb index 0c955d5..5c53cf0 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -18,6 +18,8 @@ class User < ActiveRecord::Base def create_choice(visitor_identifier, question, choice_params = {}) visitor = visitors.find_or_create_by_identifier(visitor_identifier) raise "Question not found" if question.nil? + + #TODO Does this serve a purpose? if visitor.owns?(question) choice = question.choices.create!(choice_params.merge(:active => false, :creator => visitor)) elsif question.local_identifier == choice_params[:local_identifier] @@ -29,11 +31,11 @@ class User < ActiveRecord::Base return choice end - def record_vote(visitor_identifier, prompt, ordinality) - @click = Click.new(:what_was_clicked => 'on the API level, inside record_vote' + " with prompt id #{prompt.id}, ordinality #{ordinality.to_s}") - @click.save! + def record_vote(visitor_identifier, prompt, ordinality, time_viewed) + #@click = Click.new(:what_was_clicked => 'on the API level, inside record_vote' + " with prompt id #{prompt.id}, ordinality #{ordinality.to_s}") + #@click.save! visitor = visitors.find_or_create_by_identifier(visitor_identifier) - visitor.vote_for!(prompt, ordinality) + visitor.vote_for!(prompt, ordinality, time_viewed) #prompt.choices.each {|c| c.compute_score; c.save!} end diff --git a/app/models/visitor.rb b/app/models/visitor.rb index 12e4db5..49ace8d 100644 --- a/app/models/visitor.rb +++ b/app/models/visitor.rb @@ -15,7 +15,7 @@ class Visitor < ActiveRecord::Base questions.include? question end - def vote_for!(prompt, ordinality) + def vote_for!(prompt, ordinality, time_viewed) choices = prompt.choices choice = choices[ordinality] #we need to guarantee that the choices are in the right order (by position) other_choices = choices - [choice] @@ -24,7 +24,7 @@ class Visitor < ActiveRecord::Base end loser_choice = other_choices.first - votes.create!(:question_id => prompt.question_id, :prompt_id => prompt.id, :voter_id=> self.id, :choice_id => choice.id, :loser_choice_id => loser_choice.id) + votes.create!(:question_id => prompt.question_id, :prompt_id => prompt.id, :voter_id=> self.id, :choice_id => choice.id, :loser_choice_id => loser_choice.id, :time_viewed => time_viewed) # Votes count is a cached value, creating the vote above will increment it in the db, but to get the proper score, we need to increment it in the current object # The updated votes_count object is not saved to the db, so we don't need to worry about double counting # Alternatively, we could just do choice.reload, but that results in another db read diff --git a/db/migrate/20100331213516_add_time_viewed_to_votes.rb b/db/migrate/20100331213516_add_time_viewed_to_votes.rb new file mode 100644 index 0000000..6603fdd --- /dev/null +++ b/db/migrate/20100331213516_add_time_viewed_to_votes.rb @@ -0,0 +1,9 @@ +class AddTimeViewedToVotes < ActiveRecord::Migration + def self.up + add_column :votes, :time_viewed, :integer #msec + end + + def self.down + remove_column :votes, :time_viewed, :integer #msec + end +end -- libgit2 0.21.2