Commit 33dac88d0bb200d45bf92b775233d99787b1f852

Authored by Pius Uzamere
1 parent 23a288e7

to be staged

app/controllers/choices_controller.rb
@@ -52,8 +52,11 @@ class ChoicesController < InheritedResources::Base @@ -52,8 +52,11 @@ class ChoicesController < InheritedResources::Base
52 respond_to do |format| 52 respond_to do |format|
53 if @choice = current_user.create_choice(params['params']['data'], @question, {:data => params['params']['data'], :local_identifier => params['params']['local_identifier']}) 53 if @choice = current_user.create_choice(params['params']['data'], @question, {:data => params['params']['data'], :local_identifier => params['params']['local_identifier']})
54 saved_choice_id = Proc.new { |options| options[:builder].tag!('saved_choice_id', @choice.id) } 54 saved_choice_id = Proc.new { |options| options[:builder].tag!('saved_choice_id', @choice.id) }
  55 + choice_status = Proc.new { |options|
  56 + the_status = @choice.active? ? 'active' : 'inactive'
  57 + options[:builder].tag!('choice_status', the_status) }
55 logger.info "successfully saved the choice #{@choice.inspect}" 58 logger.info "successfully saved the choice #{@choice.inspect}"
56 - format.xml { render :xml => @question.picked_prompt.to_xml(:methods => [:left_choice_text, :right_choice_text], :procs => [saved_choice_id]), :status => :ok } 59 + format.xml { render :xml => @question.picked_prompt.to_xml(:methods => [:left_choice_text, :right_choice_text], :procs => [saved_choice_id, choice_status]), :status => :ok }
57 format.json { render :json => @question.picked_prompt.to_json, :status => :ok } 60 format.json { render :json => @question.picked_prompt.to_json, :status => :ok }
58 else 61 else
59 format.xml { render :xml => @choice.errors, :status => :unprocessable_entity } 62 format.xml { render :xml => @choice.errors, :status => :unprocessable_entity }
@@ -81,19 +84,19 @@ class ChoicesController < InheritedResources::Base @@ -81,19 +84,19 @@ class ChoicesController < InheritedResources::Base
81 end 84 end
82 85
83 def activate 86 def activate
84 - authenticate  
85 - @question = current_user.questions.find(params[:question_id])  
86 - @choice = @question.choices.find(params[:id])  
87 - respond_to do |format|  
88 - if @choice.activate!  
89 - format.xml { render :xml => @choice.to_xml, :status => :created }  
90 - format.json { render :json => @choice.to_json, :status => :created }  
91 - else  
92 - format.xml { render :xml => @choice.errors, :status => :unprocessable_entity }  
93 - format.json { render :json => @choice.to_json }  
94 - end 87 + authenticate
  88 + @question = current_user.questions.find(params[:question_id])
  89 + @choice = @question.choices.find(params[:id])
  90 + respond_to do |format|
  91 + if @choice.activate!
  92 + format.xml { render :xml => @choice.to_xml, :status => :created }
  93 + format.json { render :json => @choice.to_json, :status => :created }
  94 + else
  95 + format.xml { render :xml => @choice.errors, :status => :unprocessable_entity }
  96 + format.json { render :json => @choice.to_json }
95 end 97 end
96 end 98 end
  99 + end
97 100
98 101
99 def suspend 102 def suspend
app/controllers/questions_controller.rb
@@ -20,7 +20,7 @@ class QuestionsController < InheritedResources::Base @@ -20,7 +20,7 @@ class QuestionsController < InheritedResources::Base
20 def create 20 def create
21 authenticate 21 authenticate
22 logger.info "vi is #{params['question']['visitor_identifier']} and local are #{params['question']['local_identifier']}. all params are #{params.inspect}" 22 logger.info "vi is #{params['question']['visitor_identifier']} and local are #{params['question']['local_identifier']}. all params are #{params.inspect}"
23 - if @question = current_user.create_question(params['question']['visitor_identifier'], :name => params['question']['name'], :local_identifier => params['question']['local_identifier'], :ideas => begin (params['question']['ideas'].lines.to_a) rescue [] end) 23 + if @question = current_user.create_question(params['question']['visitor_identifier'], :name => params['question']['name'], :local_identifier => params['question']['local_identifier'], :ideas => begin (params['question']['ideas'].lines.to_a.delete_if? {|i| i.blank?}) rescue [] end)
24 respond_to do |format| 24 respond_to do |format|
25 format.xml { render :xml => @question.to_xml} 25 format.xml { render :xml => @question.to_xml}
26 end 26 end
app/models/choice.rb
@@ -7,6 +7,7 @@ class Choice < ActiveRecord::Base @@ -7,6 +7,7 @@ class Choice < ActiveRecord::Base
7 7
8 validates_presence_of :creator, :on => :create, :message => "can't be blank" 8 validates_presence_of :creator, :on => :create, :message => "can't be blank"
9 validates_presence_of :question, :on => :create, :message => "can't be blank" 9 validates_presence_of :question, :on => :create, :message => "can't be blank"
  10 + #validates_length_of :item, :maximum => 140
10 11
11 has_many :votes, :as => :voteable 12 has_many :votes, :as => :voteable
12 has_many :prompts_on_the_left, :class_name => "Prompt", :foreign_key => "left_choice_id" 13 has_many :prompts_on_the_left, :class_name => "Prompt", :foreign_key => "left_choice_id"
@@ -51,6 +52,9 @@ class Choice < ActiveRecord::Base @@ -51,6 +52,9 @@ class Choice < ActiveRecord::Base
51 unless self.score 52 unless self.score
52 self.score = 0.0 53 self.score = 0.0
53 end 54 end
  55 + unless self.active?
  56 + question.should_autoactivate_ideas? ? self.active = true : self.active = false
  57 + end
54 return true #so active record will save 58 return true #so active record will save
55 end 59 end
56 60
app/models/question.rb
@@ -24,23 +24,27 @@ class Question < ActiveRecord::Base @@ -24,23 +24,27 @@ class Question < ActiveRecord::Base
24 choices_count 24 choices_count
25 end 25 end
26 26
27 - #TODO: generalize for prompts of rank > 2  
28 - #TODO: add index for rapid finding  
29 - def picked_prompt(rank = 2)  
30 - raise NotImplementedError.new("Sorry, we currently only support pairwise prompts. Rank of the prompt must be 2.") unless rank == 2 27 + #TODO: generalize for prompts of rank > 2
  28 + #TODO: add index for rapid finding
  29 + def picked_prompt(rank = 2)
  30 + raise NotImplementedError.new("Sorry, we currently only support pairwise prompts. Rank of the prompt must be 2.") unless rank == 2
  31 + begin
31 choice_id_array = distinct_array_of_choice_ids(rank) 32 choice_id_array = distinct_array_of_choice_ids(rank)
32 @p ||= prompts.find_or_create_by_left_choice_id_and_right_choice_id(choice_id_array[0], choice_id_array[1], :include => [{ :left_choice => :item }, { :right_choice => :item }]) 33 @p ||= prompts.find_or_create_by_left_choice_id_and_right_choice_id(choice_id_array[0], choice_id_array[1], :include => [{ :left_choice => :item }, { :right_choice => :item }])
33 - end  
34 - memoize :picked_prompt 34 + end until @p.active?
  35 + return @p
  36 + end
  37 + memoize :picked_prompt
35 38
36 def distinct_array_of_choice_ids(rank = 2, only_active = true) 39 def distinct_array_of_choice_ids(rank = 2, only_active = true)
37 @choice_ids = choice_ids 40 @choice_ids = choice_ids
38 @s = @choice_ids.size 41 @s = @choice_ids.size
39 begin 42 begin
40 - @the_choice_ids = Set.new(@choice_ids.values_at(rand(@s), rand(@s))) 43 + first_one, second_one = rand(@s), rand(@s)
  44 + @the_choice_ids = Set.new(@choice_ids.values_at(first_one, second_one))
41 # @the_choice_ids << choices.active.first(:order => 'RAND()', :select => 'id').id 45 # @the_choice_ids << choices.active.first(:order => 'RAND()', :select => 'id').id
42 # @the_choice_ids << choices.active.last(:order => 'RAND()', :select => 'id').id 46 # @the_choice_ids << choices.active.last(:order => 'RAND()', :select => 'id').id
43 - end until @the_choice_ids.size == rank 47 + end until (@the_choice_ids.size == rank)
44 logger.info "set populated and looks like #{@the_choice_ids.inspect}" 48 logger.info "set populated and looks like #{@the_choice_ids.inspect}"
45 return @the_choice_ids.to_a 49 return @the_choice_ids.to_a
46 end 50 end
@@ -65,6 +69,9 @@ class Question &lt; ActiveRecord::Base @@ -65,6 +69,9 @@ class Question &lt; ActiveRecord::Base
65 u.questions_voted_on.include? self 69 u.questions_voted_on.include? self
66 end 70 end
67 71
  72 + def should_autoactivate_ideas?
  73 + it_should_autoactivate_ideas?
  74 + end
68 75
69 validates_presence_of :site, :on => :create, :message => "can't be blank" 76 validates_presence_of :site, :on => :create, :message => "can't be blank"
70 validates_presence_of :creator, :on => :create, :message => "can't be blank" 77 validates_presence_of :creator, :on => :create, :message => "can't be blank"
db/migrate/20100107200151_add_flag_for_idea_activation_to_questions.rb 0 → 100644
@@ -0,0 +1,9 @@ @@ -0,0 +1,9 @@
  1 +class AddFlagForIdeaActivationToQuestions < ActiveRecord::Migration
  2 + def self.up
  3 + add_column :questions, :it_should_autoactivate_ideas, :boolean, :default => false
  4 + end
  5 +
  6 + def self.down
  7 + remove_column :questions, :it_should_autoactivate_ideas
  8 + end
  9 +end