1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Ensure collections are not treated as nested resources.

This commit is contained in:
José Valim 2009-08-13 10:27:41 +02:00
parent 5f7cfffc53
commit 4f9047ecc8
4 changed files with 36 additions and 18 deletions

View file

@ -226,10 +226,11 @@ module ActionController #:nodoc:
# is quite simple (it just needs to respond to call), you can even give
# a proc to it.
#
def respond_with(resource, options={}, &block)
def respond_with(*resources, &block)
respond_to(&block)
rescue ActionView::MissingTemplate
(options.delete(:responder) || responder).call(self, resource, options)
options = resources.extract_options!
(options.delete(:responder) || responder).call(self, resources, options)
end
def responder

View file

@ -64,7 +64,7 @@ module ActionController #:nodoc:
# @project = Project.find(params[:project_id])
# @task = @project.comments.build(params[:task])
# flash[:notice] = 'Task was successfully created.' if @task.save
# respond_with([@project, @task])
# respond_with(@project, @task)
# end
#
# Giving an array of resources, you ensure that the responder will redirect to
@ -74,19 +74,19 @@ module ActionController #:nodoc:
# polymorphic urls. If a project has one manager which has many tasks, it
# should be invoked as:
#
# respond_with([@project, :manager, @task])
# respond_with(@project, :manager, @task)
#
# Check polymorphic_url documentation for more examples.
#
class Responder
attr_reader :controller, :request, :format, :resource, :resource_location, :options
def initialize(controller, resource, options={})
def initialize(controller, resources, options={})
@controller = controller
@request = controller.request
@format = controller.formats.first
@resource = resource.is_a?(Array) ? resource.last : resource
@resource_location = options[:location] || resource
@resource = resources.is_a?(Array) ? resources.last : resources
@resource_location = options[:location] || resources
@options = options
end

View file

@ -497,8 +497,12 @@ class RespondWithController < ActionController::Base
respond_with(Customer.new("david", 13))
end
def using_resource_with_collection
respond_with([Customer.new("david", 13), Customer.new("jamis", 9)])
end
def using_resource_with_parent
respond_with([Quiz::Store.new("developer?", 11), Customer.new("david", 13)])
respond_with(Quiz::Store.new("developer?", 11), Customer.new("david", 13))
end
def using_resource_with_status_and_location
@ -506,7 +510,7 @@ class RespondWithController < ActionController::Base
end
def using_resource_with_responder
responder = proc { |c, r, o| c.render :text => "Resource name is #{r.name}" }
responder = proc { |c, r, o| c.render :text => "Resource name is #{r.first.name}" }
respond_with(Customer.new("david", 13), :responder => responder)
end
@ -592,7 +596,7 @@ class RespondWithControllerTest < ActionController::TestCase
@request.accept = "application/xml"
get :using_resource
assert_equal "application/xml", @response.content_type
assert_equal "XML", @response.body
assert_equal "<name>david</name>", @response.body
@request.accept = "application/json"
assert_raise ActionView::MissingTemplate do
@ -622,7 +626,7 @@ class RespondWithControllerTest < ActionController::TestCase
post :using_resource
assert_equal "application/xml", @response.content_type
assert_equal 201, @response.status
assert_equal "XML", @response.body
assert_equal "<name>david</name>", @response.body
assert_equal "http://www.example.com/customers/13", @response.location
errors = { :name => :invalid }
@ -689,7 +693,7 @@ class RespondWithControllerTest < ActionController::TestCase
get :using_resource_with_parent
assert_equal "application/xml", @response.content_type
assert_equal 200, @response.status
assert_equal "XML", @response.body
assert_equal "<name>david</name>", @response.body
end
def test_using_resource_with_parent_for_post
@ -698,7 +702,7 @@ class RespondWithControllerTest < ActionController::TestCase
post :using_resource_with_parent
assert_equal "application/xml", @response.content_type
assert_equal 201, @response.status
assert_equal "XML", @response.body
assert_equal "<name>david</name>", @response.body
assert_equal "http://www.example.com/quiz_stores/11/customers/13", @response.location
errors = { :name => :invalid }
@ -710,6 +714,15 @@ class RespondWithControllerTest < ActionController::TestCase
assert_nil @response.location
end
def test_using_resource_with_collection
@request.accept = "application/xml"
get :using_resource_with_collection
assert_equal "application/xml", @response.content_type
assert_equal 200, @response.status
assert_match /<name>david<\/name>/, @response.body
assert_match /<name>jamis<\/name>/, @response.body
end
def test_clear_respond_to
@controller = InheritedRespondWithController.new
@request.accept = "text/html"
@ -722,7 +735,7 @@ class RespondWithControllerTest < ActionController::TestCase
@request.accept = "*/*"
get :index
assert_equal "application/xml", @response.content_type
assert_equal "XML", @response.body
assert_equal "<name>david</name>", @response.body
end
def test_no_double_render_is_raised

View file

@ -10,12 +10,16 @@ class Customer < Struct.new(:name, :id)
id.to_s
end
def to_xml
"XML"
def to_xml(options={})
if options[:builder]
options[:builder].name name
else
"<name>#{name}</name>"
end
end
def to_js
"JS"
def to_js(options={})
"name: #{name.inspect}"
end
def errors