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 51 logger.info "#{current_user.inspect} is voting #{direction} at #{Time.now}."
52 52 @question = Question.find(params[:question_id])
53 53 @prompt = @question.prompts.find(params[:id])
  54 +
  55 + time_viewed = params['params']['time_viewed']
54 56  
55 57 case direction
56 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 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 62 else
61 63 raise "need to specify either ':left' or ':right' as a direction"
62 64 end
... ...
app/models/user.rb
... ... @@ -18,6 +18,8 @@ class User < ActiveRecord::Base
18 18 def create_choice(visitor_identifier, question, choice_params = {})
19 19 visitor = visitors.find_or_create_by_identifier(visitor_identifier)
20 20 raise "Question not found" if question.nil?
  21 +
  22 + #TODO Does this serve a purpose?
21 23 if visitor.owns?(question)
22 24 choice = question.choices.create!(choice_params.merge(:active => false, :creator => visitor))
23 25 elsif question.local_identifier == choice_params[:local_identifier]
... ... @@ -29,11 +31,11 @@ class User < ActiveRecord::Base
29 31 return choice
30 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 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 39 #prompt.choices.each {|c| c.compute_score; c.save!}
38 40 end
39 41  
... ...
app/models/visitor.rb
... ... @@ -15,7 +15,7 @@ class Visitor < ActiveRecord::Base
15 15 questions.include? question
16 16 end
17 17  
18   - def vote_for!(prompt, ordinality)
  18 + def vote_for!(prompt, ordinality, time_viewed)
19 19 choices = prompt.choices
20 20 choice = choices[ordinality] #we need to guarantee that the choices are in the right order (by position)
21 21 other_choices = choices - [choice]
... ... @@ -24,7 +24,7 @@ class Visitor < ActiveRecord::Base
24 24 end
25 25  
26 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 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 29 # The updated votes_count object is not saved to the db, so we don't need to worry about double counting
30 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 @@
  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
... ...