Commit fb487ab0581530bd9d5fca1f833298bbfeb721f4
1 parent
2aea3f19
Exists in
master
and in
1 other branch
results and scoring
Showing
4 changed files
with
94 additions
and
0 deletions
Show diff stats
app/controllers/choices_controller.rb
| ... | ... | @@ -4,6 +4,58 @@ class ChoicesController < InheritedResources::Base |
| 4 | 4 | belongs_to :question |
| 5 | 5 | has_scope :active, :boolean => true, :only => :index |
| 6 | 6 | |
| 7 | + def index | |
| 8 | + if params[:limit] | |
| 9 | + @question = Question.find(params[:question_id])#, :include => :choices) | |
| 10 | + @choices = Choice.find(:all, :conditions => {:question_id => @question.id}, :limit => params[:limit].to_i, :order => 'score DESC') | |
| 11 | + #@choices = @question.choices(true).sort_by {|c| 0 - c.score }.first(params[:limit].to_i)#.active | |
| 12 | + else | |
| 13 | + @question = Question.find(params[:question_id], :include => :choices) #eagerloads ALL choices | |
| 14 | + @choices = @question.choices(true)#.sort_by {|c| 0 - c.score } | |
| 15 | + end | |
| 16 | + index! do |format| | |
| 17 | + format.xml { render :xml => params[:data].blank? ? @choices.to_xml(:methods => [:data, :votes_count]) : @choices.to_xml(:include => [:items], :methods => [:data, :votes_count])} | |
| 18 | + format.json { render :json => params[:data].blank? ? @choices.to_json : @choices.to_json(:include => [:items]) } | |
| 19 | + end | |
| 20 | + | |
| 21 | + # index! do |format| | |
| 22 | + # if !params[:voter_id].blank? | |
| 23 | + # format.xml { render :xml => User.find(params[:voter_id]).prompts_voted_on.to_xml(:include => [:items, :votes], | |
| 24 | + # :methods => [ :active_items_count, | |
| 25 | + # :all_items_count, | |
| 26 | + # :votes_count ]) } | |
| 27 | + # format.json { render :json => User.find(params[:voter_id]).prompts_voted_on.to_json(:include => [:items, :votes], | |
| 28 | + # :methods => [ :active_items_count, | |
| 29 | + # :all_items_count, | |
| 30 | + # :votes_count ]) } | |
| 31 | + # else | |
| 32 | + # format.xml { render :xml => params[:data].blank? ? | |
| 33 | + # @prompts.to_xml : | |
| 34 | + # @prompts.to_xml(:include => [:items]) | |
| 35 | + # } | |
| 36 | + # format.json { render :json => params[:data].blank? ? @prompts.to_json : @prompts.to_json(:include => [:items]) } | |
| 37 | + # end | |
| 38 | + # end | |
| 39 | + end | |
| 40 | + | |
| 41 | + def show | |
| 42 | + @question = Question.find(params[:question_id]) | |
| 43 | + @prompt = @question.prompts.find(params[:id]) | |
| 44 | + show! do |format| | |
| 45 | + format.xml { render :xml => @prompt.to_xml(:methods => [:left_choice_text, :right_choice_text])} | |
| 46 | + format.json { render :json => @prompt.to_json(:methods => [:left_choice_text, :right_choice_text])} | |
| 47 | + end | |
| 48 | + end | |
| 49 | + | |
| 50 | + def single | |
| 51 | + @question = current_user.questions.find(params[:question_id]) | |
| 52 | + @prompt = @question.prompts.pick | |
| 53 | + show! do |format| | |
| 54 | + format.xml { render :xml => @prompt.to_xml} | |
| 55 | + format.json { render :json => @prompt.to_json} | |
| 56 | + end | |
| 57 | + end | |
| 58 | + | |
| 7 | 59 | |
| 8 | 60 | def create_from_abroad |
| 9 | 61 | authenticate | ... | ... |
app/models/choice.rb
| ... | ... | @@ -8,6 +8,25 @@ class Choice < ActiveRecord::Base |
| 8 | 8 | |
| 9 | 9 | attr_accessor :data |
| 10 | 10 | |
| 11 | + def data | |
| 12 | + item.data | |
| 13 | + end | |
| 14 | + | |
| 15 | + | |
| 16 | + has_many :prompts_on_the_left, :class_name => "Prompt", :foreign_key => "left_choice_id" | |
| 17 | + has_many :prompts_on_the_right, :class_name => "Prompt", :foreign_key => "right_choice_id" | |
| 18 | + def wins_plus_losses | |
| 19 | + (prompts_on_the_left.collect(&:votes_count).sum + prompts_on_the_right.collect(&:votes_count).sum) | |
| 20 | + end | |
| 21 | + | |
| 22 | + def wins | |
| 23 | + votes_count | |
| 24 | + end | |
| 25 | + | |
| 26 | + def votes_count | |
| 27 | + votes(true).size | |
| 28 | + end | |
| 29 | + | |
| 11 | 30 | |
| 12 | 31 | after_create :generate_prompts |
| 13 | 32 | def before_create |
| ... | ... | @@ -17,7 +36,24 @@ class Choice < ActiveRecord::Base |
| 17 | 36 | end |
| 18 | 37 | end |
| 19 | 38 | |
| 39 | + def before_save | |
| 40 | + unless self.score | |
| 41 | + self.score = 0.0 | |
| 42 | + end | |
| 43 | + return true #so active record will save | |
| 44 | + end | |
| 45 | + | |
| 46 | + | |
| 47 | + def compute_score | |
| 48 | + if wins_plus_losses == 0 | |
| 49 | + return 0 | |
| 50 | + else | |
| 51 | + (wins.to_f / wins_plus_losses ) * 100 | |
| 52 | + end | |
| 53 | + end | |
| 54 | + | |
| 20 | 55 | protected |
| 56 | + | |
| 21 | 57 | |
| 22 | 58 | def generate_prompts |
| 23 | 59 | #once a choice is added, we need to generate the new prompts (possible combinations of choices) | ... | ... |
app/models/prompt.rb
| ... | ... | @@ -26,6 +26,10 @@ class Prompt < ActiveRecord::Base |
| 26 | 26 | validates_presence_of :left_choice, :on => :create, :message => "can't be blank" |
| 27 | 27 | validates_presence_of :right_choice, :on => :create, :message => "can't be blank" |
| 28 | 28 | |
| 29 | + def votes_count | |
| 30 | + votes(true).size | |
| 31 | + end | |
| 32 | + | |
| 29 | 33 | def choices |
| 30 | 34 | [left_choice, right_choice] |
| 31 | 35 | end | ... | ... |
app/models/visitor.rb
| ... | ... | @@ -16,6 +16,8 @@ class Visitor < ActiveRecord::Base |
| 16 | 16 | choice = prompt.choices[ordinality] #we need to guarantee that the choices are in the right order (by position) |
| 17 | 17 | prompt_vote = votes.create!(:voteable => prompt) |
| 18 | 18 | choice_vote = votes.create!(:voteable => choice) |
| 19 | + choice.score = choice.compute_score | |
| 20 | + choice.save! | |
| 19 | 21 | end |
| 20 | 22 | |
| 21 | 23 | def skip!(prompt) | ... | ... |