diff options
author | Andrew White <andrew.white@unboxed.co> | 2017-02-22 21:22:22 +0000 |
---|---|---|
committer | Andrew White <andrew.white@unboxed.co> | 2017-02-22 21:22:22 +0000 |
commit | 630e709ea6cb535b45a9cbd89c08c572646f5608 (patch) | |
tree | 5478b9f0bd4a464195bc78e6e6f9147bcd6e102e /actionpack/test | |
parent | a5b83123177b41a1b9cc7fabe89e408228efcd19 (diff) | |
download | rails-630e709ea6cb535b45a9cbd89c08c572646f5608.tar.gz rails-630e709ea6cb535b45a9cbd89c08c572646f5608.tar.bz2 rails-630e709ea6cb535b45a9cbd89c08c572646f5608.zip |
Clarify use of params in `direct`
Since a `direct` url helper block is evaluated using `instance_exec`
then methods that are available in the instance context can be
accessed, e.g. the params object in a controller action or view.
This wasn't clear from the example so expand on that point and add
a test case for this situation.
Diffstat (limited to 'actionpack/test')
-rw-r--r-- | actionpack/test/dispatch/routing/custom_url_helpers_test.rb | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/actionpack/test/dispatch/routing/custom_url_helpers_test.rb b/actionpack/test/dispatch/routing/custom_url_helpers_test.rb index 6d230a2557..f85b989892 100644 --- a/actionpack/test/dispatch/routing/custom_url_helpers_test.rb +++ b/actionpack/test/dispatch/routing/custom_url_helpers_test.rb @@ -96,6 +96,10 @@ class TestCustomUrlHelpers < ActionDispatch::IntegrationTest direct(:options) { |options| [:products, options] } direct(:defaults, size: 10) { |options| [:products, options] } + direct(:browse, page: 1, size: 10) do |options| + [:products, options.merge(params.permit(:page, :size).to_h.symbolize_keys)] + end + resolve("Article") { |article| [:post, { id: article.id }] } resolve("Basket") { |basket| [:basket] } resolve("User", anchor: "details") { |user, options| [:profile, options] } @@ -127,6 +131,10 @@ class TestCustomUrlHelpers < ActionDispatch::IntegrationTest @safe_params = ActionController::Parameters.new(@path_params).permit(:controller, :action) end + def params + ActionController::Parameters.new(page: 2, size: 25) + end + def test_direct_paths assert_equal "http://www.rubyonrails.org", website_path assert_equal "http://www.rubyonrails.org", Routes.url_helpers.website_path @@ -162,6 +170,9 @@ class TestCustomUrlHelpers < ActionDispatch::IntegrationTest assert_equal "/products?size=10", Routes.url_helpers.defaults_path assert_equal "/products?size=20", defaults_path(size: 20) assert_equal "/products?size=20", Routes.url_helpers.defaults_path(size: 20) + + assert_equal "/products?page=2&size=25", browse_path + assert_raises(NameError) { Routes.url_helpers.browse_path } end def test_direct_urls @@ -199,6 +210,9 @@ class TestCustomUrlHelpers < ActionDispatch::IntegrationTest assert_equal "http://www.example.com/products?size=10", Routes.url_helpers.defaults_url assert_equal "http://www.example.com/products?size=20", defaults_url(size: 20) assert_equal "http://www.example.com/products?size=20", Routes.url_helpers.defaults_url(size: 20) + + assert_equal "http://www.example.com/products?page=2&size=25", browse_url + assert_raises(NameError) { Routes.url_helpers.browse_url } end def test_resolve_paths |