From c5ab68e114204b1ca8387b4feb3a7e7917aa16a9 Mon Sep 17 00:00:00 2001 From: Pius Uzamere Date: Thu, 3 Dec 2009 18:12:48 -0500 Subject: [PATCH] creating a question with seed ideas works --- app/controllers/choices_controller.rb | 22 ---------------------- app/controllers/questions_controller.rb | 2 +- app/models/choice.rb | 7 ++++--- app/models/question.rb | 14 +++++++++++++- 4 files changed, 18 insertions(+), 27 deletions(-) diff --git a/app/controllers/choices_controller.rb b/app/controllers/choices_controller.rb index a45aff9..d3028eb 100644 --- a/app/controllers/choices_controller.rb +++ b/app/controllers/choices_controller.rb @@ -8,7 +8,6 @@ class ChoicesController < InheritedResources::Base if params[:limit] @question = Question.find(params[:question_id])#, :include => :choices) @choices = Choice.find(:all, :conditions => {:question_id => @question.id}, :limit => params[:limit].to_i, :order => 'score DESC') - #@choices = @question.choices(true).sort_by {|c| 0 - c.score }.first(params[:limit].to_i)#.active else @question = Question.find(params[:question_id], :include => :choices) #eagerloads ALL choices @choices = @question.choices(true)#.sort_by {|c| 0 - c.score } @@ -18,24 +17,6 @@ class ChoicesController < InheritedResources::Base format.json { render :json => params[:data].blank? ? @choices.to_json : @choices.to_json(:include => [:items]) } end - # index! do |format| - # if !params[:voter_id].blank? - # format.xml { render :xml => User.find(params[:voter_id]).prompts_voted_on.to_xml(:include => [:items, :votes], - # :methods => [ :active_items_count, - # :all_items_count, - # :votes_count ]) } - # format.json { render :json => User.find(params[:voter_id]).prompts_voted_on.to_json(:include => [:items, :votes], - # :methods => [ :active_items_count, - # :all_items_count, - # :votes_count ]) } - # else - # format.xml { render :xml => params[:data].blank? ? - # @prompts.to_xml : - # @prompts.to_xml(:include => [:items]) - # } - # format.json { render :json => params[:data].blank? ? @prompts.to_json : @prompts.to_json(:include => [:items]) } - # end - # end end def show @@ -62,9 +43,6 @@ class ChoicesController < InheritedResources::Base logger.info "inside create_from_abroad" @question = Question.find params[:question_id] - # @visitor = Visitor.find_or_create_by_identifier(params['params']['sid']) - # @item = current_user.items.create({:data => params['params']['data'], :creator => @visitor} - # @choice = @question.choices.build(:item => @item, :creator => @visitor) respond_to do |format| if @choice = current_user.create_choice(params['params']['data'], @question, {:data => params['params']['data'], :local_identifier => params['params']['local_identifier']}) diff --git a/app/controllers/questions_controller.rb b/app/controllers/questions_controller.rb index 86223dc..9096bd3 100644 --- a/app/controllers/questions_controller.rb +++ b/app/controllers/questions_controller.rb @@ -15,7 +15,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']) + if @question = current_user.create_question(params['question']['visitor_identifier'], :name => params['question']['name'], :local_identifier => params['question']['local_identifier'], :ideas => params['question']['ideas'].split) 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 70f7a24..bbe4f18 100644 --- a/app/models/choice.rb +++ b/app/models/choice.rb @@ -2,9 +2,13 @@ class Choice < ActiveRecord::Base belongs_to :question belongs_to :item belongs_to :creator, :class_name => "Visitor", :foreign_key => "creator_id" + validates_presence_of :creator, :on => :create, :message => "can't be blank" validates_presence_of :question, :on => :create, :message => "can't be blank" + has_many :votes, :as => :voteable + has_many :prompts_on_the_left, :class_name => "Prompt", :foreign_key => "left_choice_id" + has_many :prompts_on_the_right, :class_name => "Prompt", :foreign_key => "right_choice_id" attr_accessor :data @@ -12,9 +16,6 @@ class Choice < ActiveRecord::Base item.data end - - has_many :prompts_on_the_left, :class_name => "Prompt", :foreign_key => "left_choice_id" - has_many :prompts_on_the_right, :class_name => "Prompt", :foreign_key => "right_choice_id" def wins_plus_losses (prompts_on_the_left.collect(&:votes_count).sum + prompts_on_the_right.collect(&:votes_count).sum) end diff --git a/app/models/question.rb b/app/models/question.rb index 3a96c9f..6436e21 100644 --- a/app/models/question.rb +++ b/app/models/question.rb @@ -14,7 +14,9 @@ class Question < ActiveRecord::Base end has_many :votes, :as => :voteable + #before_create :create_choices_from_ideas after_save :ensure_at_least_two_choices + attr_accessor :ideas def item_count choice_count @@ -65,9 +67,19 @@ class Question < ActiveRecord::Base validates_presence_of :site, :on => :create, :message => "can't be blank" validates_presence_of :creator, :on => :create, :message => "can't be blank" + # def create_choices_from_ideas + # the_ideas = (self.ideas.blank? || self.ideas.empty?) ? ['sample idea 1', 'sample idea 2'] : self.ideas + # + # the_ideas.each { |idea| + # item = Item.create!({:data => idea, :creator => creator}) + # choice = choices.build(:item => item, :creator => creator) + # } + # end + def ensure_at_least_two_choices + the_ideas = (self.ideas.blank? || self.ideas.empty?) ? ['sample idea 1', 'sample idea 2'] : self.ideas if self.choices.empty? - ["sample choice 1", "sample choice 2"].each { |choice_text| + the_ideas.each { |choice_text| item = Item.create!({:data => choice_text, :creator => creator}) puts item.inspect choice = choices.create!(:item => item, :creator => creator) -- libgit2 0.21.2