diff --git a/app/models/question.rb b/app/models/question.rb index 964296e..8ff23a3 100644 --- a/app/models/question.rb +++ b/app/models/question.rb @@ -26,13 +26,13 @@ class Question < ActiveRecord::Base attr_accessor :ideas def item_count - choices_count + choices.size end def choose_prompt(options = {}) # if there is one or fewer active choices, we won't be able to find a prompt - if self.choices_count - self.inactive_choices_count <= 1 + if self.choices.size - self.inactive_choices_count <= 1 return nil end diff --git a/spec/controllers/questions_controller_spec.rb b/spec/controllers/questions_controller_spec.rb index 59191c6..3e59d05 100644 --- a/spec/controllers/questions_controller_spec.rb +++ b/spec/controllers/questions_controller_spec.rb @@ -40,12 +40,12 @@ describe QuestionsController do describe "GET show normal" do before(:each) do Question.stub!(:find).with("37").and_return(mock_question) - mock_question.stub!(:picked_prompt).and_return(mock_prompt) end it "assigns the requested question as @question" do Question.stub!(:find).with("37").and_return(mock_question) + mock_question.should_receive(:choose_prompt).and_return(mock_prompt) #TODO it shouldn't call this unless we are generating an appearance, right? get :show, :id => "37" @@ -74,6 +74,7 @@ describe QuestionsController do #TODO this is not a particularly intutive param to pass in order to create an appearance it "creates an appearance when a visitor identifier is a param" do + mock_question.should_receive(:choose_prompt).and_return(mock_prompt) get :show, :id => "37", :visitor_identifier => @visitor_identifier assigns[:question].should equal(mock_question) assigns[:p].should equal(mock_prompt) @@ -89,14 +90,10 @@ describe QuestionsController do end describe "calls catchup algorithm" do - before(:each) do - mock_question.should_receive(:send_later).with(:add_prompt_to_queue) - end - #TODO Refactor out to use uses_catchup? it "should pop prompt from cached queue using the catchup algorithm if params dictate" do - mock_question.should_receive(:pop_prompt_queue).and_return(mock_prompt) + mock_question.should_receive(:choose_prompt).with(:algorithm => "catchup").and_return(mock_prompt) get :show, :id => "37", :visitor_identifier => @visitor_identifier, :algorithm => "catchup" assigns[:question].should equal(mock_question) @@ -105,8 +102,7 @@ describe QuestionsController do end it "should handle cache misses gracefully" do - mock_question.should_receive(:pop_prompt_queue).and_return(nil) - mock_question.should_receive(:catchup_choose_prompt).and_return(mock_prompt) + mock_question.should_receive(:choose_prompt).with(:algorithm => "catchup").and_return(mock_prompt) get :show, :id => "37", :visitor_identifier => @visitor_identifier, :algorithm => "catchup" assigns[:question].should equal(mock_question) @@ -117,111 +113,5 @@ describe QuestionsController do end end - # - # describe "GET new" do - # it "assigns a new question as @question" do - # Question.stub!(:new).and_return(mock_question) - # get :new - # assigns[:question].should equal(mock_question) - # end - # end - # - # describe "GET edit" do - # it "assigns the requested question as @question" do - # Question.stub!(:find).with("37").and_return(mock_question) - # get :edit, :id => "37" - # assigns[:question].should equal(mock_question) - # end - # end - # - # describe "POST create" do - # - # describe "with valid params" do - # it "assigns a newly created question as @question" do - # Question.stub!(:new).with({'these' => 'params'}).and_return(mock_question(:save => true)) - # post :create, :question => {:these => 'params'} - # assigns[:question].should equal(mock_question) - # end - # - # it "redirects to the created question" do - # Question.stub!(:new).and_return(mock_question(:save => true)) - # post :create, :question => {} - # response.should redirect_to(question_url(mock_question)) - # end - # end - # - # describe "with invalid params" do - # it "assigns a newly created but unsaved question as @question" do - # Question.stub!(:new).with({'these' => 'params'}).and_return(mock_question(:save => false)) - # post :create, :question => {:these => 'params'} - # assigns[:question].should equal(mock_question) - # end - # - # it "re-renders the 'new' template" do - # Question.stub!(:new).and_return(mock_question(:save => false)) - # post :create, :question => {} - # response.should render_template('new') - # end - # end - # - # end - # - # describe "PUT update" do - # - # describe "with valid params" do - # it "updates the requested question" do - # Question.should_receive(:find).with("37").and_return(mock_question) - # mock_question.should_receive(:update_attributes).with({'these' => 'params'}) - # put :update, :id => "37", :question => {:these => 'params'} - # end - # - # it "assigns the requested question as @question" do - # Question.stub!(:find).and_return(mock_question(:update_attributes => true)) - # put :update, :id => "1" - # assigns[:question].should equal(mock_question) - # end - # - # it "redirects to the question" do - # Question.stub!(:find).and_return(mock_question(:update_attributes => true)) - # put :update, :id => "1" - # response.should redirect_to(question_url(mock_question)) - # end - # end - # - # describe "with invalid params" do - # it "updates the requested question" do - # Question.should_receive(:find).with("37").and_return(mock_question) - # mock_question.should_receive(:update_attributes).with({'these' => 'params'}) - # put :update, :id => "37", :question => {:these => 'params'} - # end - # - # it "assigns the question as @question" do - # Question.stub!(:find).and_return(mock_question(:update_attributes => false)) - # put :update, :id => "1" - # assigns[:question].should equal(mock_question) - # end - # - # it "re-renders the 'edit' template" do - # Question.stub!(:find).and_return(mock_question(:update_attributes => false)) - # put :update, :id => "1" - # response.should render_template('edit') - # end - # end - # - # end - # - # describe "DELETE destroy" do - # it "destroys the requested question" do - # Question.should_receive(:find).with("37").and_return(mock_question) - # mock_question.should_receive(:destroy) - # delete :destroy, :id => "37" - # end - # - # it "redirects to the questions list" do - # Question.stub!(:find).and_return(mock_question(:destroy => true)) - # delete :destroy, :id => "1" - # response.should redirect_to(questions_url) - # end - # end end diff --git a/spec/models/choice_spec.rb b/spec/models/choice_spec.rb index a4caa6d..f6a64d6 100644 --- a/spec/models/choice_spec.rb +++ b/spec/models/choice_spec.rb @@ -24,8 +24,9 @@ describe Choice do end it "should generate prompts after creation" do + prev_choices = @question.choices.size @question.prompts.should_not be_empty - choice1 = Choice.create!(@valid_attributes.merge(:data => '1234')) + proc {choice1 = Choice.create!(@valid_attributes.merge(:data => '1234'))}.should change(@question.prompts, :count).by(prev_choices*2) @question.prompts.should_not be_empty end @@ -34,4 +35,31 @@ describe Choice do choice1.deactivate! choice1.should_not be_active end + + it "should update a question's counter cache on creation" do + @question.choices.size.should == 2 + choice1 = Choice.create!(@valid_attributes.merge(:data => '1234')) + @question.reload + @question.choices.size.should == 3 + end + + it "should update a question's counter cache on activation" do + prev_inactive = @question.inactive_choices_count + choice1 = Choice.create!(@valid_attributes.merge(:data => '1234')) + choice1.deactivate! + @question.reload + @question.inactive_choices_count.should == prev_inactive + 1 + choice1.activate! + @question.reload + @question.inactive_choices_count.should == prev_inactive + choice1.should be_active + end + + it "should update a question's counter cache on deactivation" do + prev_inactive = @question.inactive_choices_count + choice1 = Choice.create!(@valid_attributes.merge(:data => '1234')) + choice1.deactivate! + @question.reload + @question.inactive_choices_count.should == prev_inactive + 1 + end end diff --git a/spec/models/question_spec.rb b/spec/models/question_spec.rb index 411b0e8..360834c 100644 --- a/spec/models/question_spec.rb +++ b/spec/models/question_spec.rb @@ -52,6 +52,15 @@ describe Question do prompt.active?.should == true end + it "should return nil if there is no possible prompt to choose" do + q = @aoi_clone.create_question("foobarbaz", {:name => 'foo'}) + q.choices.first.deactivate! + q.reload + q.choose_prompt.should be_nil + + end + + context "catchup algorithm" do before(:all) do -- libgit2 0.21.2