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 96 def show
97 97 @question = current_user.questions.find(params[:question_id])
98 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 106 end
101 107  
102 108  
... ...
app/controllers/questions_controller.rb
... ... @@ -23,13 +23,15 @@ class QuestionsController < InheritedResources::Base
23 23 @question_optional_information.each do |key, value|
24 24 optional_information << Proc.new { |options| options[:builder].tag!(key, value)}
25 25 end
  26 + response_options = { :methods => [:item_count], :procs => optional_information }
  27 + response_options[:include] = :versions if params[:version] == "all"
26 28  
27 29 respond_to do |format|
28 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 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 36 end
35 37 end
... ...
spec/controllers/choices_controller_spec.rb
... ... @@ -125,4 +125,25 @@ describe ChoicesController do
125 125 end
126 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 149 end
... ...
spec/controllers/questions_controller_spec.rb
... ... @@ -17,6 +17,7 @@ describe QuestionsController do
17 17 assigns[:question].should == @question
18 18 @response.body.should have_tag("question")
19 19 @response.code.should == "200"
  20 + @response.body.should_not have_tag("versions")
20 21 end
21 22  
22 23  
... ... @@ -32,4 +33,12 @@ describe QuestionsController do
32 33 @response.body.should have_tag("visitor_votes")
33 34 @response.body.should have_tag("visitor_ideas")
34 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 44 end
... ...