Commit c5ab68e114204b1ca8387b4feb3a7e7917aa16a9

Authored by Pius Uzamere
1 parent cbadb50b

creating a question with seed ideas works

app/controllers/choices_controller.rb
@@ -8,7 +8,6 @@ class ChoicesController < InheritedResources::Base @@ -8,7 +8,6 @@ class ChoicesController < InheritedResources::Base
8 if params[:limit] 8 if params[:limit]
9 @question = Question.find(params[:question_id])#, :include => :choices) 9 @question = Question.find(params[:question_id])#, :include => :choices)
10 @choices = Choice.find(:all, :conditions => {:question_id => @question.id}, :limit => params[:limit].to_i, :order => 'score DESC') 10 @choices = Choice.find(:all, :conditions => {:question_id => @question.id}, :limit => params[:limit].to_i, :order => 'score DESC')
11 - #@choices = @question.choices(true).sort_by {|c| 0 - c.score }.first(params[:limit].to_i)#.active  
12 else 11 else
13 @question = Question.find(params[:question_id], :include => :choices) #eagerloads ALL choices 12 @question = Question.find(params[:question_id], :include => :choices) #eagerloads ALL choices
14 @choices = @question.choices(true)#.sort_by {|c| 0 - c.score } 13 @choices = @question.choices(true)#.sort_by {|c| 0 - c.score }
@@ -18,24 +17,6 @@ class ChoicesController < InheritedResources::Base @@ -18,24 +17,6 @@ class ChoicesController < InheritedResources::Base
18 format.json { render :json => params[:data].blank? ? @choices.to_json : @choices.to_json(:include => [:items]) } 17 format.json { render :json => params[:data].blank? ? @choices.to_json : @choices.to_json(:include => [:items]) }
19 end 18 end
20 19
21 - # index! do |format|  
22 - # if !params[:voter_id].blank?  
23 - # format.xml { render :xml => User.find(params[:voter_id]).prompts_voted_on.to_xml(:include => [:items, :votes],  
24 - # :methods => [ :active_items_count,  
25 - # :all_items_count,  
26 - # :votes_count ]) }  
27 - # format.json { render :json => User.find(params[:voter_id]).prompts_voted_on.to_json(:include => [:items, :votes],  
28 - # :methods => [ :active_items_count,  
29 - # :all_items_count,  
30 - # :votes_count ]) }  
31 - # else  
32 - # format.xml { render :xml => params[:data].blank? ?  
33 - # @prompts.to_xml :  
34 - # @prompts.to_xml(:include => [:items])  
35 - # }  
36 - # format.json { render :json => params[:data].blank? ? @prompts.to_json : @prompts.to_json(:include => [:items]) }  
37 - # end  
38 - # end  
39 end 20 end
40 21
41 def show 22 def show
@@ -62,9 +43,6 @@ class ChoicesController < InheritedResources::Base @@ -62,9 +43,6 @@ class ChoicesController < InheritedResources::Base
62 logger.info "inside create_from_abroad" 43 logger.info "inside create_from_abroad"
63 44
64 @question = Question.find params[:question_id] 45 @question = Question.find params[:question_id]
65 - # @visitor = Visitor.find_or_create_by_identifier(params['params']['sid'])  
66 - # @item = current_user.items.create({:data => params['params']['data'], :creator => @visitor}  
67 - # @choice = @question.choices.build(:item => @item, :creator => @visitor)  
68 46
69 respond_to do |format| 47 respond_to do |format|
70 if @choice = current_user.create_choice(params['params']['data'], @question, {:data => params['params']['data'], :local_identifier => params['params']['local_identifier']}) 48 if @choice = current_user.create_choice(params['params']['data'], @question, {:data => params['params']['data'], :local_identifier => params['params']['local_identifier']})
app/controllers/questions_controller.rb
@@ -15,7 +15,7 @@ class QuestionsController < InheritedResources::Base @@ -15,7 +15,7 @@ class QuestionsController < InheritedResources::Base
15 def create 15 def create
16 authenticate 16 authenticate
17 logger.info "vi is #{params['question']['visitor_identifier']} and local are #{params['question']['local_identifier']}. all params are #{params.inspect}" 17 logger.info "vi is #{params['question']['visitor_identifier']} and local are #{params['question']['local_identifier']}. all params are #{params.inspect}"
18 - if @question = current_user.create_question(params['question']['visitor_identifier'], :name => params['question']['name'], :local_identifier => params['question']['local_identifier']) 18 + 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)
19 respond_to do |format| 19 respond_to do |format|
20 format.xml { render :xml => @question.to_xml} 20 format.xml { render :xml => @question.to_xml}
21 end 21 end
app/models/choice.rb
@@ -2,9 +2,13 @@ class Choice < ActiveRecord::Base @@ -2,9 +2,13 @@ class Choice < ActiveRecord::Base
2 belongs_to :question 2 belongs_to :question
3 belongs_to :item 3 belongs_to :item
4 belongs_to :creator, :class_name => "Visitor", :foreign_key => "creator_id" 4 belongs_to :creator, :class_name => "Visitor", :foreign_key => "creator_id"
  5 +
5 validates_presence_of :creator, :on => :create, :message => "can't be blank" 6 validates_presence_of :creator, :on => :create, :message => "can't be blank"
6 validates_presence_of :question, :on => :create, :message => "can't be blank" 7 validates_presence_of :question, :on => :create, :message => "can't be blank"
  8 +
7 has_many :votes, :as => :voteable 9 has_many :votes, :as => :voteable
  10 + has_many :prompts_on_the_left, :class_name => "Prompt", :foreign_key => "left_choice_id"
  11 + has_many :prompts_on_the_right, :class_name => "Prompt", :foreign_key => "right_choice_id"
8 12
9 attr_accessor :data 13 attr_accessor :data
10 14
@@ -12,9 +16,6 @@ class Choice < ActiveRecord::Base @@ -12,9 +16,6 @@ class Choice < ActiveRecord::Base
12 item.data 16 item.data
13 end 17 end
14 18
15 -  
16 - has_many :prompts_on_the_left, :class_name => "Prompt", :foreign_key => "left_choice_id"  
17 - has_many :prompts_on_the_right, :class_name => "Prompt", :foreign_key => "right_choice_id"  
18 def wins_plus_losses 19 def wins_plus_losses
19 (prompts_on_the_left.collect(&:votes_count).sum + prompts_on_the_right.collect(&:votes_count).sum) 20 (prompts_on_the_left.collect(&:votes_count).sum + prompts_on_the_right.collect(&:votes_count).sum)
20 end 21 end
app/models/question.rb
@@ -14,7 +14,9 @@ class Question < ActiveRecord::Base @@ -14,7 +14,9 @@ class Question < ActiveRecord::Base
14 end 14 end
15 has_many :votes, :as => :voteable 15 has_many :votes, :as => :voteable
16 16
  17 + #before_create :create_choices_from_ideas
17 after_save :ensure_at_least_two_choices 18 after_save :ensure_at_least_two_choices
  19 + attr_accessor :ideas
18 20
19 def item_count 21 def item_count
20 choice_count 22 choice_count
@@ -65,9 +67,19 @@ class Question < ActiveRecord::Base @@ -65,9 +67,19 @@ class Question < ActiveRecord::Base
65 validates_presence_of :site, :on => :create, :message => "can't be blank" 67 validates_presence_of :site, :on => :create, :message => "can't be blank"
66 validates_presence_of :creator, :on => :create, :message => "can't be blank" 68 validates_presence_of :creator, :on => :create, :message => "can't be blank"
67 69
  70 + # def create_choices_from_ideas
  71 + # the_ideas = (self.ideas.blank? || self.ideas.empty?) ? ['sample idea 1', 'sample idea 2'] : self.ideas
  72 + #
  73 + # the_ideas.each { |idea|
  74 + # item = Item.create!({:data => idea, :creator => creator})
  75 + # choice = choices.build(:item => item, :creator => creator)
  76 + # }
  77 + # end
  78 +
68 def ensure_at_least_two_choices 79 def ensure_at_least_two_choices
  80 + the_ideas = (self.ideas.blank? || self.ideas.empty?) ? ['sample idea 1', 'sample idea 2'] : self.ideas
69 if self.choices.empty? 81 if self.choices.empty?
70 - ["sample choice 1", "sample choice 2"].each { |choice_text| 82 + the_ideas.each { |choice_text|
71 item = Item.create!({:data => choice_text, :creator => creator}) 83 item = Item.create!({:data => choice_text, :creator => creator})
72 puts item.inspect 84 puts item.inspect
73 choice = choices.create!(:item => item, :creator => creator) 85 choice = choices.create!(:item => item, :creator => creator)