Commit bd5b49852ede22ad8aaae7634329086a14ffc9bd

Authored by Ábner Silva de Oliveira
1 parent 739bcd4c

fixed bug introduced in vote.rb and added pagination to visitors controllers index action

app/controllers/visitors_controller.rb
  1 +require 'will_paginate/array'
  2 +
1 class VisitorsController < InheritedResources::Base 3 class VisitorsController < InheritedResources::Base
2 respond_to :xml, :json 4 respond_to :xml, :json
3 before_filter :authenticate 5 before_filter :authenticate
4 actions :index 6 actions :index
5 7
6 def index 8 def index
7 - cond = params[:question_id] ? "question_id = #{params[:question_id]}" : nil  
8 - 9 + cond = nil
  10 + if params[:question_id]
  11 + Question.find(params[:question_id])
  12 + cond = "question_id = #{params[:question_id]}"
  13 + end
9 counts = {} 14 counts = {}
10 if params[:votes_count] 15 if params[:votes_count]
11 counts[:votes_count] = Vote.count(:conditions => cond, :group => "voter_id") 16 counts[:votes_count] = Vote.count(:conditions => cond, :group => "voter_id")
@@ -41,7 +46,15 @@ class VisitorsController &lt; InheritedResources::Base @@ -41,7 +46,15 @@ class VisitorsController &lt; InheritedResources::Base
41 counts.each_pair do |attr,values| 46 counts.each_pair do |attr,values|
42 @visitors.each{ |v| v[attr] = values[v.id] || 0 } 47 @visitors.each{ |v| v[attr] = values[v.id] || 0 }
43 end 48 end
  49 +
  50 + if(params[:page])
  51 + @visitors = WillPaginate::Collection.create(params[:page], params[:page_size] || 10, @visitors.length) do |pager|
  52 + result = @visitors.slice(pager.offset, pager.per_page)
  53 + result ? pager.replace(result) : nil
  54 + end
  55 + end
44 56
  57 +
45 index! 58 index!
46 end 59 end
47 60
app/models/vote.rb
  1 +require 'will_paginate'
1 class Vote < ActiveRecord::Base 2 class Vote < ActiveRecord::Base
2 # belongs_to :voteable, :polymorphic => true, :counter_cache => true 3 # belongs_to :voteable, :polymorphic => true, :counter_cache => true
3 validates_presence_of :question 4 validates_presence_of :question
@@ -19,7 +20,7 @@ class Vote &lt; ActiveRecord::Base @@ -19,7 +20,7 @@ class Vote &lt; ActiveRecord::Base
19 named_scope :active, :include => :choice, :conditions => { 'choices.active' => true } 20 named_scope :active, :include => :choice, :conditions => { 'choices.active' => true }
20 named_scope :active_loser, :include => :loser_choice, :conditions => { 'choices.active' => true } 21 named_scope :active_loser, :include => :loser_choice, :conditions => { 'choices.active' => true }
21 22
22 - default_scope :conditions => {"#{table_name}.valid_record" => true } 23 + default_scope :conditions => {"valid_record" => true }
23 24
24 serialize :tracking 25 serialize :tracking
25 26