Skip to content

Commit

Permalink
Ensure collections are not treated as nested resources.
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Aug 13, 2009
1 parent 5f7cfff commit 4f9047e
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 18 deletions.
5 changes: 3 additions & 2 deletions actionpack/lib/action_controller/metal/mime_responds.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -226,10 +226,11 @@ def respond_to(*mimes, &block)
# is quite simple (it just needs to respond to call), you can even give # is quite simple (it just needs to respond to call), you can even give
# a proc to it. # a proc to it.
# #
def respond_with(resource, options={}, &block) def respond_with(*resources, &block)
respond_to(&block) respond_to(&block)
rescue ActionView::MissingTemplate rescue ActionView::MissingTemplate
(options.delete(:responder) || responder).call(self, resource, options) options = resources.extract_options!
(options.delete(:responder) || responder).call(self, resources, options)
end end


def responder def responder
Expand Down
10 changes: 5 additions & 5 deletions actionpack/lib/action_controller/metal/responder.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ module ActionController #:nodoc:
# @project = Project.find(params[:project_id]) # @project = Project.find(params[:project_id])
# @task = @project.comments.build(params[:task]) # @task = @project.comments.build(params[:task])
# flash[:notice] = 'Task was successfully created.' if @task.save # flash[:notice] = 'Task was successfully created.' if @task.save
# respond_with([@project, @task]) # respond_with(@project, @task)
# end # end
# #
# Giving an array of resources, you ensure that the responder will redirect to # Giving an array of resources, you ensure that the responder will redirect to
Expand All @@ -74,19 +74,19 @@ module ActionController #:nodoc:
# polymorphic urls. If a project has one manager which has many tasks, it # polymorphic urls. If a project has one manager which has many tasks, it
# should be invoked as: # should be invoked as:
# #
# respond_with([@project, :manager, @task]) # respond_with(@project, :manager, @task)
# #
# Check polymorphic_url documentation for more examples. # Check polymorphic_url documentation for more examples.
# #
class Responder class Responder
attr_reader :controller, :request, :format, :resource, :resource_location, :options attr_reader :controller, :request, :format, :resource, :resource_location, :options


def initialize(controller, resource, options={}) def initialize(controller, resources, options={})
@controller = controller @controller = controller
@request = controller.request @request = controller.request
@format = controller.formats.first @format = controller.formats.first
@resource = resource.is_a?(Array) ? resource.last : resource @resource = resources.is_a?(Array) ? resources.last : resources
@resource_location = options[:location] || resource @resource_location = options[:location] || resources
@options = options @options = options
end end


Expand Down
27 changes: 20 additions & 7 deletions actionpack/test/controller/mime_responds_test.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -497,16 +497,20 @@ def using_resource
respond_with(Customer.new("david", 13)) respond_with(Customer.new("david", 13))
end end


def using_resource_with_collection
respond_with([Customer.new("david", 13), Customer.new("jamis", 9)])
end

def using_resource_with_parent 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 end


def using_resource_with_status_and_location def using_resource_with_status_and_location
respond_with(Customer.new("david", 13), :location => "http://test.host/", :status => :created) respond_with(Customer.new("david", 13), :location => "http://test.host/", :status => :created)
end end


def using_resource_with_responder 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) respond_with(Customer.new("david", 13), :responder => responder)
end end


Expand Down Expand Up @@ -592,7 +596,7 @@ def test_using_resource
@request.accept = "application/xml" @request.accept = "application/xml"
get :using_resource get :using_resource
assert_equal "application/xml", @response.content_type assert_equal "application/xml", @response.content_type
assert_equal "XML", @response.body assert_equal "<name>david</name>", @response.body


@request.accept = "application/json" @request.accept = "application/json"
assert_raise ActionView::MissingTemplate do assert_raise ActionView::MissingTemplate do
Expand Down Expand Up @@ -622,7 +626,7 @@ def test_using_resource_for_post_with_xml
post :using_resource post :using_resource
assert_equal "application/xml", @response.content_type assert_equal "application/xml", @response.content_type
assert_equal 201, @response.status 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 assert_equal "http://www.example.com/customers/13", @response.location


errors = { :name => :invalid } errors = { :name => :invalid }
Expand Down Expand Up @@ -689,7 +693,7 @@ def test_using_resource_with_parent_for_get
get :using_resource_with_parent get :using_resource_with_parent
assert_equal "application/xml", @response.content_type assert_equal "application/xml", @response.content_type
assert_equal 200, @response.status assert_equal 200, @response.status
assert_equal "XML", @response.body assert_equal "<name>david</name>", @response.body
end end


def test_using_resource_with_parent_for_post def test_using_resource_with_parent_for_post
Expand All @@ -698,7 +702,7 @@ def test_using_resource_with_parent_for_post
post :using_resource_with_parent post :using_resource_with_parent
assert_equal "application/xml", @response.content_type assert_equal "application/xml", @response.content_type
assert_equal 201, @response.status 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 assert_equal "http://www.example.com/quiz_stores/11/customers/13", @response.location


errors = { :name => :invalid } errors = { :name => :invalid }
Expand All @@ -710,6 +714,15 @@ def test_using_resource_with_parent_for_post
assert_nil @response.location assert_nil @response.location
end 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 def test_clear_respond_to
@controller = InheritedRespondWithController.new @controller = InheritedRespondWithController.new
@request.accept = "text/html" @request.accept = "text/html"
Expand All @@ -722,7 +735,7 @@ def test_first_in_respond_to_has_higher_priority
@request.accept = "*/*" @request.accept = "*/*"
get :index get :index
assert_equal "application/xml", @response.content_type assert_equal "application/xml", @response.content_type
assert_equal "XML", @response.body assert_equal "<name>david</name>", @response.body
end end


def test_no_double_render_is_raised def test_no_double_render_is_raised
Expand Down
12 changes: 8 additions & 4 deletions actionpack/test/lib/controller/fake_models.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@ def to_param
id.to_s id.to_s
end end


def to_xml def to_xml(options={})
"XML" if options[:builder]
options[:builder].name name
else
"<name>#{name}</name>"
end
end end


def to_js def to_js(options={})
"JS" "name: #{name.inspect}"
end end


def errors def errors
Expand Down

0 comments on commit 4f9047e

Please sign in to comment.