From fca41d3408c1fe81a2b9cb19c5e7b083241ccea4 Mon Sep 17 00:00:00 2001 From: Ábner Silva de Oliveira Date: Thu, 20 Mar 2014 15:08:05 -0300 Subject: [PATCH] added more filters options to choices_controller --- app/controllers/choices_controller.rb | 38 ++++++++++++++++++++++++++++++-------- app/controllers/visitors_controller.rb | 17 +++++++++++++++-- app/models/choice.rb | 17 +++++++++++++++++ app/models/vote.rb | 3 ++- 4 files changed, 64 insertions(+), 11 deletions(-) diff --git a/app/controllers/choices_controller.rb b/app/controllers/choices_controller.rb index 8a9b9eb..b422dbe 100644 --- a/app/controllers/choices_controller.rb +++ b/app/controllers/choices_controller.rb @@ -11,28 +11,50 @@ class ChoicesController < InheritedResources::Base @question = current_user.questions.find(params[:question_id]) find_options = {:conditions => {:question_id => @question.id}, - :limit => params[:limit].to_i, - :order => 'score DESC' - } + :limit => params[:limit].to_i, + :order => 'score DESC' + } find_options[:conditions].merge!(:active => true) unless params[:include_inactive] + + if(params[:ignore_flagged]) + find_options.merge({ :include => :flags }) + find_options[:conditions].merge!({:flags => {:id => nil}}) + end + find_options.merge!(:offset => params[:offset]) if params[:offset] @choices = Choice.find(:all, find_options) else - @question = current_user.questions.find(params[:question_id], :include => :choices) #eagerloads ALL choices - unless params[:include_inactive] - @choices = @question.choices(true).active.find(:all) + @question = current_user.questions.find(params[:question_id]) #eagerloads ALL choices + + if params[:inactive_ignore_flagged] + @choices = @question.choices(true).inactive_ignore_flagged.find(:all) + elsif params[:inactive] + @choices = @question.choices(true).inactive.find(:all) else - @choices = @question.choices.find(:all) + unless params[:include_inactive] + @choices = @question.choices(true).active.find(:all) + else + @choices = @question.choices.find(:all) + end end end index! do |format| - format.xml { render :xml => @choices.to_xml(:only => [ :data, :score, :id, :active, :created_at, :wins, :losses], :methods => :user_created)} + format.xml { render :xml => @choices.to_xml(:only => [ :data, :score, :id, :active, :created_at, :wins, :losses], :methods => [:user_created, :creator_identifier])} end end + + # method added to provide more choices search options and + # provide better tool for manage the suggestions users provided + def search + @choices = Choice.search(params) + index! do |format| + format.xml { render :xml => @choices.to_xml(:only => [ :data, :score, :id, :active, :created_at, :wins, :losses], :methods => [:user_created, :creator_identifier])} + end + end def votes @choice = Choice.find(params[:id]) diff --git a/app/controllers/visitors_controller.rb b/app/controllers/visitors_controller.rb index b9f916b..194a6b0 100644 --- a/app/controllers/visitors_controller.rb +++ b/app/controllers/visitors_controller.rb @@ -1,11 +1,16 @@ +require 'will_paginate/array' + class VisitorsController < InheritedResources::Base respond_to :xml, :json before_filter :authenticate actions :index def index - cond = params[:question_id] ? "question_id = #{params[:question_id]}" : nil - + cond = nil + if params[:question_id] + Question.find(params[:question_id]) + cond = "question_id = #{params[:question_id]}" + end counts = {} if params[:votes_count] counts[:votes_count] = Vote.count(:conditions => cond, :group => "voter_id") @@ -41,7 +46,15 @@ class VisitorsController < InheritedResources::Base counts.each_pair do |attr,values| @visitors.each{ |v| v[attr] = values[v.id] || 0 } end + + if(params[:page]) + @visitors = WillPaginate::Collection.create(params[:page], params[:page_size] || 10, @visitors.length) do |pager| + result = @visitors.slice(pager.offset, pager.per_page) + result ? pager.replace(result) : nil + end + end + index! end diff --git a/app/models/choice.rb b/app/models/choice.rb index d34a50f..6a4d8a3 100644 --- a/app/models/choice.rb +++ b/app/models/choice.rb @@ -22,6 +22,7 @@ class Choice < ActiveRecord::Base has_many :skips_on_the_right, :through => :prompts_on_the_right, :source => :skips named_scope :active, :conditions => { :active => true } named_scope :inactive, :conditions => { :active => false} + named_scope :inactive_ignore_flagged, :include => :flags, :conditions => {:active => false, :flags => {:id => nil}} named_scope :not_created_by, lambda { |creator_id| { :conditions => ["creator_id <> ?", creator_id] } } @@ -81,6 +82,10 @@ class Choice < ActiveRecord::Base self.creator_id != self.question.creator_id end + def creator_identifier + self.creator.identifier + end + def compute_bt_score(btprobs = nil) if btprobs.nil? btprobs = self.question.bradley_terry_probs @@ -111,6 +116,18 @@ class Choice < ActiveRecord::Base self.save! end + def self.search(params) + question = current_user.questions.find(params[:question_id]) + + if params[:limit] + end + + if params[:scope] #:active, :inactive, :ignore_flagged + end + + #if params[:] + end + protected diff --git a/app/models/vote.rb b/app/models/vote.rb index 67e563e..4819337 100644 --- a/app/models/vote.rb +++ b/app/models/vote.rb @@ -1,3 +1,4 @@ +require 'will_paginate' class Vote < ActiveRecord::Base # belongs_to :voteable, :polymorphic => true, :counter_cache => true validates_presence_of :question @@ -19,7 +20,7 @@ class Vote < ActiveRecord::Base named_scope :active, :include => :choice, :conditions => { 'choices.active' => true } named_scope :active_loser, :include => :loser_choice, :conditions => { 'choices.active' => true } - default_scope :conditions => "#{table_name}.valid_record = 1" + default_scope :conditions => {"valid_record" => true } serialize :tracking -- libgit2 0.21.2