Permalink
Browse files

Ensure collections are not treated as nested resources.

  • Loading branch information...
josevalim committed Aug 13, 2009
1 parent 5f7cfff commit 4f9047ecc8f0e2c3d4491bae0051679569da71dc
@@ -226,10 +226,11 @@ def respond_to(*mimes, &block)
# 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
@@ -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
@@ -497,16 +497,20 @@ def using_resource
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
respond_with(Customer.new("david", 13), :location => "http://test.host/", :status => :created)
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 @@ def test_using_resource
@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 @@ def test_using_resource_for_post_with_xml
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 @@ def test_using_resource_with_parent_for_get
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 @@ def test_using_resource_with_parent_for_post
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 @@ def test_using_resource_with_parent_for_post
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 @@ def test_first_in_respond_to_has_higher_priority
@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
@@ -10,12 +10,16 @@ def to_param
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

0 comments on commit 4f9047e

Please sign in to comment.