Commit bc7ce11022499f08ef28fe87b0a943e56c680e6d

Authored by Dhruv Kapadia
1 parent 87857f3f

Adding pagination support to choices controller

Showing 1 changed file with 12 additions and 11 deletions   Show diff stats
app/controllers/choices_controller.rb
... ... @@ -4,24 +4,25 @@ class ChoicesController < InheritedResources::Base
4 4 belongs_to :question
5 5 has_scope :active, :boolean => true, :only => :index
6 6  
7   - before_filter :authenticate, :only => [:flag]
  7 + before_filter :authenticate, :only => [:index, :flag]
8 8 #caches_page :index
9 9  
10 10 def index
11 11 if params[:limit]
12   - @question = Question.find(params[:question_id])#, :include => :choices)
13   - @question.reload
  12 + @question = Question.find(params[:question_id])
  13 +
  14 + find_options = {:conditions => {:question_id => @question.id},
  15 + :limit => params[:limit].to_i,
  16 + :order => 'score DESC',
  17 + :include => :item}
14 18  
15   - #should compute score after each win/loss
16   - #@question.choices.each(&:compute_score!)
17   - unless params[:include_inactive]
18   - @choices = Choice.find(:all, :conditions => {:question_id => @question.id, :active => true}, :limit => params[:limit].to_i, :order => 'score DESC', :include => :item)
19   - else
20   - @choices = Choice.find(:all, :conditions => {:question_id => @question.id}, :limit => params[:limit].to_i, :order => 'score DESC', :include => :item)
21   - end
  19 + find_options[:conditions].merge!(:active => true) if params[:include_inactive]
  20 + find_options.merge!(:offset => params[:offset]) if params[:offset]
  21 +
  22 + @choices = Choice.find(:all, find_options)
  23 +
22 24 else
23 25 @question = Question.find(params[:question_id], :include => :choices) #eagerloads ALL choices
24   - #@question.choices.each(&:compute_score!)
25 26 unless params[:include_inactive]
26 27 @choices = @question.choices(true).active.find(:all, :include => :item)
27 28 else
... ...