Skip to content
This repository
Browse code

Implemented RouteSet#default_scope, which allows to set the scope for…

… the entire routes object
  • Loading branch information...
commit b8d6dc3c84321c751ab2ca8232e1e3fb332a844c 1 parent b43b686
Piotr Sarnacki drogus authored
6 actionpack/lib/action_dispatch/routing/mapper.rb
@@ -274,6 +274,12 @@ def default_url_options=(options)
274 274 end
275 275 alias_method :default_url_options, :default_url_options=
276 276
  277 + def with_default_scope(scope, &block)
  278 + scope(scope) do
  279 + instance_exec(&block)
  280 + end
  281 + end
  282 +
277 283 private
278 284 def app_name(app)
279 285 return unless app.respond_to?(:routes)
8 actionpack/lib/action_dispatch/routing/route_set.rb
@@ -199,7 +199,7 @@ def #{selector}(*args)
199 199 end
200 200 end
201 201
202   - attr_accessor :set, :routes, :named_routes
  202 + attr_accessor :set, :routes, :named_routes, :default_scope
203 203 attr_accessor :disable_clear_and_finalize, :resources_path_names
204 204 attr_accessor :default_url_options, :request_class, :valid_conditions
205 205
@@ -230,7 +230,11 @@ def draw(&block)
230 230 if block.arity == 1
231 231 mapper.instance_exec(DeprecatedMapper.new(self), &block)
232 232 else
233   - mapper.instance_exec(&block)
  233 + if default_scope
  234 + mapper.with_default_scope(default_scope, &block)
  235 + else
  236 + mapper.instance_exec(&block)
  237 + end
234 238 end
235 239
236 240 finalize! unless @disable_clear_and_finalize
29 actionpack/test/dispatch/routing_test.rb
@@ -2151,3 +2151,32 @@ def expected_redirect_body(url)
2151 2151 %(<html><body>You are being <a href="#{ERB::Util.h(url)}">redirected</a>.</body></html>)
2152 2152 end
2153 2153 end
  2154 +
  2155 +
  2156 +class TestDefaultScope < ActionController::IntegrationTest
  2157 + module ::Blog
  2158 + class PostsController < ActionController::Base
  2159 + def index
  2160 + render :text => "blog/posts#index"
  2161 + end
  2162 + end
  2163 + end
  2164 +
  2165 + DefaultScopeRoutes = ActionDispatch::Routing::RouteSet.new
  2166 + DefaultScopeRoutes.default_scope = {:module => :blog}
  2167 + DefaultScopeRoutes.draw do
  2168 + resources :posts
  2169 + end
  2170 +
  2171 + def app
  2172 + DefaultScopeRoutes
  2173 + end
  2174 +
  2175 + include DefaultScopeRoutes.url_helpers
  2176 +
  2177 + def test_default_scope
  2178 + get '/posts'
  2179 + assert_equal "blog/posts#index", @response.body
  2180 + end
  2181 +end
  2182 +

0 comments on commit b8d6dc3

Please sign in to comment.
Something went wrong with that request. Please try again.