routing-filter 0.6.3 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/README.markdown +8 -9
- data/lib/routing_filter/adapters/rails.rb +8 -13
- data/lib/routing_filter/filter.rb +5 -7
- data/lib/routing_filter/filters/extension.rb +10 -11
- data/lib/routing_filter/filters/locale.rb +3 -4
- data/lib/routing_filter/filters/pagination.rb +1 -1
- data/lib/routing_filter/filters/uuid.rb +2 -2
- data/lib/routing_filter/result_wrapper.rb +24 -0
- data/lib/routing_filter/version.rb +1 -1
- metadata +15 -14
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 545f70e3cf86cd8a929b1cdad7d7b69709d4b39a8bd492d92c7de3bdb1b7e33d
|
4
|
+
data.tar.gz: 1f558537658aad99dd8399e4ed366c169df2f4b26339bb420e9615e39ff28639
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1d33b7c6b011e74c14372a05553b608d1beccbb0637fea654e011d0969a77b30a7c119438108545caa7185990347a4c8b49057a784c968386b47b95d4de2812b
|
7
|
+
data.tar.gz: 535eb1baa68413efa08e41c30256ebfca6e76b8eaa6807e533851a08028ca6269d79d9e3b31d7f17c53a17d60e2ba3db13057caf4a80c5bf10d877b9d4b81465
|
data/CHANGELOG.md
CHANGED
data/README.markdown
CHANGED
@@ -92,7 +92,7 @@ RoutingFilter.active = false
|
|
92
92
|
|
93
93
|
## Filter order
|
94
94
|
|
95
|
-
You can picture the way routing-filter wraps filters around your application as a russian puppet pattern. Your application sits in the center and is wrapped by a number of filters. An incoming request's path will be
|
95
|
+
You can picture the way routing-filter wraps filters around your application as a russian puppet pattern. Your application sits in the center and is wrapped by a number of filters. An incoming request's path will be passed through these layers of filters from the outside in until it is passed to the regular application routes set. When you generate URLs on the other hand then the filters will be run from the inside out.
|
96
96
|
|
97
97
|
Filter order might be confusing at first. The reason for that is that the way rack/mount (which is used by Rails as a core routing engine) is confusing in this respect and Rails tries to make the best of it.
|
98
98
|
|
@@ -108,7 +108,7 @@ This way common base filters (such as the locale filter) can run first and do no
|
|
108
108
|
For example implementations have a look at the existing filters in
|
109
109
|
[lib/routing_filter/filters](http://github.com/svenfuchs/routing-filter/tree/master/lib/routing_filter/filters)
|
110
110
|
|
111
|
-
The following would be a
|
111
|
+
The following would be a skeleton of an empty filter:
|
112
112
|
|
113
113
|
```ruby
|
114
114
|
module RoutingFilter
|
@@ -128,16 +128,15 @@ module RoutingFilter
|
|
128
128
|
# Make sure to yield (calls the next around filter if present and
|
129
129
|
# eventually `url_for` on the controller):
|
130
130
|
yield.tap do |result|
|
131
|
-
# You can change the generated url_or_path here
|
132
|
-
#
|
133
|
-
# and friends).
|
131
|
+
# You can change the generated url_or_path here by calling `result.update(url)`.
|
132
|
+
# The current url is available via `result.url`
|
134
133
|
end
|
135
134
|
end
|
136
135
|
end
|
137
136
|
end
|
138
137
|
```
|
139
138
|
|
140
|
-
You can specify the filter
|
139
|
+
You can specify the filter explicitly in your routes.rb:
|
141
140
|
|
142
141
|
```ruby
|
143
142
|
Rails.application.routes.draw do
|
@@ -154,12 +153,12 @@ figure out what it's doing much better then from any lengthy documentation.
|
|
154
153
|
If I'm mistaken on this please drop me an email with your suggestions.)
|
155
154
|
|
156
155
|
|
157
|
-
## Rationale: Two example
|
156
|
+
## Rationale: Two example use cases
|
158
157
|
|
159
158
|
### Conditionally prepending the locale
|
160
159
|
|
161
|
-
An early
|
162
|
-
at the beginning of
|
160
|
+
An early use-case from which this originated was the need to define a locale
|
161
|
+
at the beginning of a URL in a way so that
|
163
162
|
|
164
163
|
* the locale can be omitted when it is the default locale
|
165
164
|
* all the url\_helpers that are generated by named routes as well as url_for continue to work in
|
@@ -1,14 +1,11 @@
|
|
1
1
|
require 'action_dispatch'
|
2
2
|
require 'active_support/core_ext/module/aliasing'
|
3
3
|
require 'active_support/core_ext/hash/reverse_merge'
|
4
|
+
require 'routing_filter/result_wrapper'
|
4
5
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
mapper.class_eval do
|
9
|
-
def filter(*args)
|
10
|
-
@set.add_filters(*args)
|
11
|
-
end
|
6
|
+
ActionDispatch::Routing::Mapper.class_eval do
|
7
|
+
def filter(*args)
|
8
|
+
@set.add_filters(*args)
|
12
9
|
end
|
13
10
|
end
|
14
11
|
|
@@ -25,10 +22,9 @@ module ActionDispatchRoutingRouteSetWithFiltering
|
|
25
22
|
def generate(route_key, options, recall = {})
|
26
23
|
options = options.symbolize_keys
|
27
24
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
})
|
25
|
+
filters.run(:around_generate, options, &lambda {
|
26
|
+
RoutingFilter::ResultWrapper.new(super(route_key, options, recall))
|
27
|
+
}).generate
|
32
28
|
end
|
33
29
|
|
34
30
|
def clear!
|
@@ -37,8 +33,7 @@ module ActionDispatchRoutingRouteSetWithFiltering
|
|
37
33
|
end
|
38
34
|
end
|
39
35
|
|
40
|
-
ActionDispatch::Routing::RouteSet.
|
41
|
-
|
36
|
+
ActionDispatch::Routing::RouteSet.prepend ActionDispatchRoutingRouteSetWithFiltering
|
42
37
|
|
43
38
|
ActionDispatch::Journey::Routes.class_eval do
|
44
39
|
def filters
|
@@ -24,14 +24,12 @@ module RoutingFilter
|
|
24
24
|
$1
|
25
25
|
end
|
26
26
|
|
27
|
-
def prepend_segment
|
28
|
-
url
|
29
|
-
url.sub!(%r(^(http.?://[^/]*)?(.*))) { "#{$1}/#{segment}#{$2 == '/' ? '' : $2}" }
|
27
|
+
def prepend_segment(url, segment)
|
28
|
+
url.sub(%r(^(http.?://[^/]*)?(.*))) { "#{$1}/#{segment}#{$2 == '/' ? '' : $2}" }
|
30
29
|
end
|
31
30
|
|
32
|
-
def append_segment
|
33
|
-
url
|
34
|
-
url.sub!(%r(/?($|\?))) { "/#{segment}#{$1}" }
|
31
|
+
def append_segment(url, segment)
|
32
|
+
url.sub(%r(/?($|\?))) { "/#{segment}#{$1}" }
|
35
33
|
end
|
36
34
|
end
|
37
|
-
end
|
35
|
+
end
|
@@ -1,11 +1,11 @@
|
|
1
1
|
# The Extension filter chops a file extension off from the end of the
|
2
|
-
# recognized path. When a path is generated the filter re-adds the extension
|
2
|
+
# recognized path. When a path is generated the filter re-adds the extension
|
3
3
|
# to the path accordingly.
|
4
|
-
#
|
4
|
+
#
|
5
5
|
# incoming url: /products.xml
|
6
6
|
# filtered url: /products
|
7
7
|
# generated url: /products.xml
|
8
|
-
#
|
8
|
+
#
|
9
9
|
# You can install the filter like this:
|
10
10
|
#
|
11
11
|
# # in config/routes.rb
|
@@ -30,30 +30,29 @@ module RoutingFilter
|
|
30
30
|
|
31
31
|
def around_generate(params, &block)
|
32
32
|
yield.tap do |result|
|
33
|
-
|
34
|
-
append_extension!(url) if append_extension?(url)
|
33
|
+
result.update append_extension!(result.url) if append_extension?(result.url)
|
35
34
|
end
|
36
35
|
end
|
37
36
|
|
38
37
|
protected
|
39
|
-
|
38
|
+
|
40
39
|
def extract_extension!(path)
|
41
40
|
path.sub!(/\.#{extension}$/, '')
|
42
41
|
$1
|
43
42
|
end
|
44
|
-
|
43
|
+
|
45
44
|
def append_extension?(url)
|
46
45
|
!(blank?(url) || excluded?(url) || mime_extension?(url))
|
47
46
|
end
|
48
|
-
|
47
|
+
|
49
48
|
def append_extension!(url)
|
50
49
|
url.replace url.sub(/(\?|$)/, ".#{extension}\\1")
|
51
50
|
end
|
52
|
-
|
51
|
+
|
53
52
|
def blank?(url)
|
54
53
|
url.blank? || !!url.match(%r(^/(\?|$)))
|
55
54
|
end
|
56
|
-
|
55
|
+
|
57
56
|
def excluded?(url)
|
58
57
|
case exclude
|
59
58
|
when Regexp
|
@@ -62,7 +61,7 @@ module RoutingFilter
|
|
62
61
|
exclude.call(url)
|
63
62
|
end
|
64
63
|
end
|
65
|
-
|
64
|
+
|
66
65
|
def mime_extension?(url)
|
67
66
|
url =~ /\.#{Mime::EXTENSION_LOOKUP.keys.join('|')}(\?|$)/
|
68
67
|
end
|
@@ -67,9 +67,8 @@ module RoutingFilter
|
|
67
67
|
|
68
68
|
args << params
|
69
69
|
|
70
|
-
yield.tap do |result|
|
71
|
-
url
|
72
|
-
prepend_segment!(result, locale) if prepend_locale?(locale) && !excluded?(url)
|
70
|
+
yield.tap do |result|
|
71
|
+
result.update prepend_segment(result.url, locale) if prepend_locale?(locale) && !excluded?(result.url)
|
73
72
|
end
|
74
73
|
end
|
75
74
|
|
@@ -85,7 +84,7 @@ module RoutingFilter
|
|
85
84
|
def prepend_locale?(locale)
|
86
85
|
locale && (self.class.include_default_locale? || !default_locale?(locale))
|
87
86
|
end
|
88
|
-
|
87
|
+
|
89
88
|
def excluded?(url)
|
90
89
|
case exclude
|
91
90
|
when Regexp
|
@@ -34,7 +34,7 @@ module RoutingFilter
|
|
34
34
|
def around_generate(params, &block)
|
35
35
|
page = params.delete(:page)
|
36
36
|
yield.tap do |result|
|
37
|
-
append_segment
|
37
|
+
result.update append_segment(result.url, "page/#{page}") if append_page?(page)
|
38
38
|
end
|
39
39
|
end
|
40
40
|
|
@@ -22,7 +22,7 @@
|
|
22
22
|
module RoutingFilter
|
23
23
|
class Uuid < Filter
|
24
24
|
UUID_SEGMENT = %r(^/?([a-z\d]{8}\-[a-z\d]{4}\-[a-z\d]{4}\-[a-z\d]{4}\-[a-z\d]{12})(/)?)
|
25
|
-
|
25
|
+
|
26
26
|
def around_recognize(path, env, &block)
|
27
27
|
uuid = extract_segment!(UUID_SEGMENT, path)
|
28
28
|
yield.tap do |params|
|
@@ -33,7 +33,7 @@ module RoutingFilter
|
|
33
33
|
def around_generate(params, &block)
|
34
34
|
uuid = params.delete(:uuid)
|
35
35
|
yield.tap do |result|
|
36
|
-
prepend_segment
|
36
|
+
result.update prepend_segment(result.url, uuid) if uuid
|
37
37
|
end
|
38
38
|
end
|
39
39
|
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module RoutingFilter
|
2
|
+
class ResultWrapper
|
3
|
+
RouteWithParams = Struct.new(:url, :params) do
|
4
|
+
def path(_)
|
5
|
+
url
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
attr_reader :url, :params
|
10
|
+
|
11
|
+
def initialize(result)
|
12
|
+
@url = result.path(nil)
|
13
|
+
@params = result.params
|
14
|
+
end
|
15
|
+
|
16
|
+
def update(url)
|
17
|
+
@url = url
|
18
|
+
end
|
19
|
+
|
20
|
+
def generate
|
21
|
+
RouteWithParams.new(url, params)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: routing-filter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.7.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sven Fuchs
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-08-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: actionpack
|
@@ -16,28 +16,28 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '6.1'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '6.1'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: activesupport
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
33
|
+
version: '6.1'
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
40
|
+
version: '6.1'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: i18n
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -86,14 +86,14 @@ dependencies:
|
|
86
86
|
requirements:
|
87
87
|
- - ">="
|
88
88
|
- !ruby/object:Gem::Version
|
89
|
-
version: '
|
89
|
+
version: '6.1'
|
90
90
|
type: :development
|
91
91
|
prerelease: false
|
92
92
|
version_requirements: !ruby/object:Gem::Requirement
|
93
93
|
requirements:
|
94
94
|
- - ">="
|
95
95
|
- !ruby/object:Gem::Version
|
96
|
-
version: '
|
96
|
+
version: '6.1'
|
97
97
|
- !ruby/object:Gem::Dependency
|
98
98
|
name: minitest
|
99
99
|
requirement: !ruby/object:Gem::Requirement
|
@@ -131,11 +131,13 @@ files:
|
|
131
131
|
- lib/routing_filter/filters/locale.rb
|
132
132
|
- lib/routing_filter/filters/pagination.rb
|
133
133
|
- lib/routing_filter/filters/uuid.rb
|
134
|
+
- lib/routing_filter/result_wrapper.rb
|
134
135
|
- lib/routing_filter/version.rb
|
135
136
|
homepage: http://github.com/svenfuchs/routing-filter
|
136
|
-
licenses:
|
137
|
+
licenses:
|
138
|
+
- MIT
|
137
139
|
metadata: {}
|
138
|
-
post_install_message:
|
140
|
+
post_install_message:
|
139
141
|
rdoc_options: []
|
140
142
|
require_paths:
|
141
143
|
- lib
|
@@ -150,9 +152,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
150
152
|
- !ruby/object:Gem::Version
|
151
153
|
version: '0'
|
152
154
|
requirements: []
|
153
|
-
|
154
|
-
|
155
|
-
signing_key:
|
155
|
+
rubygems_version: 3.2.22
|
156
|
+
signing_key:
|
156
157
|
specification_version: 4
|
157
158
|
summary: Routing filters wraps around the complex beast that the Rails routing system
|
158
159
|
is, allowing for unseen flexibility and power in Rails URL recognition and generation
|