Commit e461e6c00a29479ef501b3f7fafcb591ed31a40e

Authored by Dmitri Garbuzov
1 parent a3ff7e4b

Removed old *_by_question_id reporting methods

Showing 1 changed file with 19 additions and 53 deletions   Show diff stats
app/controllers/questions_controller.rb
1 1 require 'fastercsv'
  2 +require 'generator'
2 3  
3 4 class QuestionsController < InheritedResources::Base
4 5 actions :all, :except => [ :show, :edit, :delete ]
... ... @@ -7,53 +8,6 @@ class QuestionsController &lt; InheritedResources::Base
7 8 respond_to :csv, :only => :export #leave the option for xml export here
8 9 belongs_to :site, :optional => true
9 10  
10   - def recent_votes_by_question_id
11   - creator_id = params[:creator_id]
12   - date = params[:date]
13   - if creator_id
14   - questions = Question.find(:all, :select => :id, :conditions => { :local_identifier => creator_id})
15   - questions_list = questions.map {|record | record.quoted_id}
16   - question_votes_hash = Vote.with_question(questions_list).recent.count(:group => :question_id)
17   -
18   - elsif date #only for admins
19   - question_votes_hash = Vote.recent(date).count(:group => :question_id)
20   - else
21   - question_votes_hash = Vote.recent.count(:group => :question_id)
22   - end
23   -
24   - respond_to do |format|
25   - format.xml{ render :xml => question_votes_hash.to_xml and return}
26   - end
27   - end
28   -
29   - def object_info_totals_by_question_id
30   - total_ideas_by_q_id = Choice.count(:include => :question,
31   - :conditions => "choices.creator_id <> questions.creator_id",
32   - :group => "choices.question_id")
33   -
34   - active_ideas_by_q_id = Choice.count(:include => :question,
35   - :conditions => "choices.active = 1 AND choices.creator_id <> questions.creator_id",
36   - :group => "choices.question_id")
37   -
38   - combined_hash = {}
39   -
40   - total_ideas_by_q_id.each do |q_id, num_total|
41   - combined_hash[q_id] = {}
42   - combined_hash[q_id][:total_ideas] = num_total
43   - if(active_ideas_by_q_id.has_key?(q_id))
44   - combined_hash[q_id][:active_ideas]= active_ideas_by_q_id[q_id]
45   - else
46   - combined_hash[q_id][:active_ideas]= 0
47   - end
48   - end
49   - respond_to do |format|
50   - format.xml { render :xml => combined_hash.to_xml and return}
51   - end
52   -
53   - end
54   -
55   -
56   -
57 11 def show
58 12 @question = current_user.questions.find(params[:id])
59 13  
... ... @@ -312,26 +266,38 @@ class QuestionsController &lt; InheritedResources::Base
312 266  
313 267 counts = {}
314 268 if params[:user_ideas]
315   - counts[:user_ideas] = Choice.count(:joins => :question,
  269 + counts['user-ideas'] = Choice.count(:joins => :question,
316 270 :conditions => "choices.creator_id <> questions.creator_id",
317 271 :group => "choices.question_id")
318 272 end
319 273 if params[:active_user_ideas]
320   - counts[:active_user_ideas] = Choice.count(:joins => :question,
  274 + counts['active-user-ideas'] = Choice.count(:joins => :question,
321 275 :conditions => "choices.active = 1 AND choices.creator_id <> questions.creator_id",
322 276 :group => "choices.question_id")
323 277 end
324 278 if params[:votes_since]
325   - counts[:recent_votes] = Vote.count(:joins => :question,
  279 + counts['recent-votes'] = Vote.count(:joins => :question,
326 280 :conditions => ["votes.created_at > ?", params[:votes_since]],
327 281 :group => "votes.question_id")
328 282 end
329 283  
330   - counts.each_pair do |attr,hash|
331   - @questions.each{ |q| q[attr] = hash[q.id] || 0 }
  284 + # There doesn't seem to be a good way to add procs to an array of
  285 + # objects. This solution depends on Array#to_xml rendering each
  286 + # member in the correct order. Internally, it just uses, #each, so
  287 + # this _should_ work.
  288 + ids = Generator.new{ |g| @questions.each{ |q| g.yield q.id } }
  289 + extra_info = Proc.new do |o|
  290 + id = ids.next
  291 + counts.each_pair do |attr, hash|
  292 + o[:builder].tag!(attr, hash[id] || 0 , :type => "integer")
  293 + end
332 294 end
333 295  
334   - index!
  296 + index! do |format|
  297 + format.xml do
  298 + render :xml => @questions.to_xml(:procs => [ extra_info ])
  299 + end
  300 + end
335 301 end
336 302  
337 303 protected
... ...