Created
October 9, 2013 10:58
-
-
Save rwrrll/6899490 to your computer and use it in GitHub Desktop.
Append Rails routes (without losing existing routes)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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