Commit 5c18a693f1dd6764cfd3e73a03d4ccc3eb3d1d87

Authored by Francisco Marcelo de Araújo Lima Júnior
2 parents dd066a50 61b5a40b

Merge branch pairwise_search into master

app/controllers/choices_controller.rb
... ... @@ -5,20 +5,22 @@ class ChoicesController < InheritedResources::Base
5 5 has_scope :active, :type => :boolean, :only => :index
6 6  
7 7 before_filter :authenticate
8   -
  8 +
9 9 def index
10 10 if params[:limit]
11 11 @question = current_user.questions.find(params[:question_id])
12 12  
13 13 find_options = {:conditions => {:question_id => @question.id},
14   - :limit => params[:limit].to_i,
  14 + :limit => params[:limit].to_i,
15 15 :order => 'score DESC'
16 16 }
17   -
  17 +
18 18 find_options[:conditions].merge!(:active => true) unless params[:include_inactive]
19 19  
  20 + find_options[:include] = [:creator]
  21 +
20 22 if(params[:ignore_flagged])
21   - find_options.merge({ :include => :flags })
  23 + find_options[:include] << :flags
22 24 find_options[:conditions].merge!({:flags => {:id => nil}})
23 25 end
24 26  
... ... @@ -27,22 +29,64 @@ class ChoicesController &lt; InheritedResources::Base
27 29 @choices = Choice.find(:all, find_options)
28 30  
29 31 else
30   - @question = current_user.questions.find(params[:question_id]) #eagerloads ALL choices
  32 + @question = current_user.questions.find(params[:question_id])
  33 +
  34 + sort_by = "score"
  35 + sort_order = "DESC"
  36 +
  37 + if !params[:order].blank?
  38 +
  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
  42 +
  43 + case params[:order][:sort_by].downcase
  44 + when "data"
  45 + sort_by = "choices.data"
  46 + when "created_date"
  47 + sort_by = "choices.created_at"
  48 + when "visitor_identifier"
  49 + sort_by = "visitors.identifier"
  50 + else
  51 + sort_by = "score"
  52 + end
  53 +
  54 + end
31 55  
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)
  56 + order = "#{sort_by} #{sort_order}"
  57 +
  58 + find_options = {
  59 + :include => [:creator],
  60 + :conditions => {},
  61 + :order => order
  62 + }
  63 +
  64 + if params[:filter] && !params[:filter][:data].blank?
  65 + conditions = []
  66 + conditions << ['lower(data) like ?', "%#{params[:filter][:data].downcase}%"]
  67 + find_options[:conditions] = [conditions.map{|c| c[0] }.join(" AND "), *conditions.map{|c| c[1..-1] }.flatten]
  68 + end
  69 +
  70 + if (!params[:reproved].blank?)
  71 + @choices = @question.choices(true).reproved.find(:all, find_options)
36 72 else
37   - unless params[:include_inactive]
38   - @choices = @question.choices(true).active.find(:all)
  73 + if params[:inactive_ignore_flagged]
  74 + @choices = @question.choices(true).inactive_ignore_flagged.find(:all, find_options)
  75 + elsif params[:inactive]
  76 + @choices = @question.choices(true).inactive.find(:all, find_options)
39 77 else
40   - @choices = @question.choices.find(:all)
  78 + unless params[:include_inactive]
  79 + @choices = @question.choices(true).active.find(:all, find_options)
  80 + else
  81 + @choices = @question.choices.find(:all, find_options)
  82 + end
41 83 end
42 84 end
  85 +
43 86 end
  87 +
44 88 index! do |format|
45   - format.xml { render :xml => @choices.to_xml(:only => [ :data, :score, :id, :active, :created_at, :wins, :losses], :methods => [:user_created, :creator_identifier])}
  89 + format.xml { render :xml => @choices.to_xml(:only => [ :data, :score, :id, :active, :created_at, :wins, :losses], :methods => [:user_created, :creator_identifier, :reproved])}
46 90 end
47 91  
48 92 end
... ... @@ -62,10 +106,10 @@ class ChoicesController &lt; InheritedResources::Base
62 106 end
63 107  
64 108 def create
65   -
  109 +
66 110 visitor_identifier = params[:choice].delete(:visitor_identifier)
67 111  
68   - visitor = current_user.default_visitor
  112 + visitor = current_user.default_visitor
69 113 if visitor_identifier
70 114 visitor = current_user.visitors.find_or_create_by_identifier(visitor_identifier)
71 115 end
... ... @@ -78,16 +122,15 @@ class ChoicesController &lt; InheritedResources::Base
78 122 @choice = Choice.new(params[:choice])
79 123 create!
80 124 end
81   -
  125 +
82 126 def flag
83 127 @question = current_user.questions.find(params[:question_id])
84 128 @choice = @question.choices.find(params[:id])
85 129  
86 130 flag_params = {:choice_id => params[:id].to_i, :question_id => params[:question_id].to_i, :site_id => current_user.id}
87 131  
88   - if explanation = params[:explanation]
  132 + if explanation = params[:explanation]
89 133 flag_params.merge!({:explanation => explanation})
90   -
91 134 end
92 135 if visitor_identifier = params[:visitor_identifier]
93 136 visitor = current_user.visitors.find_or_create_by_identifier(visitor_identifier)
... ... @@ -130,6 +173,5 @@ class ChoicesController &lt; InheritedResources::Base
130 173 end
131 174 end
132 175  
133   -
134 176 end
135   -
  177 +
... ...
app/models/choice.rb
... ... @@ -23,6 +23,7 @@ class Choice &lt; ActiveRecord::Base
23 23 named_scope :active, :conditions => { :active => true }
24 24 named_scope :inactive, :conditions => { :active => false}
25 25 named_scope :inactive_ignore_flagged, :include => :flags, :conditions => {:active => false, :flags => {:id => nil}}
  26 + named_scope :reproved, :joins => :flags
26 27 named_scope :not_created_by, lambda { |creator_id|
27 28 { :conditions => ["creator_id <> ?", creator_id] }
28 29 }
... ... @@ -40,6 +41,10 @@ class Choice &lt; ActiveRecord::Base
40 41 end
41 42 end
42 43  
  44 + def reproved
  45 + self.flags.any?{|a| a.choice_id == self.id}
  46 + end
  47 +
43 48 # if changing a choice to active, we want to regenerate prompts
44 49 def update_prompt_queue
45 50 unless part_of_batch_create
... ... @@ -83,7 +88,7 @@ class Choice &lt; ActiveRecord::Base
83 88 end
84 89  
85 90 def creator_identifier
86   - self.creator.identifier
  91 + self.creator.identifier
87 92 end
88 93  
89 94 def compute_bt_score(btprobs = nil)
... ...