From 31cdac9d0ed4feac1c77ca61bf178528e9ea2515 Mon Sep 17 00:00:00 2001 From: Konstantin Haase Date: Sat, 26 Mar 2011 00:22:19 +0100 Subject: [PATCH] make respond-with play nice with namespace --- sinatra-contrib/lib/sinatra/namespace.rb | 5 +++++ sinatra-contrib/lib/sinatra/respond_with.rb | 2 +- sinatra-contrib/spec/respond_with_spec.rb | 14 ++++++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/sinatra-contrib/lib/sinatra/namespace.rb b/sinatra-contrib/lib/sinatra/namespace.rb index 3e846196..8bca9253 100644 --- a/sinatra-contrib/lib/sinatra/namespace.rb +++ b/sinatra-contrib/lib/sinatra/namespace.rb @@ -66,6 +66,11 @@ module Sinatra [*codes].each { |c| errors[c] = block } end + def respond_to(*args) + return @conditions[:provides] || base.respond_to if args.empty? + @conditions[:provides] = args + end + private def app diff --git a/sinatra-contrib/lib/sinatra/respond_with.rb b/sinatra-contrib/lib/sinatra/respond_with.rb index 42f39064..e5a6d122 100644 --- a/sinatra-contrib/lib/sinatra/respond_with.rb +++ b/sinatra-contrib/lib/sinatra/respond_with.rb @@ -108,7 +108,7 @@ module Sinatra result end - def respond_to(*formats, &block) + def respond_to(*formats) if formats.any? @respond_to ||= [] @respond_to.concat formats diff --git a/sinatra-contrib/spec/respond_with_spec.rb b/sinatra-contrib/spec/respond_with_spec.rb index 3b7ee951..e00f060a 100644 --- a/sinatra-contrib/spec/respond_with_spec.rb +++ b/sinatra-contrib/spec/respond_with_spec.rb @@ -262,5 +262,19 @@ describe Sinatra::RespondWith do req('/b', :html).should_not be_ok req('/b', :json).should be_ok end + + it 'plays well with namespaces' do + respond_app do + register Sinatra::Namespace + namespace '/a' do + respond_to :json + get { 'json' } + end + get('/b') { 'anything' } + end + + req('/a', :html).should_not be_ok + req('/b', :html).should be_ok + end end end