Commit 3537faabae938257acc652a3f748b56118d1aa28

Authored by Dhruv Kapadia
1 parent 8da4bfb4

Added time_viewed field to vote model

app/controllers/prompts_controller.rb
@@ -51,12 +51,14 @@ class PromptsController < InheritedResources::Base @@ -51,12 +51,14 @@ class PromptsController < InheritedResources::Base
51 logger.info "#{current_user.inspect} is voting #{direction} at #{Time.now}." 51 logger.info "#{current_user.inspect} is voting #{direction} at #{Time.now}."
52 @question = Question.find(params[:question_id]) 52 @question = Question.find(params[:question_id])
53 @prompt = @question.prompts.find(params[:id]) 53 @prompt = @question.prompts.find(params[:id])
  54 +
  55 + time_viewed = params['params']['time_viewed']
54 56
55 case direction 57 case direction
56 when :left 58 when :left
57 - successful = c = current_user.record_vote(params['params']['auto'], @prompt, 0) 59 + successful = c = current_user.record_vote(params['params']['auto'], @prompt, 0, time_viewed)
58 when :right 60 when :right
59 - successful = c = current_user.record_vote(params['params']['auto'], @prompt, 1) 61 + successful = c = current_user.record_vote(params['params']['auto'], @prompt, 1, time_viewed)
60 else 62 else
61 raise "need to specify either ':left' or ':right' as a direction" 63 raise "need to specify either ':left' or ':right' as a direction"
62 end 64 end
app/models/user.rb
@@ -18,6 +18,8 @@ class User < ActiveRecord::Base @@ -18,6 +18,8 @@ class User < ActiveRecord::Base
18 def create_choice(visitor_identifier, question, choice_params = {}) 18 def create_choice(visitor_identifier, question, choice_params = {})
19 visitor = visitors.find_or_create_by_identifier(visitor_identifier) 19 visitor = visitors.find_or_create_by_identifier(visitor_identifier)
20 raise "Question not found" if question.nil? 20 raise "Question not found" if question.nil?
  21 +
  22 + #TODO Does this serve a purpose?
21 if visitor.owns?(question) 23 if visitor.owns?(question)
22 choice = question.choices.create!(choice_params.merge(:active => false, :creator => visitor)) 24 choice = question.choices.create!(choice_params.merge(:active => false, :creator => visitor))
23 elsif question.local_identifier == choice_params[:local_identifier] 25 elsif question.local_identifier == choice_params[:local_identifier]
@@ -29,11 +31,11 @@ class User < ActiveRecord::Base @@ -29,11 +31,11 @@ class User < ActiveRecord::Base
29 return choice 31 return choice
30 end 32 end
31 33
32 - def record_vote(visitor_identifier, prompt, ordinality)  
33 - @click = Click.new(:what_was_clicked => 'on the API level, inside record_vote' + " with prompt id #{prompt.id}, ordinality #{ordinality.to_s}")  
34 - @click.save! 34 + def record_vote(visitor_identifier, prompt, ordinality, time_viewed)
  35 + #@click = Click.new(:what_was_clicked => 'on the API level, inside record_vote' + " with prompt id #{prompt.id}, ordinality #{ordinality.to_s}")
  36 + #@click.save!
35 visitor = visitors.find_or_create_by_identifier(visitor_identifier) 37 visitor = visitors.find_or_create_by_identifier(visitor_identifier)
36 - visitor.vote_for!(prompt, ordinality) 38 + visitor.vote_for!(prompt, ordinality, time_viewed)
37 #prompt.choices.each {|c| c.compute_score; c.save!} 39 #prompt.choices.each {|c| c.compute_score; c.save!}
38 end 40 end
39 41
app/models/visitor.rb
@@ -15,7 +15,7 @@ class Visitor < ActiveRecord::Base @@ -15,7 +15,7 @@ class Visitor < ActiveRecord::Base
15 questions.include? question 15 questions.include? question
16 end 16 end
17 17
18 - def vote_for!(prompt, ordinality) 18 + def vote_for!(prompt, ordinality, time_viewed)
19 choices = prompt.choices 19 choices = prompt.choices
20 choice = choices[ordinality] #we need to guarantee that the choices are in the right order (by position) 20 choice = choices[ordinality] #we need to guarantee that the choices are in the right order (by position)
21 other_choices = choices - [choice] 21 other_choices = choices - [choice]
@@ -24,7 +24,7 @@ class Visitor < ActiveRecord::Base @@ -24,7 +24,7 @@ class Visitor < ActiveRecord::Base
24 end 24 end
25 25
26 loser_choice = other_choices.first 26 loser_choice = other_choices.first
27 - 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) 27 + 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)
28 # 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 28 # 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
29 # The updated votes_count object is not saved to the db, so we don't need to worry about double counting 29 # The updated votes_count object is not saved to the db, so we don't need to worry about double counting
30 # Alternatively, we could just do choice.reload, but that results in another db read 30 # Alternatively, we could just do choice.reload, but that results in another db read
db/migrate/20100331213516_add_time_viewed_to_votes.rb 0 → 100644
@@ -0,0 +1,9 @@ @@ -0,0 +1,9 @@
  1 +class AddTimeViewedToVotes < ActiveRecord::Migration
  2 + def self.up
  3 + add_column :votes, :time_viewed, :integer #msec
  4 + end
  5 +
  6 + def self.down
  7 + remove_column :votes, :time_viewed, :integer #msec
  8 + end
  9 +end