Skip to content

Instantly share code, notes, and snippets.

@rwrrll
Created October 9, 2013 10:58
Show Gist options
  • Save rwrrll/6899490 to your computer and use it in GitHub Desktop.
Save rwrrll/6899490 to your computer and use it in GitHub Desktop.
Append Rails routes (without losing existing routes)
# spec/support/append_routes.rb
# Basically this: http://experiments.openhood.com/rails/rails%203/2010/07/20/add-routes-at-runtime-rails-3/
# but in a re-usable block format.
def append_routes(&block)
begin
_routes = Rails.application.routes
_routes.disable_clear_and_finalize = true
_routes.clear!
Rails.application.routes_reloader.paths.each { |path| load(path) }
_routes.draw(&block)
ActiveSupport.on_load(:action_controller) { _routes.finalize! }
ensure
_routes.disable_clear_and_finalize = false
end
end
# spec/probably_controllers/worlds_greatest_spec.rb
# Example usage:
describe "whatever" do
before(:each) do
append_routes do
match '/no_restrictions', :controller => 'some_dummy_controller', :action => 'no_restrictions'
match '/role_is_required', :controller => 'some_dummy_controller', :action => 'role_is_required'
end
end
after(:each) do
Rails.application.reload_routes!
end
# Your tests here
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment