Commit f408721c8cd9bdc11f7fea841080fef7b7ee2a44

Authored by Luke Baker
2 parents 56556d28 d5eeeaa3

Merge pull request #25 from vitorbaptista/master

Returns all Choice or Questions' versions if requested
app/controllers/choices_controller.rb
@@ -96,7 +96,13 @@ class ChoicesController < InheritedResources::Base @@ -96,7 +96,13 @@ class ChoicesController < InheritedResources::Base
96 def show 96 def show
97 @question = current_user.questions.find(params[:question_id]) 97 @question = current_user.questions.find(params[:question_id])
98 @choice = @question.choices.find(params[:id]) 98 @choice = @question.choices.find(params[:id])
99 - show! 99 + response_options = {}
  100 + response_options[:include] = :versions if params[:version] == 'all'
  101 +
  102 + respond_to do |format|
  103 + format.xml { render :xml => @choice.to_xml(response_options) }
  104 + format.json { render :json => @choice.to_json(response_options) }
  105 + end
100 end 106 end
101 107
102 108
app/controllers/questions_controller.rb
@@ -23,13 +23,15 @@ class QuestionsController < InheritedResources::Base @@ -23,13 +23,15 @@ class QuestionsController < InheritedResources::Base
23 @question_optional_information.each do |key, value| 23 @question_optional_information.each do |key, value|
24 optional_information << Proc.new { |options| options[:builder].tag!(key, value)} 24 optional_information << Proc.new { |options| options[:builder].tag!(key, value)}
25 end 25 end
  26 + response_options = { :methods => [:item_count], :procs => optional_information }
  27 + response_options[:include] = :versions if params[:version] == "all"
26 28
27 respond_to do |format| 29 respond_to do |format|
28 format.xml { 30 format.xml {
29 - render :xml => @question.to_xml(:methods => [:item_count], :procs => optional_information) 31 + render :xml => @question.to_xml(response_options)
30 } 32 }
31 format.js{ 33 format.js{
32 - render :json => @question.to_json(:methods => [:item_count], :procs => optional_information) 34 + render :json => @question.to_json(response_options)
33 } 35 }
34 end 36 end
35 end 37 end
spec/controllers/choices_controller_spec.rb
@@ -125,4 +125,25 @@ describe ChoicesController do @@ -125,4 +125,25 @@ describe ChoicesController do
125 end 125 end
126 end 126 end
127 127
  128 + describe "GET show" do
  129 + it "doesn't returns all versions by default" do
  130 + question = Factory(:question, :site => @user)
  131 + choice = Factory(:choice, :question => question)
  132 +
  133 + get :show, :question_id => question.id, :id => choice.id, :format => "xml"
  134 +
  135 + response.code.should == "200"
  136 + response.body.should_not have_tag("versions")
  137 + end
  138 +
  139 + it "responds with all versions if requested" do
  140 + question = Factory(:question, :site => @user)
  141 + choice = Factory(:choice, :question => question)
  142 +
  143 + get :show, :question_id => question.id, :id => choice.id, :format => "xml", :version => "all"
  144 +
  145 + response.code.should == "200"
  146 + response.body.should have_tag("versions")
  147 + end
  148 + end
128 end 149 end
spec/controllers/questions_controller_spec.rb
@@ -17,6 +17,7 @@ describe QuestionsController do @@ -17,6 +17,7 @@ describe QuestionsController do
17 assigns[:question].should == @question 17 assigns[:question].should == @question
18 @response.body.should have_tag("question") 18 @response.body.should have_tag("question")
19 @response.code.should == "200" 19 @response.code.should == "200"
  20 + @response.body.should_not have_tag("versions")
20 end 21 end
21 22
22 23
@@ -32,4 +33,12 @@ describe QuestionsController do @@ -32,4 +33,12 @@ describe QuestionsController do
32 @response.body.should have_tag("visitor_votes") 33 @response.body.should have_tag("visitor_votes")
33 @response.body.should have_tag("visitor_ideas") 34 @response.body.should have_tag("visitor_ideas")
34 end 35 end
  36 +
  37 + it "responds with all versions if requested" do
  38 + get :show, :id => @question.id, :format => "xml", :version => "all"
  39 +
  40 + assigns[:question].should == @question
  41 + @response.code.should == "200"
  42 + @response.body.should have_tag("versions")
  43 + end
35 end 44 end