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 8 if params[:limit]
9 9 @question = Question.find(params[:question_id])#, :include => :choices)
10 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 11 else
13 12 @question = Question.find(params[:question_id], :include => :choices) #eagerloads ALL choices
14 13 @choices = @question.choices(true)#.sort_by {|c| 0 - c.score }
... ... @@ -18,24 +17,6 @@ class ChoicesController < InheritedResources::Base
18 17 format.json { render :json => params[:data].blank? ? @choices.to_json : @choices.to_json(:include => [:items]) }
19 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 20 end
40 21  
41 22 def show
... ... @@ -62,9 +43,6 @@ class ChoicesController < InheritedResources::Base
62 43 logger.info "inside create_from_abroad"
63 44  
64 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 47 respond_to do |format|
70 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 15 def create
16 16 authenticate
17 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 19 respond_to do |format|
20 20 format.xml { render :xml => @question.to_xml}
21 21 end
... ...
app/models/choice.rb
... ... @@ -2,9 +2,13 @@ class Choice < ActiveRecord::Base
2 2 belongs_to :question
3 3 belongs_to :item
4 4 belongs_to :creator, :class_name => "Visitor", :foreign_key => "creator_id"
  5 +
5 6 validates_presence_of :creator, :on => :create, :message => "can't be blank"
6 7 validates_presence_of :question, :on => :create, :message => "can't be blank"
  8 +
7 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 13 attr_accessor :data
10 14  
... ... @@ -12,9 +16,6 @@ class Choice < ActiveRecord::Base
12 16 item.data
13 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 19 def wins_plus_losses
19 20 (prompts_on_the_left.collect(&:votes_count).sum + prompts_on_the_right.collect(&:votes_count).sum)
20 21 end
... ...
app/models/question.rb
... ... @@ -14,7 +14,9 @@ class Question < ActiveRecord::Base
14 14 end
15 15 has_many :votes, :as => :voteable
16 16  
  17 + #before_create :create_choices_from_ideas
17 18 after_save :ensure_at_least_two_choices
  19 + attr_accessor :ideas
18 20  
19 21 def item_count
20 22 choice_count
... ... @@ -65,9 +67,19 @@ class Question < ActiveRecord::Base
65 67 validates_presence_of :site, :on => :create, :message => "can't be blank"
66 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 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 81 if self.choices.empty?
70   - ["sample choice 1", "sample choice 2"].each { |choice_text|
  82 + the_ideas.each { |choice_text|
71 83 item = Item.create!({:data => choice_text, :creator => creator})
72 84 puts item.inspect
73 85 choice = choices.create!(:item => item, :creator => creator)
... ...