Commit bd5b49852ede22ad8aaae7634329086a14ffc9bd
1 parent
739bcd4c
Exists in
master
and in
1 other branch
fixed bug introduced in vote.rb and added pagination to visitors controllers index action
Showing
2 changed files
with
17 additions
and
3 deletions
Show diff stats
app/controllers/visitors_controller.rb
| 1 | +require 'will_paginate/array' | |
| 2 | + | |
| 1 | 3 | class VisitorsController < InheritedResources::Base |
| 2 | 4 | respond_to :xml, :json |
| 3 | 5 | before_filter :authenticate |
| 4 | 6 | actions :index |
| 5 | 7 | |
| 6 | 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 | 14 | counts = {} |
| 10 | 15 | if params[:votes_count] |
| 11 | 16 | counts[:votes_count] = Vote.count(:conditions => cond, :group => "voter_id") |
| ... | ... | @@ -41,7 +46,15 @@ class VisitorsController < InheritedResources::Base |
| 41 | 46 | counts.each_pair do |attr,values| |
| 42 | 47 | @visitors.each{ |v| v[attr] = values[v.id] || 0 } |
| 43 | 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 | 58 | index! |
| 46 | 59 | end |
| 47 | 60 | ... | ... |
app/models/vote.rb
| 1 | +require 'will_paginate' | |
| 1 | 2 | class Vote < ActiveRecord::Base |
| 2 | 3 | # belongs_to :voteable, :polymorphic => true, :counter_cache => true |
| 3 | 4 | validates_presence_of :question |
| ... | ... | @@ -19,7 +20,7 @@ class Vote < ActiveRecord::Base |
| 19 | 20 | named_scope :active, :include => :choice, :conditions => { 'choices.active' => true } |
| 20 | 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 | 25 | serialize :tracking |
| 25 | 26 | ... | ... |