File tree 3 files changed +48
-8
lines changed
3 files changed +48
-8
lines changed Original file line number Diff line number Diff line change @@ -14,6 +14,7 @@ This project adheres to [Semantic Versioning](http://semver.org/) and [Keep a Ch
14
14
### Changes
15
15
16
16
- Optimize named ` *_rfc6570 ` helpers by removing unnecessary indirection
17
+ - Optimize ` #rfc6570_routes ` and ` #rfc6570_route ` helpers
17
18
18
19
### Fixes
19
20
Original file line number Diff line number Diff line change @@ -88,16 +88,11 @@ def to_rfc6570(**opts)
88
88
89
89
module Helper
90
90
def rfc6570_routes ( **opts )
91
- routes = { }
92
- Rails . application . routes . named_routes . names . each do |key |
93
- routes [ key ] = rfc6570_route ( key , **opts )
94
- end
95
-
96
- routes
91
+ _routes . named_routes . to_rfc6570 ( **opts , ctx : self )
97
92
end
98
93
99
94
def rfc6570_route ( name , **opts )
100
- route = Rails . application . routes . named_routes [ name ]
95
+ route = _routes . named_routes [ name ]
101
96
raise KeyError . new "No named routed for `#{ name } '." unless route
102
97
103
98
route . to_rfc6570 ( **opts , ctx : self )
@@ -120,7 +115,7 @@ def rfc6570_params_for(defs)
120
115
121
116
def params_for ( controller , action )
122
117
ctr = "#{ controller . camelize } Controller" . constantize
123
- ctr . rfc6570_defs [ action . to_sym ] if ctr . respond_to? ( :rfc6570_defs )
118
+ ctr . rfc6570_params_for ( action . to_sym ) if ctr . respond_to? ( :rfc6570_params_for )
124
119
rescue NameError
125
120
nil
126
121
end
Original file line number Diff line number Diff line change
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec . describe Rails ::RFC6570 ::Helper do
6
+ subject ( :helper ) do
7
+ routes = self . routes
8
+
9
+ Class . new do
10
+ def url_options
11
+ { host : 'www.example.org' }
12
+ end
13
+
14
+ include routes . url_helpers
15
+ include Rails ::RFC6570 ::Helper
16
+ end . new
17
+ end
18
+
19
+ let ( :routes ) do
20
+ ActionDispatch ::Routing ::RouteSet . new . tap do |routes |
21
+ routes . draw do
22
+ get '/path/:id' , to : 'controller#action' , as : :test1
23
+ end
24
+ end
25
+ end
26
+
27
+ describe '#rfc6570_routes' do
28
+ it 'returns dictionary of all named routes' do
29
+ expect ( helper . rfc6570_routes ) . to eq ( {
30
+ test1 : Addressable ::Template . new ( 'http://www.example.org/path/{id}' ) ,
31
+ } )
32
+ end
33
+ end
34
+
35
+ describe '#rfc6570_route' do
36
+ it 'returns template for named route' do
37
+ expect ( helper . rfc6570_route ( :test1 ) ) . to eq Addressable ::Template . new ( 'http://www.example.org/path/{id}' )
38
+ end
39
+
40
+ it 'raise KeyError with unknown named route' do
41
+ expect { helper . rfc6570_route ( :not_found ) } . to raise_error KeyError
42
+ end
43
+ end
44
+ end
You can’t perform that action at this time.
0 commit comments