Commit fca41d3408c1fe81a2b9cb19c5e7b083241ccea4

Authored by Ábner Silva de Oliveira
1 parent 1b00d7f2

added more filters options to choices_controller

app/controllers/choices_controller.rb
@@ -11,28 +11,50 @@ class ChoicesController < InheritedResources::Base @@ -11,28 +11,50 @@ class ChoicesController < InheritedResources::Base
11 @question = current_user.questions.find(params[:question_id]) 11 @question = current_user.questions.find(params[:question_id])
12 12
13 find_options = {:conditions => {:question_id => @question.id}, 13 find_options = {:conditions => {:question_id => @question.id},
14 - :limit => params[:limit].to_i,  
15 - :order => 'score DESC'  
16 - } 14 + :limit => params[:limit].to_i,
  15 + :order => 'score DESC'
  16 + }
17 17
18 find_options[:conditions].merge!(:active => true) unless params[:include_inactive] 18 find_options[:conditions].merge!(:active => true) unless params[:include_inactive]
  19 +
  20 + if(params[:ignore_flagged])
  21 + find_options.merge({ :include => :flags })
  22 + find_options[:conditions].merge!({:flags => {:id => nil}})
  23 + end
  24 +
19 find_options.merge!(:offset => params[:offset]) if params[:offset] 25 find_options.merge!(:offset => params[:offset]) if params[:offset]
20 26
21 @choices = Choice.find(:all, find_options) 27 @choices = Choice.find(:all, find_options)
22 28
23 else 29 else
24 - @question = current_user.questions.find(params[:question_id], :include => :choices) #eagerloads ALL choices  
25 - unless params[:include_inactive]  
26 - @choices = @question.choices(true).active.find(:all) 30 + @question = current_user.questions.find(params[:question_id]) #eagerloads ALL choices
  31 +
  32 + if params[:inactive_ignore_flagged]
  33 + @choices = @question.choices(true).inactive_ignore_flagged.find(:all)
  34 + elsif params[:inactive]
  35 + @choices = @question.choices(true).inactive.find(:all)
27 else 36 else
28 - @choices = @question.choices.find(:all) 37 + unless params[:include_inactive]
  38 + @choices = @question.choices(true).active.find(:all)
  39 + else
  40 + @choices = @question.choices.find(:all)
  41 + end
29 end 42 end
30 end 43 end
31 index! do |format| 44 index! do |format|
32 - format.xml { render :xml => @choices.to_xml(:only => [ :data, :score, :id, :active, :created_at, :wins, :losses], :methods => :user_created)} 45 + format.xml { render :xml => @choices.to_xml(:only => [ :data, :score, :id, :active, :created_at, :wins, :losses], :methods => [:user_created, :creator_identifier])}
33 end 46 end
34 47
35 end 48 end
  49 +
  50 + # method added to provide more choices search options and
  51 + # provide better tool for manage the suggestions users provided
  52 + def search
  53 + @choices = Choice.search(params)
  54 + index! do |format|
  55 + format.xml { render :xml => @choices.to_xml(:only => [ :data, :score, :id, :active, :created_at, :wins, :losses], :methods => [:user_created, :creator_identifier])}
  56 + end
  57 + end
36 58
37 def votes 59 def votes
38 @choice = Choice.find(params[:id]) 60 @choice = Choice.find(params[:id])
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/choice.rb
@@ -22,6 +22,7 @@ class Choice &lt; ActiveRecord::Base @@ -22,6 +22,7 @@ class Choice &lt; ActiveRecord::Base
22 has_many :skips_on_the_right, :through => :prompts_on_the_right, :source => :skips 22 has_many :skips_on_the_right, :through => :prompts_on_the_right, :source => :skips
23 named_scope :active, :conditions => { :active => true } 23 named_scope :active, :conditions => { :active => true }
24 named_scope :inactive, :conditions => { :active => false} 24 named_scope :inactive, :conditions => { :active => false}
  25 + named_scope :inactive_ignore_flagged, :include => :flags, :conditions => {:active => false, :flags => {:id => nil}}
25 named_scope :not_created_by, lambda { |creator_id| 26 named_scope :not_created_by, lambda { |creator_id|
26 { :conditions => ["creator_id <> ?", creator_id] } 27 { :conditions => ["creator_id <> ?", creator_id] }
27 } 28 }
@@ -81,6 +82,10 @@ class Choice &lt; ActiveRecord::Base @@ -81,6 +82,10 @@ class Choice &lt; ActiveRecord::Base
81 self.creator_id != self.question.creator_id 82 self.creator_id != self.question.creator_id
82 end 83 end
83 84
  85 + def creator_identifier
  86 + self.creator.identifier
  87 + end
  88 +
84 def compute_bt_score(btprobs = nil) 89 def compute_bt_score(btprobs = nil)
85 if btprobs.nil? 90 if btprobs.nil?
86 btprobs = self.question.bradley_terry_probs 91 btprobs = self.question.bradley_terry_probs
@@ -111,6 +116,18 @@ class Choice &lt; ActiveRecord::Base @@ -111,6 +116,18 @@ class Choice &lt; ActiveRecord::Base
111 self.save! 116 self.save!
112 end 117 end
113 118
  119 + def self.search(params)
  120 + question = current_user.questions.find(params[:question_id])
  121 +
  122 + if params[:limit]
  123 + end
  124 +
  125 + if params[:scope] #:active, :inactive, :ignore_flagged
  126 + end
  127 +
  128 + #if params[:]
  129 + end
  130 +
114 protected 131 protected
115 132
116 133
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 = 1" 23 + default_scope :conditions => {"valid_record" => true }
23 24
24 serialize :tracking 25 serialize :tracking
25 26