diff --git a/app/controllers/choices_controller.rb b/app/controllers/choices_controller.rb index 7c30dff..25c1c9a 100644 --- a/app/controllers/choices_controller.rb +++ b/app/controllers/choices_controller.rb @@ -52,8 +52,11 @@ class ChoicesController < InheritedResources::Base respond_to do |format| if @choice = current_user.create_choice(params['params']['data'], @question, {:data => params['params']['data'], :local_identifier => params['params']['local_identifier']}) saved_choice_id = Proc.new { |options| options[:builder].tag!('saved_choice_id', @choice.id) } + choice_status = Proc.new { |options| + the_status = @choice.active? ? 'active' : 'inactive' + options[:builder].tag!('choice_status', the_status) } logger.info "successfully saved the choice #{@choice.inspect}" - format.xml { render :xml => @question.picked_prompt.to_xml(:methods => [:left_choice_text, :right_choice_text], :procs => [saved_choice_id]), :status => :ok } + format.xml { render :xml => @question.picked_prompt.to_xml(:methods => [:left_choice_text, :right_choice_text], :procs => [saved_choice_id, choice_status]), :status => :ok } format.json { render :json => @question.picked_prompt.to_json, :status => :ok } else format.xml { render :xml => @choice.errors, :status => :unprocessable_entity } @@ -81,19 +84,19 @@ class ChoicesController < InheritedResources::Base end def activate - authenticate - @question = current_user.questions.find(params[:question_id]) - @choice = @question.choices.find(params[:id]) - respond_to do |format| - if @choice.activate! - format.xml { render :xml => @choice.to_xml, :status => :created } - format.json { render :json => @choice.to_json, :status => :created } - else - format.xml { render :xml => @choice.errors, :status => :unprocessable_entity } - format.json { render :json => @choice.to_json } - end + authenticate + @question = current_user.questions.find(params[:question_id]) + @choice = @question.choices.find(params[:id]) + respond_to do |format| + if @choice.activate! + format.xml { render :xml => @choice.to_xml, :status => :created } + format.json { render :json => @choice.to_json, :status => :created } + else + format.xml { render :xml => @choice.errors, :status => :unprocessable_entity } + format.json { render :json => @choice.to_json } end end + end def suspend diff --git a/app/controllers/questions_controller.rb b/app/controllers/questions_controller.rb index 43def3d..1d3f433 100644 --- a/app/controllers/questions_controller.rb +++ b/app/controllers/questions_controller.rb @@ -20,7 +20,7 @@ class QuestionsController < InheritedResources::Base def create authenticate logger.info "vi is #{params['question']['visitor_identifier']} and local are #{params['question']['local_identifier']}. all params are #{params.inspect}" - 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) + 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) respond_to do |format| format.xml { render :xml => @question.to_xml} end diff --git a/app/models/choice.rb b/app/models/choice.rb index 2977eba..ef43d6a 100644 --- a/app/models/choice.rb +++ b/app/models/choice.rb @@ -7,6 +7,7 @@ class Choice < ActiveRecord::Base validates_presence_of :creator, :on => :create, :message => "can't be blank" validates_presence_of :question, :on => :create, :message => "can't be blank" + #validates_length_of :item, :maximum => 140 has_many :votes, :as => :voteable has_many :prompts_on_the_left, :class_name => "Prompt", :foreign_key => "left_choice_id" @@ -51,6 +52,9 @@ class Choice < ActiveRecord::Base unless self.score self.score = 0.0 end + unless self.active? + question.should_autoactivate_ideas? ? self.active = true : self.active = false + end return true #so active record will save end diff --git a/app/models/question.rb b/app/models/question.rb index 911b3b6..9c8230b 100644 --- a/app/models/question.rb +++ b/app/models/question.rb @@ -24,23 +24,27 @@ class Question < ActiveRecord::Base choices_count end - #TODO: generalize for prompts of rank > 2 - #TODO: add index for rapid finding - def picked_prompt(rank = 2) - raise NotImplementedError.new("Sorry, we currently only support pairwise prompts. Rank of the prompt must be 2.") unless rank == 2 + #TODO: generalize for prompts of rank > 2 + #TODO: add index for rapid finding + def picked_prompt(rank = 2) + raise NotImplementedError.new("Sorry, we currently only support pairwise prompts. Rank of the prompt must be 2.") unless rank == 2 + begin choice_id_array = distinct_array_of_choice_ids(rank) @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 }]) - end - memoize :picked_prompt + end until @p.active? + return @p + end + memoize :picked_prompt def distinct_array_of_choice_ids(rank = 2, only_active = true) @choice_ids = choice_ids @s = @choice_ids.size begin - @the_choice_ids = Set.new(@choice_ids.values_at(rand(@s), rand(@s))) + first_one, second_one = rand(@s), rand(@s) + @the_choice_ids = Set.new(@choice_ids.values_at(first_one, second_one)) # @the_choice_ids << choices.active.first(:order => 'RAND()', :select => 'id').id # @the_choice_ids << choices.active.last(:order => 'RAND()', :select => 'id').id - end until @the_choice_ids.size == rank + end until (@the_choice_ids.size == rank) logger.info "set populated and looks like #{@the_choice_ids.inspect}" return @the_choice_ids.to_a end @@ -65,6 +69,9 @@ class Question < ActiveRecord::Base u.questions_voted_on.include? self end + def should_autoactivate_ideas? + it_should_autoactivate_ideas? + end validates_presence_of :site, :on => :create, :message => "can't be blank" validates_presence_of :creator, :on => :create, :message => "can't be blank" diff --git a/db/migrate/20100107200151_add_flag_for_idea_activation_to_questions.rb b/db/migrate/20100107200151_add_flag_for_idea_activation_to_questions.rb new file mode 100644 index 0000000..c17e25a --- /dev/null +++ b/db/migrate/20100107200151_add_flag_for_idea_activation_to_questions.rb @@ -0,0 +1,9 @@ +class AddFlagForIdeaActivationToQuestions < ActiveRecord::Migration + def self.up + add_column :questions, :it_should_autoactivate_ideas, :boolean, :default => false + end + + def self.down + remove_column :questions, :it_should_autoactivate_ideas + end +end -- libgit2 0.21.2