actionpack-direct_routes 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.md +39 -0
  4. data/Rakefile +34 -0
  5. data/lib/action_dispatch/routing/direct_routes/mapper.rb +17 -0
  6. data/lib/action_dispatch/routing/direct_routes/mapper/scope.rb +15 -0
  7. data/lib/action_dispatch/routing/direct_routes/route_set.rb +89 -0
  8. data/lib/action_dispatch/routing/direct_routes/route_set/custom_url_helper.rb +39 -0
  9. data/lib/action_dispatch/routing/direct_routes/route_set/named_route_collection.rb +36 -0
  10. data/lib/actionpack/direct_routes.rb +15 -0
  11. data/lib/actionpack/direct_routes/version.rb +5 -0
  12. data/test/application/routing_test.rb +206 -0
  13. data/test/dummy/README.rdoc +28 -0
  14. data/test/dummy/Rakefile +6 -0
  15. data/test/dummy/app/assets/javascripts/application.js +13 -0
  16. data/test/dummy/app/assets/stylesheets/application.css +15 -0
  17. data/test/dummy/app/controllers/application_controller.rb +5 -0
  18. data/test/dummy/app/helpers/application_helper.rb +2 -0
  19. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  20. data/test/dummy/bin/bundle +3 -0
  21. data/test/dummy/bin/rails +4 -0
  22. data/test/dummy/bin/rake +4 -0
  23. data/test/dummy/bin/setup +29 -0
  24. data/test/dummy/config.ru +4 -0
  25. data/test/dummy/config/application.rb +26 -0
  26. data/test/dummy/config/boot.rb +5 -0
  27. data/test/dummy/config/database.yml +25 -0
  28. data/test/dummy/config/environment.rb +5 -0
  29. data/test/dummy/config/environments/development.rb +41 -0
  30. data/test/dummy/config/environments/production.rb +79 -0
  31. data/test/dummy/config/environments/test.rb +42 -0
  32. data/test/dummy/config/initializers/assets.rb +11 -0
  33. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  34. data/test/dummy/config/initializers/cookies_serializer.rb +3 -0
  35. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  36. data/test/dummy/config/initializers/inflections.rb +16 -0
  37. data/test/dummy/config/initializers/mime_types.rb +4 -0
  38. data/test/dummy/config/initializers/session_store.rb +3 -0
  39. data/test/dummy/config/initializers/to_time_preserves_timezone.rb +10 -0
  40. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  41. data/test/dummy/config/locales/en.yml +23 -0
  42. data/test/dummy/config/routes.rb +56 -0
  43. data/test/dummy/config/secrets.yml +22 -0
  44. data/test/dummy/db/test.sqlite3 +0 -0
  45. data/test/dummy/log/test.log +690 -0
  46. data/test/dummy/public/404.html +67 -0
  47. data/test/dummy/public/422.html +67 -0
  48. data/test/dummy/public/500.html +66 -0
  49. data/test/dummy/public/favicon.ico +0 -0
  50. data/test/integration/custom_url_helpers_test.rb +238 -0
  51. data/test/test_helper.rb +20 -0
  52. metadata +219 -0
@@ -0,0 +1,67 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The page you were looking for doesn't exist (404)</title>
5
+ <meta name="viewport" content="width=device-width,initial-scale=1">
6
+ <style>
7
+ body {
8
+ background-color: #EFEFEF;
9
+ color: #2E2F30;
10
+ text-align: center;
11
+ font-family: arial, sans-serif;
12
+ margin: 0;
13
+ }
14
+
15
+ div.dialog {
16
+ width: 95%;
17
+ max-width: 33em;
18
+ margin: 4em auto 0;
19
+ }
20
+
21
+ div.dialog > div {
22
+ border: 1px solid #CCC;
23
+ border-right-color: #999;
24
+ border-left-color: #999;
25
+ border-bottom-color: #BBB;
26
+ border-top: #B00100 solid 4px;
27
+ border-top-left-radius: 9px;
28
+ border-top-right-radius: 9px;
29
+ background-color: white;
30
+ padding: 7px 12% 0;
31
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
32
+ }
33
+
34
+ h1 {
35
+ font-size: 100%;
36
+ color: #730E15;
37
+ line-height: 1.5em;
38
+ }
39
+
40
+ div.dialog > p {
41
+ margin: 0 0 1em;
42
+ padding: 1em;
43
+ background-color: #F7F7F7;
44
+ border: 1px solid #CCC;
45
+ border-right-color: #999;
46
+ border-left-color: #999;
47
+ border-bottom-color: #999;
48
+ border-bottom-left-radius: 4px;
49
+ border-bottom-right-radius: 4px;
50
+ border-top-color: #DADADA;
51
+ color: #666;
52
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
53
+ }
54
+ </style>
55
+ </head>
56
+
57
+ <body>
58
+ <!-- This file lives in public/404.html -->
59
+ <div class="dialog">
60
+ <div>
61
+ <h1>The page you were looking for doesn't exist.</h1>
62
+ <p>You may have mistyped the address or the page may have moved.</p>
63
+ </div>
64
+ <p>If you are the application owner check the logs for more information.</p>
65
+ </div>
66
+ </body>
67
+ </html>
@@ -0,0 +1,67 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The change you wanted was rejected (422)</title>
5
+ <meta name="viewport" content="width=device-width,initial-scale=1">
6
+ <style>
7
+ body {
8
+ background-color: #EFEFEF;
9
+ color: #2E2F30;
10
+ text-align: center;
11
+ font-family: arial, sans-serif;
12
+ margin: 0;
13
+ }
14
+
15
+ div.dialog {
16
+ width: 95%;
17
+ max-width: 33em;
18
+ margin: 4em auto 0;
19
+ }
20
+
21
+ div.dialog > div {
22
+ border: 1px solid #CCC;
23
+ border-right-color: #999;
24
+ border-left-color: #999;
25
+ border-bottom-color: #BBB;
26
+ border-top: #B00100 solid 4px;
27
+ border-top-left-radius: 9px;
28
+ border-top-right-radius: 9px;
29
+ background-color: white;
30
+ padding: 7px 12% 0;
31
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
32
+ }
33
+
34
+ h1 {
35
+ font-size: 100%;
36
+ color: #730E15;
37
+ line-height: 1.5em;
38
+ }
39
+
40
+ div.dialog > p {
41
+ margin: 0 0 1em;
42
+ padding: 1em;
43
+ background-color: #F7F7F7;
44
+ border: 1px solid #CCC;
45
+ border-right-color: #999;
46
+ border-left-color: #999;
47
+ border-bottom-color: #999;
48
+ border-bottom-left-radius: 4px;
49
+ border-bottom-right-radius: 4px;
50
+ border-top-color: #DADADA;
51
+ color: #666;
52
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
53
+ }
54
+ </style>
55
+ </head>
56
+
57
+ <body>
58
+ <!-- This file lives in public/422.html -->
59
+ <div class="dialog">
60
+ <div>
61
+ <h1>The change you wanted was rejected.</h1>
62
+ <p>Maybe you tried to change something you didn't have access to.</p>
63
+ </div>
64
+ <p>If you are the application owner check the logs for more information.</p>
65
+ </div>
66
+ </body>
67
+ </html>
@@ -0,0 +1,66 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>We're sorry, but something went wrong (500)</title>
5
+ <meta name="viewport" content="width=device-width,initial-scale=1">
6
+ <style>
7
+ body {
8
+ background-color: #EFEFEF;
9
+ color: #2E2F30;
10
+ text-align: center;
11
+ font-family: arial, sans-serif;
12
+ margin: 0;
13
+ }
14
+
15
+ div.dialog {
16
+ width: 95%;
17
+ max-width: 33em;
18
+ margin: 4em auto 0;
19
+ }
20
+
21
+ div.dialog > div {
22
+ border: 1px solid #CCC;
23
+ border-right-color: #999;
24
+ border-left-color: #999;
25
+ border-bottom-color: #BBB;
26
+ border-top: #B00100 solid 4px;
27
+ border-top-left-radius: 9px;
28
+ border-top-right-radius: 9px;
29
+ background-color: white;
30
+ padding: 7px 12% 0;
31
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
32
+ }
33
+
34
+ h1 {
35
+ font-size: 100%;
36
+ color: #730E15;
37
+ line-height: 1.5em;
38
+ }
39
+
40
+ div.dialog > p {
41
+ margin: 0 0 1em;
42
+ padding: 1em;
43
+ background-color: #F7F7F7;
44
+ border: 1px solid #CCC;
45
+ border-right-color: #999;
46
+ border-left-color: #999;
47
+ border-bottom-color: #999;
48
+ border-bottom-left-radius: 4px;
49
+ border-bottom-right-radius: 4px;
50
+ border-top-color: #DADADA;
51
+ color: #666;
52
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
53
+ }
54
+ </style>
55
+ </head>
56
+
57
+ <body>
58
+ <!-- This file lives in public/500.html -->
59
+ <div class="dialog">
60
+ <div>
61
+ <h1>We're sorry, but something went wrong.</h1>
62
+ </div>
63
+ <p>If you are the application owner check the logs for more information.</p>
64
+ </div>
65
+ </body>
66
+ </html>
File without changes
@@ -0,0 +1,238 @@
1
+ require "test_helper"
2
+
3
+ class CustomUrlHelpersTest < ActionDispatch::IntegrationTest
4
+ class Linkable
5
+ attr_reader :id
6
+
7
+ def self.name
8
+ super.demodulize
9
+ end
10
+
11
+ def initialize(id)
12
+ @id = id
13
+ end
14
+
15
+ def linkable_type
16
+ self.class.name.underscore
17
+ end
18
+ end
19
+
20
+ class Category < Linkable; end
21
+ class Collection < Linkable; end
22
+ class Product < Linkable; end
23
+
24
+ class Model
25
+ extend ActiveModel::Naming
26
+ include ActiveModel::Conversion
27
+
28
+ attr_reader :id
29
+
30
+ def initialize(id = nil)
31
+ @id = id
32
+ end
33
+
34
+ remove_method :model_name
35
+ def model_name
36
+ @_model_name ||= ActiveModel::Name.new(self.class, nil, self.class.name.demodulize)
37
+ end
38
+
39
+ def persisted?
40
+ false
41
+ end
42
+ end
43
+
44
+ class Basket < Model; end
45
+ class User < Model; end
46
+
47
+ class Article
48
+ attr_reader :id
49
+
50
+ def self.name
51
+ "Article"
52
+ end
53
+
54
+ def initialize(id)
55
+ @id = id
56
+ end
57
+ end
58
+
59
+ class Page
60
+ attr_reader :id
61
+
62
+ def self.name
63
+ super.demodulize
64
+ end
65
+
66
+ def initialize(id)
67
+ @id = id
68
+ end
69
+ end
70
+
71
+ class RoutedRackApp
72
+ attr_reader :routes
73
+
74
+ def initialize(routes)
75
+ @routes = routes
76
+ @stack = ActionDispatch::MiddlewareStack.new.build(@routes)
77
+ end
78
+
79
+ def call(env)
80
+ @stack.call(env)
81
+ end
82
+ end
83
+
84
+ Routes = ActionDispatch::Routing::RouteSet.new
85
+ Routes.draw do
86
+ default_url_options host: "www.example.com"
87
+
88
+ root to: "pages#index"
89
+ get "/basket", to: "basket#show", as: :basket
90
+ get "/posts/:id", to: "posts#show", as: :post
91
+ get "/profile", to: "users#profile", as: :profile
92
+ get "/media/:id", to: "media#show", as: :media
93
+ get "/pages/:id", to: "pages#show", as: :page
94
+
95
+ resources :categories, :collections, :products
96
+
97
+ namespace :admin do
98
+ get "/dashboard", to: "dashboard#index"
99
+ end
100
+
101
+ direct(:website) { "http://www.rubyonrails.org" }
102
+ direct("string") { "http://www.rubyonrails.org" }
103
+ direct(:helper) { basket_url }
104
+ direct(:linkable) { |linkable| [:"#{linkable.linkable_type}", { id: linkable.id }] }
105
+ direct(:params) { |params| params }
106
+ direct(:symbol) { :basket }
107
+ direct(:hash) { { controller: "basket", action: "show" } }
108
+ direct(:array) { [:admin, :dashboard] }
109
+ direct(:options) { |options| [:products, options] }
110
+ direct(:defaults, size: 10) { |options| [:products, options] }
111
+
112
+ direct(:browse, page: 1, size: 10) do |options|
113
+ [:products, options.merge(params.permit(:page, :size).to_h.symbolize_keys)]
114
+ end
115
+ end
116
+
117
+ APP = RoutedRackApp.new(Routes)
118
+ def app
119
+ APP
120
+ end
121
+
122
+ include Routes.url_helpers
123
+
124
+ def setup
125
+ @category = Category.new("1")
126
+ @collection = Collection.new("2")
127
+ @product = Product.new("3")
128
+ @basket = Basket.new
129
+ @user = User.new
130
+ @page = Page.new("6")
131
+ @path_params = { "controller" => "pages", "action" => "index" }
132
+ @unsafe_params = ActionController::Parameters.new(@path_param)
133
+ @safe_params = ActionController::Parameters.new(@path_params).permit(:controller, :action)
134
+ end
135
+
136
+ def params
137
+ ActionController::Parameters.new(page: 2, size: 25)
138
+ end
139
+
140
+ def test_direct_paths
141
+ assert_equal "/", website_path
142
+ assert_equal "/", Routes.url_helpers.website_path
143
+
144
+ assert_equal "/", string_path
145
+ assert_equal "/", Routes.url_helpers.string_path
146
+
147
+ assert_equal "/basket", helper_path
148
+ assert_equal "/basket", Routes.url_helpers.helper_path
149
+
150
+ assert_equal "/categories/1", linkable_path(@category)
151
+ assert_equal "/categories/1", Routes.url_helpers.linkable_path(@category)
152
+ assert_equal "/collections/2", linkable_path(@collection)
153
+ assert_equal "/collections/2", Routes.url_helpers.linkable_path(@collection)
154
+ assert_equal "/products/3", linkable_path(@product)
155
+ assert_equal "/products/3", Routes.url_helpers.linkable_path(@product)
156
+
157
+ assert_equal "/", params_path(@safe_params)
158
+ assert_equal "/", Routes.url_helpers.params_path(@safe_params)
159
+ assert_raises(ActionController::UrlGenerationError) { params_path(@unsafe_params) }
160
+ assert_raises(ActionController::UrlGenerationError) { Routes.url_helpers.params_path(@unsafe_params) }
161
+
162
+ assert_equal "/basket", symbol_path
163
+ assert_equal "/basket", Routes.url_helpers.symbol_path
164
+ assert_equal "/basket", hash_path
165
+ assert_equal "/basket", Routes.url_helpers.hash_path
166
+ assert_equal "/admin/dashboard", array_path
167
+ assert_equal "/admin/dashboard", Routes.url_helpers.array_path
168
+
169
+ assert_equal "/products?page=2", options_path(page: 2)
170
+ assert_equal "/products?page=2", Routes.url_helpers.options_path(page: 2)
171
+ assert_equal "/products?size=10", defaults_path
172
+ assert_equal "/products?size=10", Routes.url_helpers.defaults_path
173
+ assert_equal "/products?size=20", defaults_path(size: 20)
174
+ assert_equal "/products?size=20", Routes.url_helpers.defaults_path(size: 20)
175
+
176
+ assert_equal "/products?page=2&size=25", browse_path
177
+ assert_raises(NameError) { Routes.url_helpers.browse_path }
178
+ end
179
+
180
+ def test_direct_urls
181
+ assert_equal "http://www.rubyonrails.org", website_url
182
+ assert_equal "http://www.rubyonrails.org", Routes.url_helpers.website_url
183
+
184
+ assert_equal "http://www.rubyonrails.org", string_url
185
+ assert_equal "http://www.rubyonrails.org", Routes.url_helpers.string_url
186
+
187
+ assert_equal "http://www.example.com/basket", helper_url
188
+ assert_equal "http://www.example.com/basket", Routes.url_helpers.helper_url
189
+
190
+ assert_equal "http://www.example.com/categories/1", linkable_url(@category)
191
+ assert_equal "http://www.example.com/categories/1", Routes.url_helpers.linkable_url(@category)
192
+ assert_equal "http://www.example.com/collections/2", linkable_url(@collection)
193
+ assert_equal "http://www.example.com/collections/2", Routes.url_helpers.linkable_url(@collection)
194
+ assert_equal "http://www.example.com/products/3", linkable_url(@product)
195
+ assert_equal "http://www.example.com/products/3", Routes.url_helpers.linkable_url(@product)
196
+
197
+ assert_equal "http://www.example.com/", params_url(@safe_params)
198
+ assert_equal "http://www.example.com/", Routes.url_helpers.params_url(@safe_params)
199
+ assert_raises(ActionController::UrlGenerationError) { params_url(@unsafe_params) }
200
+ assert_raises(ActionController::UrlGenerationError) { Routes.url_helpers.params_url(@unsafe_params) }
201
+
202
+ assert_equal "http://www.example.com/basket", symbol_url
203
+ assert_equal "http://www.example.com/basket", Routes.url_helpers.symbol_url
204
+ assert_equal "http://www.example.com/basket", hash_url
205
+ assert_equal "http://www.example.com/basket", Routes.url_helpers.hash_url
206
+ assert_equal "http://www.example.com/admin/dashboard", array_url
207
+ assert_equal "http://www.example.com/admin/dashboard", Routes.url_helpers.array_url
208
+
209
+ assert_equal "http://www.example.com/products?page=2", options_url(page: 2)
210
+ assert_equal "http://www.example.com/products?page=2", Routes.url_helpers.options_url(page: 2)
211
+ assert_equal "http://www.example.com/products?size=10", defaults_url
212
+ assert_equal "http://www.example.com/products?size=10", Routes.url_helpers.defaults_url
213
+ assert_equal "http://www.example.com/products?size=20", defaults_url(size: 20)
214
+ assert_equal "http://www.example.com/products?size=20", Routes.url_helpers.defaults_url(size: 20)
215
+
216
+ assert_equal "http://www.example.com/products?page=2&size=25", browse_url
217
+ assert_raises(NameError) { Routes.url_helpers.browse_url }
218
+ end
219
+
220
+
221
+ def test_defining_direct_inside_a_scope_raises_runtime_error
222
+ routes = ActionDispatch::Routing::RouteSet.new
223
+
224
+ assert_raises RuntimeError do
225
+ routes.draw do
226
+ namespace :admin do
227
+ direct(:rubyonrails) { "http://www.rubyonrails.org" }
228
+ end
229
+ end
230
+ end
231
+ end
232
+
233
+ def test_defining_direct_url_registers_helper_method
234
+ assert_equal "http://www.example.com/basket", Routes.url_helpers.symbol_url
235
+ assert_equal true, Routes.named_routes.route_defined?(:symbol_url), "'symbol_url' named helper not found"
236
+ assert_equal true, Routes.named_routes.route_defined?(:symbol_path), "'symbol_path' named helper not found"
237
+ end
238
+ end
@@ -0,0 +1,20 @@
1
+ # Configure Rails Environment
2
+ ENV["RAILS_ENV"] = "test"
3
+
4
+ require File.expand_path("../../test/dummy/config/environment.rb", __FILE__)
5
+ ActiveRecord::Migrator.migrations_paths = [File.expand_path("../../test/dummy/db/migrate", __FILE__)]
6
+ require "rails/test_help"
7
+
8
+ # Filter out Minitest backtrace while allowing backtrace from other libraries
9
+ # to be shown.
10
+ Minitest.backtrace_filter = Minitest::BacktraceFilter.new
11
+
12
+ # Load support files
13
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
14
+
15
+ # Load fixtures from the engine
16
+ if ActiveSupport::TestCase.respond_to?(:fixture_path=)
17
+ ActiveSupport::TestCase.fixture_path = File.expand_path("../fixtures", __FILE__)
18
+ ActionDispatch::IntegrationTest.fixture_path = ActiveSupport::TestCase.fixture_path
19
+ ActiveSupport::TestCase.fixtures :all
20
+ end