Commit f9071a75f8740b1de2726f14480c1f3c03bbca55

Authored by Dhruv Kapadia
1 parent eb7cee48

Inactive choice count for each question

app/controllers/choices_controller.rb
... ... @@ -68,6 +68,9 @@ class ChoicesController < InheritedResources::Base
68 68 the_status = @choice.active? ? 'active' : 'inactive'
69 69 options[:builder].tag!('choice_status', the_status) }
70 70 logger.info "successfully saved the choice #{@choice.inspect}"
  71 +
  72 + Question.update_counters(@question.id, :inactive_choices_count => @choice.active? ? 0 : 1)
  73 +
71 74 format.xml { render :xml => @choice.to_xml(:procs => [saved_choice_id, choice_status]), :status => :ok }
72 75 # format.xml { render :xml => @question.picked_prompt.to_xml(:methods => [:left_choice_text, :right_choice_text], :procs => [saved_choice_id, choice_status]), :status => :ok }
73 76 format.json { render :json => @question.to_json(:procs => [saved_choice_id, choice_status]), :status => :ok }
... ... @@ -87,6 +90,7 @@ class ChoicesController < InheritedResources::Base
87 90 respond_to do |format|
88 91 if @choice.activate!
89 92 logger.info "successfully activated choice #{@choice.inspect}"
  93 + Question.update_counters(@question.id, :inactive_choices_count => -1)
90 94 format.xml { render :xml => true }
91 95 format.json { render :json => true }
92 96 else
... ... @@ -109,6 +113,7 @@ class ChoicesController < InheritedResources::Base
109 113 format.json { render :json => false }
110 114 elsif @choice.deactivate!
111 115 logger.info "successfully deactivated choice #{@choice.inspect}"
  116 + Question.update_counters(@question.id, :inactive_choices_count => 1 )
112 117 format.xml { render :xml => true }
113 118 format.json { render :json => true }
114 119 else
... ...
app/models/choice.rb
... ... @@ -13,6 +13,7 @@ class Choice < ActiveRecord::Base
13 13 has_many :prompts_on_the_left, :class_name => "Prompt", :foreign_key => "left_choice_id"
14 14 has_many :prompts_on_the_right, :class_name => "Prompt", :foreign_key => "right_choice_id"
15 15 named_scope :active, :conditions => { :active => true }
  16 + named_scope :inactive, :conditions => { :active => false}
16 17  
17 18 #attr_accessor :data
18 19  
... ...
db/migrate/20100325155833_add_inactive_choice_count_to_questions.rb 0 → 100644
... ... @@ -0,0 +1,14 @@
  1 +class AddInactiveChoiceCountToQuestions < ActiveRecord::Migration
  2 + def self.up
  3 + add_column :questions, :inactive_choices_count, :integer, :default => 0
  4 + Question.reset_column_information
  5 + Question.find(:all).each do |q|
  6 + Question.update_counters(q.id, :inactive_choices_count => q.choices.inactive.size)
  7 + end
  8 +
  9 + end
  10 +
  11 + def self.down
  12 + remove_column :questions, :inactive_choices_count
  13 + end
  14 +end
... ...