Commit d6ea3f13b570b87170716a8c86e408385d2db926
1 parent
36dd01a4
Exists in
master
and in
1 other branch
Added ability to get recent vote information by question
- Named scopes added to vote model
Showing
3 changed files
with
25 additions
and
2 deletions
Show diff stats
app/controllers/questions_controller.rb
... | ... | @@ -5,6 +5,26 @@ class QuestionsController < InheritedResources::Base |
5 | 5 | respond_to :csv, :only => :export #leave the option for xml export here |
6 | 6 | belongs_to :site, :optional => true |
7 | 7 | |
8 | + def recent_votes_by_question_id | |
9 | + creator_id = params[:creator_id] | |
10 | + date = params[:date] | |
11 | + if creator_id | |
12 | + questions = Question.find(:all, :select => :id, :conditions => { :local_identifier => creator_id}) | |
13 | + questions_list = questions.map {|record | record.quoted_id} | |
14 | + question_votes_hash = Vote.with_question(questions_list).recent.count(:group => :question_id) | |
15 | + | |
16 | + elsif date #only for admins | |
17 | + question_votes_hash = Vote.recent(date).count(:group => :question_id) | |
18 | + else | |
19 | + question_votes_hash = Vote.recent.count(:group => :question_id) | |
20 | + end | |
21 | + | |
22 | + respond_to do |format| | |
23 | + format.xml{ render :xml => question_votes_hash.to_xml and return} | |
24 | + end | |
25 | + end | |
26 | + | |
27 | + | |
8 | 28 | def show |
9 | 29 | @question = Question.find(params[:id]) |
10 | 30 | unless params[:barebones] |
... | ... | @@ -22,7 +42,7 @@ class QuestionsController < InheritedResources::Base |
22 | 42 | show! do |format| |
23 | 43 | session['prompts_ids'] ||= [] |
24 | 44 | format.xml { |
25 | - render :xml => @question.to_xml(:methods => [:item_count]) | |
45 | + render :xml => @question.to_xml(:methods => [:item_count, :votes_count]) | |
26 | 46 | } |
27 | 47 | end |
28 | 48 | end | ... | ... |
app/models/vote.rb
... | ... | @@ -5,4 +5,7 @@ class Vote < ActiveRecord::Base |
5 | 5 | belongs_to :prompt, :counter_cache => true |
6 | 6 | belongs_to :choice, :counter_cache => true |
7 | 7 | belongs_to :loser_choice, :class_name => "Choice", :foreign_key => "loser_choice_id" |
8 | + | |
9 | + named_scope :recent, lambda { |*args| {:conditions => ["created_at > ?", (args.first || Date.today.beginning_of_day)]} } | |
10 | + named_scope :with_question, lambda { |*args| {:conditions => {:question_id => args.first }} } | |
8 | 11 | end | ... | ... |
config/routes.rb
1 | 1 | ActionController::Routing::Routes.draw do |map| |
2 | 2 | map.resources :clicks |
3 | - 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| | |
3 | + 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| | |
4 | 4 | question.resources :items |
5 | 5 | question.resources :prompts, :member => {:vote_left => :post, :vote_right => :post, :skip => :post, :vote => :post}, |
6 | 6 | :collection => {:single => :get, :index => :get} | ... | ... |