Commit 366f4c42d7e2b4f3da15c6173971c68ae33bbe18
1 parent
39f927e5
Exists in
master
and in
1 other branch
#pairwise - add support for sorting choices
Showing
2 changed files
with
76 additions
and
22 deletions
Show diff stats
app/controllers/choices_controller.rb
@@ -5,45 +5,93 @@ class ChoicesController < InheritedResources::Base | @@ -5,45 +5,93 @@ class ChoicesController < InheritedResources::Base | ||
5 | has_scope :active, :type => :boolean, :only => :index | 5 | has_scope :active, :type => :boolean, :only => :index |
6 | 6 | ||
7 | before_filter :authenticate | 7 | before_filter :authenticate |
8 | - | 8 | + |
9 | def index | 9 | def index |
10 | - order = 'score DESC' | ||
11 | - order = params[:order].map{|a| "#{a.first} #{a.second}"}.join(',') unless params[:order].blank? | 10 | + if params[:limit] |
11 | + @question = current_user.questions.find(params[:question_id]) | ||
12 | 12 | ||
13 | - conditions = [] | ||
14 | - conditions << ['lower(data) like ?', params[:filter][:data].downcase] if params[:filter] && !params[:filter][:data].blank? | 13 | + find_options = {:conditions => {:question_id => @question.id}, |
14 | + :limit => params[:limit].to_i, | ||
15 | + :order => 'score DESC' | ||
16 | + } | ||
15 | 17 | ||
16 | - @question = current_user.questions.find(params[:question_id]) | 18 | + find_options[:conditions].merge!(:active => true) unless params[:include_inactive] |
19 | + | ||
20 | + find_options[:include] = [:creator] | ||
21 | + | ||
22 | + if(params[:ignore_flagged]) | ||
23 | + find_options[:include] << :flags | ||
24 | + find_options[:conditions].merge!({:flags => {:id => nil}}) | ||
25 | + end | ||
26 | + | ||
27 | + find_options.merge!(:offset => params[:offset]) if params[:offset] | ||
28 | + | ||
29 | + @choices = Choice.find(:all, find_options) | ||
30 | + | ||
31 | + else | ||
32 | + @question = current_user.questions.find(params[:question_id]) | ||
33 | + | ||
34 | + sort_by = "score" | ||
35 | + sort_order = "DESC" | ||
36 | + | ||
37 | + if !params[:order].blank? | ||
17 | 38 | ||
18 | - find_options = {:conditions => {:question_id => @question.id}, | ||
19 | - :order => order | ||
20 | - } | 39 | + if params[:order][:sort_order].downcase == "asc" or params[:order][:sort_order].downcase == "desc" |
40 | + sort_order = params[:order][:sort_order].downcase | ||
41 | + end | ||
21 | 42 | ||
22 | - conditions << ["question_id = ?", @question.id] | ||
23 | - conditions << ['active = ?', true] unless params[:include_inactive] | ||
24 | - find_options[:conditions] = [conditions.map{|c| c[0] }.join(" AND "), *conditions.map{|c| c[1..-1] }.flatten] | 43 | + case params[:order][:sort_by].downcase |
44 | + when "name" | ||
45 | + sort_by = "choices.data" | ||
46 | + when "date" | ||
47 | + sort_by = "choices.created_at" | ||
48 | + when "author" | ||
49 | + sort_by = "visitors.identifier" | ||
50 | + else | ||
51 | + sort_by = "score" | ||
52 | + end | ||
25 | 53 | ||
26 | - find_options.merge!(:limit => params[:limit].to_i) if params[:limit] | ||
27 | - find_options.merge!(:offset => params[:offset]) if params[:offset] | 54 | + end |
28 | 55 | ||
29 | - @choices = Choice.find(:all, find_options) | 56 | + order = "#{sort_by} #{sort_order}" |
57 | + | ||
58 | + find_options = { :include => [:creator] } | ||
59 | + find_options.merge!({ :order => order }) | ||
60 | + | ||
61 | + if (!params[:reproved].blank?) | ||
62 | + @choices = @question.choices(true).reproved.find(:all, find_options) | ||
63 | + else | ||
64 | + if params[:inactive_ignore_flagged] | ||
65 | + @choices = @question.choices(true).inactive_ignore_flagged.find(:all, find_options) | ||
66 | + elsif params[:inactive] | ||
67 | + @choices = @question.choices(true).inactive.find(:all, find_options) | ||
68 | + else | ||
69 | + unless params[:include_inactive] | ||
70 | + @choices = @question.choices(true).active.find(:all, find_options) | ||
71 | + else | ||
72 | + @choices = @question.choices.find(:all, find_options) | ||
73 | + end | ||
74 | + end | ||
75 | + end | ||
76 | + | ||
77 | + end | ||
30 | 78 | ||
31 | index! do |format| | 79 | index! do |format| |
32 | - format.xml { render :xml => @choices.to_xml(:only => [ :data, :score, :id, :active, :created_at, :wins, :losses], :methods => :user_created)} | 80 | + format.xml { render :xml => @choices.to_xml(:only => [ :data, :score, :id, :active, :created_at, :wins, :losses], :methods => [:user_created, :creator_identifier])} |
33 | end | 81 | end |
34 | 82 | ||
35 | end | 83 | end |
36 | - | 84 | + |
37 | def votes | 85 | def votes |
38 | @choice = Choice.find(params[:id]) | 86 | @choice = Choice.find(params[:id]) |
39 | render :xml => @choice.votes.to_xml | 87 | render :xml => @choice.votes.to_xml |
40 | end | 88 | end |
41 | 89 | ||
42 | def create | 90 | def create |
43 | - | 91 | + |
44 | visitor_identifier = params[:choice].delete(:visitor_identifier) | 92 | visitor_identifier = params[:choice].delete(:visitor_identifier) |
45 | 93 | ||
46 | - visitor = current_user.default_visitor | 94 | + visitor = current_user.default_visitor |
47 | if visitor_identifier | 95 | if visitor_identifier |
48 | visitor = current_user.visitors.find_or_create_by_identifier(visitor_identifier) | 96 | visitor = current_user.visitors.find_or_create_by_identifier(visitor_identifier) |
49 | end | 97 | end |
@@ -56,7 +104,7 @@ class ChoicesController < InheritedResources::Base | @@ -56,7 +104,7 @@ class ChoicesController < InheritedResources::Base | ||
56 | @choice = Choice.new(params[:choice]) | 104 | @choice = Choice.new(params[:choice]) |
57 | create! | 105 | create! |
58 | end | 106 | end |
59 | - | 107 | + |
60 | def flag | 108 | def flag |
61 | @question = current_user.questions.find(params[:question_id]) | 109 | @question = current_user.questions.find(params[:question_id]) |
62 | @choice = @question.choices.find(params[:id]) | 110 | @choice = @question.choices.find(params[:id]) |
@@ -65,7 +113,7 @@ class ChoicesController < InheritedResources::Base | @@ -65,7 +113,7 @@ class ChoicesController < InheritedResources::Base | ||
65 | 113 | ||
66 | if explanation = params[:explanation] | 114 | if explanation = params[:explanation] |
67 | flag_params.merge!({:explanation => explanation}) | 115 | flag_params.merge!({:explanation => explanation}) |
68 | - | 116 | + |
69 | end | 117 | end |
70 | if visitor_identifier = params[:visitor_identifier] | 118 | if visitor_identifier = params[:visitor_identifier] |
71 | visitor = current_user.visitors.find_or_create_by_identifier(visitor_identifier) | 119 | visitor = current_user.visitors.find_or_create_by_identifier(visitor_identifier) |
@@ -110,4 +158,4 @@ class ChoicesController < InheritedResources::Base | @@ -110,4 +158,4 @@ class ChoicesController < InheritedResources::Base | ||
110 | 158 | ||
111 | 159 | ||
112 | end | 160 | end |
113 | - | 161 | + |
app/models/choice.rb
@@ -22,6 +22,8 @@ class Choice < ActiveRecord::Base | @@ -22,6 +22,8 @@ class Choice < 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}} | ||
26 | + named_scope :reproved, :joins => :flags | ||
25 | named_scope :not_created_by, lambda { |creator_id| | 27 | named_scope :not_created_by, lambda { |creator_id| |
26 | { :conditions => ["creator_id <> ?", creator_id] } | 28 | { :conditions => ["creator_id <> ?", creator_id] } |
27 | } | 29 | } |
@@ -82,6 +84,10 @@ class Choice < ActiveRecord::Base | @@ -82,6 +84,10 @@ class Choice < ActiveRecord::Base | ||
82 | self.creator_id != self.question.creator_id | 84 | self.creator_id != self.question.creator_id |
83 | end | 85 | end |
84 | 86 | ||
87 | + def creator_identifier | ||
88 | + self.creator.identifier | ||
89 | + end | ||
90 | + | ||
85 | def compute_bt_score(btprobs = nil) | 91 | def compute_bt_score(btprobs = nil) |
86 | if btprobs.nil? | 92 | if btprobs.nil? |
87 | btprobs = self.question.bradley_terry_probs | 93 | btprobs = self.question.bradley_terry_probs |