Permalink
Browse files

Nested children can redirect to their parents now.

  • Loading branch information...
1 parent ce354b1 commit bb0f29666124c84f3b512efb751e72693908aa57 @nakajima committed Feb 5, 2009
Showing with 32 additions and 12 deletions.
  1. +12 −7 lib/sinatras-hat/response.rb
  2. +20 −5 spec/response_spec.rb
@@ -6,7 +6,7 @@ module Hat
class Response
attr_reader :maker
- delegate :resource_path, :to => :maker
+ delegate :model, :resource_path, :to => :maker
def initialize(maker, request)
@maker = maker
@@ -26,6 +26,14 @@ def redirect(*args)
@request.redirect url_for(*args)
end
+ def url_for(resource, *args)
+ case resource
+ when String then resource
+ when Symbol then resource_path(Maker.actions[resource][:path], *args)
+ else maker_for(resource).resource_path('/:id', resource)
+ end
+ end
+
private
def no_template!(msg)
@@ -42,12 +50,9 @@ def views
end
end
- def url_for(resource, *args)
- case resource
- when String then resource
- when Symbol then resource_path(Maker.actions[resource][:path], *args)
- else resource_path('/:id', resource)
- end
+ def maker_for(record)
+ resource = record.is_a?(model.klass) ? maker : maker.parents.detect { |m| record.is_a?(m.model.klass) }
+ resource || maker
end
end
end
View
@@ -5,7 +5,7 @@ class Article; end
describe Sinatra::Hat::Response do
attr_reader :maker, :response, :request
- def new_response
+ def new_response(maker=@maker)
Sinatra::Hat::Response.new(maker, request)
end
@@ -65,15 +65,30 @@ def new_response
end
end
- context "when passed some data" do
+ context "when passed a record" do
before(:each) do
stub(@article = Article.new).id { 2 }
end
- it "redirects to the resource path for that data" do
- mock(request).redirect("/articles/2")
- new_response.redirect(@article)
+ context "when passed a record for the current maker" do
+ it "redirects to the resource path for that data" do
+ mock(request).redirect("/articles/2")
+ new_response.redirect(@article)
+ end
end
+
+ context "when passed a record for the current maker" do
+ before(:each) do
+ @article.save!
+ @comment = @article.comments.create!
+ @child_maker = new_maker(Comment, :parent => @maker)
+ end
+
+ it "redirects to the resource path for that data" do
+ mock(request).redirect("/articles/#{@article.to_param}")
+ new_response(@child_maker).redirect(@article)
+ end
+ end
end
context "when passed a symbol" do

0 comments on commit bb0f296

Please sign in to comment.