Commit 39f927e5bbb18054bdd572b22b0a6be3455aceff

Authored by Victor Costa
1 parent b27c4c3a

added params to search and order questions

Showing 1 changed file with 21 additions and 21 deletions   Show diff stats
app/controllers/choices_controller.rb
... ... @@ -7,27 +7,27 @@ class ChoicesController < InheritedResources::Base
7 7 before_filter :authenticate
8 8  
9 9 def index
10   - if params[:limit]
11   - @question = current_user.questions.find(params[:question_id])
12   -
13   - find_options = {:conditions => {:question_id => @question.id},
14   - :limit => params[:limit].to_i,
15   - :order => 'score DESC'
16   - }
17   -
18   - find_options[:conditions].merge!(:active => true) unless params[:include_inactive]
19   - find_options.merge!(:offset => params[:offset]) if params[:offset]
20   -
21   - @choices = Choice.find(:all, find_options)
22   -
23   - 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)
27   - else
28   - @choices = @question.choices.find(:all)
29   - end
30   - end
  10 + order = 'score DESC'
  11 + order = params[:order].map{|a| "#{a.first} #{a.second}"}.join(',') unless params[:order].blank?
  12 +
  13 + conditions = []
  14 + conditions << ['lower(data) like ?', params[:filter][:data].downcase] if params[:filter] && !params[:filter][:data].blank?
  15 +
  16 + @question = current_user.questions.find(params[:question_id])
  17 +
  18 + find_options = {:conditions => {:question_id => @question.id},
  19 + :order => order
  20 + }
  21 +
  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]
  25 +
  26 + find_options.merge!(:limit => params[:limit].to_i) if params[:limit]
  27 + find_options.merge!(:offset => params[:offset]) if params[:offset]
  28 +
  29 + @choices = Choice.find(:all, find_options)
  30 +
31 31 index! do |format|
32 32 format.xml { render :xml => @choices.to_xml(:only => [ :data, :score, :id, :active, :created_at, :wins, :losses], :methods => :user_created)}
33 33 end
... ...