diff --git a/app/controllers/questions_controller.rb b/app/controllers/questions_controller.rb index b6918fc..235edce 100644 --- a/app/controllers/questions_controller.rb +++ b/app/controllers/questions_controller.rb @@ -5,6 +5,26 @@ class QuestionsController < InheritedResources::Base respond_to :csv, :only => :export #leave the option for xml export here belongs_to :site, :optional => true + def recent_votes_by_question_id + creator_id = params[:creator_id] + date = params[:date] + if creator_id + questions = Question.find(:all, :select => :id, :conditions => { :local_identifier => creator_id}) + questions_list = questions.map {|record | record.quoted_id} + question_votes_hash = Vote.with_question(questions_list).recent.count(:group => :question_id) + + elsif date #only for admins + question_votes_hash = Vote.recent(date).count(:group => :question_id) + else + question_votes_hash = Vote.recent.count(:group => :question_id) + end + + respond_to do |format| + format.xml{ render :xml => question_votes_hash.to_xml and return} + end + end + + def show @question = Question.find(params[:id]) unless params[:barebones] @@ -22,7 +42,7 @@ class QuestionsController < InheritedResources::Base show! do |format| session['prompts_ids'] ||= [] format.xml { - render :xml => @question.to_xml(:methods => [:item_count]) + render :xml => @question.to_xml(:methods => [:item_count, :votes_count]) } end end diff --git a/app/models/vote.rb b/app/models/vote.rb index 3dc1383..31f3ffd 100644 --- a/app/models/vote.rb +++ b/app/models/vote.rb @@ -5,4 +5,7 @@ class Vote < ActiveRecord::Base belongs_to :prompt, :counter_cache => true belongs_to :choice, :counter_cache => true belongs_to :loser_choice, :class_name => "Choice", :foreign_key => "loser_choice_id" + + named_scope :recent, lambda { |*args| {:conditions => ["created_at > ?", (args.first || Date.today.beginning_of_day)]} } + named_scope :with_question, lambda { |*args| {:conditions => {:question_id => args.first }} } end diff --git a/config/routes.rb b/config/routes.rb index abcdab6..3c82e6d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,6 +1,6 @@ ActionController::Routing::Routes.draw do |map| map.resources :clicks - map.resources :questions, :member => { :object_info_totals_by_date => :get, :num_votes_by_visitor_id => :get, :export => :post, :set_autoactivate_ideas_from_abroad => :put, :activate => :put, :suspend => :put} do |question| + map.resources :questions, :member => { :object_info_totals_by_date => :get, :num_votes_by_visitor_id => :get, :export => :post, :set_autoactivate_ideas_from_abroad => :put, :activate => :put, :suspend => :put}, :collection => {:recent_votes_by_question_id => :get} do |question| question.resources :items question.resources :prompts, :member => {:vote_left => :post, :vote_right => :post, :skip => :post, :vote => :post}, :collection => {:single => :get, :index => :get} -- libgit2 0.21.2