Commit 61b5a40bf38258533b051358275e35c04d9f68bb

Authored by Francisco Marcelo de Araújo Lima Júnior
1 parent 366f4c42

#pairwise - add support for search choices

app/controllers/choices_controller.rb
@@ -41,11 +41,11 @@ class ChoicesController < InheritedResources::Base @@ -41,11 +41,11 @@ class ChoicesController < InheritedResources::Base
41 end 41 end
42 42
43 case params[:order][:sort_by].downcase 43 case params[:order][:sort_by].downcase
44 - when "name" 44 + when "data"
45 sort_by = "choices.data" 45 sort_by = "choices.data"
46 - when "date" 46 + when "created_date"
47 sort_by = "choices.created_at" 47 sort_by = "choices.created_at"
48 - when "author" 48 + when "visitor_identifier"
49 sort_by = "visitors.identifier" 49 sort_by = "visitors.identifier"
50 else 50 else
51 sort_by = "score" 51 sort_by = "score"
@@ -55,8 +55,17 @@ class ChoicesController < InheritedResources::Base @@ -55,8 +55,17 @@ class ChoicesController < InheritedResources::Base
55 55
56 order = "#{sort_by} #{sort_order}" 56 order = "#{sort_by} #{sort_order}"
57 57
58 - find_options = { :include => [:creator] }  
59 - find_options.merge!({ :order => order }) 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
60 69
61 if (!params[:reproved].blank?) 70 if (!params[:reproved].blank?)
62 @choices = @question.choices(true).reproved.find(:all, find_options) 71 @choices = @question.choices(true).reproved.find(:all, find_options)
@@ -77,7 +86,7 @@ class ChoicesController &lt; InheritedResources::Base @@ -77,7 +86,7 @@ class ChoicesController &lt; InheritedResources::Base
77 end 86 end
78 87
79 index! do |format| 88 index! do |format|
80 - 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])}
81 end 90 end
82 91
83 end 92 end
@@ -111,9 +120,8 @@ class ChoicesController &lt; InheritedResources::Base @@ -111,9 +120,8 @@ class ChoicesController &lt; InheritedResources::Base
111 120
112 flag_params = {:choice_id => params[:id].to_i, :question_id => params[:question_id].to_i, :site_id => current_user.id} 121 flag_params = {:choice_id => params[:id].to_i, :question_id => params[:question_id].to_i, :site_id => current_user.id}
113 122
114 - if explanation = params[:explanation] 123 + if explanation = params[:explanation]
115 flag_params.merge!({:explanation => explanation}) 124 flag_params.merge!({:explanation => explanation})
116 -  
117 end 125 end
118 if visitor_identifier = params[:visitor_identifier] 126 if visitor_identifier = params[:visitor_identifier]
119 visitor = current_user.visitors.find_or_create_by_identifier(visitor_identifier) 127 visitor = current_user.visitors.find_or_create_by_identifier(visitor_identifier)
@@ -156,6 +164,5 @@ class ChoicesController &lt; InheritedResources::Base @@ -156,6 +164,5 @@ class ChoicesController &lt; InheritedResources::Base
156 end 164 end
157 end 165 end
158 166
159 -  
160 end 167 end
161 168
app/models/choice.rb
@@ -41,6 +41,10 @@ class Choice &lt; ActiveRecord::Base @@ -41,6 +41,10 @@ class Choice &lt; ActiveRecord::Base
41 end 41 end
42 end 42 end
43 43
  44 + def reproved
  45 + self.flags.any?{|a| a.choice_id == self.id}
  46 + end
  47 +
44 # if changing a choice to active, we want to regenerate prompts 48 # if changing a choice to active, we want to regenerate prompts
45 def update_prompt_queue 49 def update_prompt_queue
46 unless part_of_batch_create 50 unless part_of_batch_create