routing-filter 0.1.3 → 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/routing_filter/adapters/rails_3.rb +9 -8
- data/lib/routing_filter/chain.rb +2 -2
- data/lib/routing_filter/filter.rb +18 -1
- data/lib/routing_filter/filters/locale.rb +12 -22
- data/lib/routing_filter/filters/pagination.rb +12 -16
- data/lib/routing_filter/filters/uuid.rb +5 -17
- data/lib/routing_filter/version.rb +1 -1
- metadata +3 -3
@@ -21,8 +21,8 @@ ActionDispatch::Routing::RouteSet.class_eval do
|
|
21
21
|
end
|
22
22
|
alias_method_chain :recognize_path, :filtering
|
23
23
|
|
24
|
-
def generate_with_filtering(
|
25
|
-
@set.filters.run(:around_generate,
|
24
|
+
def generate_with_filtering(options, recall = {}, extras = false)
|
25
|
+
@set.filters.run(:around_generate, options, &lambda{ generate_without_filtering(options, recall, extras) })
|
26
26
|
end
|
27
27
|
alias_method_chain :generate, :filtering
|
28
28
|
|
@@ -38,7 +38,7 @@ require 'rack/mount/code_generation'
|
|
38
38
|
|
39
39
|
Rack::Mount::RouteSet.class_eval do
|
40
40
|
def filters
|
41
|
-
@filters
|
41
|
+
@filters || RoutingFilter::Chain.new.tap { |f| @filters = f unless frozen? }
|
42
42
|
end
|
43
43
|
end
|
44
44
|
|
@@ -56,12 +56,13 @@ Rack::Mount::CodeGeneration.class_eval do
|
|
56
56
|
# note: if you overly and unnecessarily use blocks in your lowlevel libraries you make it fricking
|
57
57
|
# hard for your users to hook in anywhere
|
58
58
|
def recognize_with_filtering(request, &block)
|
59
|
-
route, matches, params = nil
|
60
|
-
filters.run(:around_recognize,
|
61
|
-
|
62
|
-
params
|
59
|
+
path, route, matches, params = request.env['PATH_INFO'], nil, nil, nil
|
60
|
+
filters.run(:around_recognize, path, {}) do
|
61
|
+
path.replace('/') if path.empty?
|
62
|
+
route, matches, params = recognize_without_filtering(request)
|
63
|
+
params || {}
|
63
64
|
end
|
64
|
-
block.call(route, matches, params)
|
65
|
+
block.call(route, matches, params) if route
|
65
66
|
end
|
66
67
|
end
|
67
68
|
|
data/lib/routing_filter/chain.rb
CHANGED
@@ -6,11 +6,11 @@ module RoutingFilter
|
|
6
6
|
end
|
7
7
|
|
8
8
|
def run(method, *args, &final)
|
9
|
-
|
9
|
+
active? ? first.run(method, *args, &final) : final.call
|
10
10
|
end
|
11
11
|
|
12
12
|
def active?
|
13
|
-
RoutingFilter.active && !empty?
|
13
|
+
RoutingFilter.active? && !empty?
|
14
14
|
end
|
15
15
|
end
|
16
16
|
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
module RoutingFilter
|
2
2
|
class Filter
|
3
3
|
attr_accessor :next, :previous, :options
|
4
|
-
|
4
|
+
|
5
5
|
def initialize(options = {})
|
6
6
|
@options = options
|
7
7
|
end
|
@@ -15,5 +15,22 @@ module RoutingFilter
|
|
15
15
|
_prev = previous ? lambda { previous.run_reverse(method, *args, &block) } : block
|
16
16
|
RoutingFilter.active? ? send(method, *args, &_prev) : _prev.call(*args)
|
17
17
|
end
|
18
|
+
|
19
|
+
protected
|
20
|
+
|
21
|
+
def extract_segment!(pattern, path)
|
22
|
+
path.sub!(pattern, '')
|
23
|
+
$1
|
24
|
+
end
|
25
|
+
|
26
|
+
def prepend_segment!(result, segment)
|
27
|
+
url = result.is_a?(Array) ? result.first : result
|
28
|
+
url.sub!(%r(^(http.?://[^/]*)?(.*))) { "#{$1}/#{segment}#{$2 == '/' ? '' : $2}" }
|
29
|
+
end
|
30
|
+
|
31
|
+
def append_segment!(result, segment)
|
32
|
+
url = result.is_a?(Array) ? result.first : result
|
33
|
+
url.sub!(%r(/?($|\?))) { "/#{segment}#{$1}" }
|
34
|
+
end
|
18
35
|
end
|
19
36
|
end
|
@@ -1,12 +1,12 @@
|
|
1
|
-
# The Locale filter extracts segments matching /:locale from the beginning of
|
1
|
+
# The Locale filter extracts segments matching /:locale from the beginning of
|
2
2
|
# the recognized path and exposes the page parameter as params[:page]. When a
|
3
3
|
# path is generated the filter adds the segments to the path accordingly if
|
4
4
|
# the page parameter is passed to the url helper.
|
5
|
-
#
|
5
|
+
#
|
6
6
|
# incoming url: /de/products/page/1
|
7
7
|
# filtered url: /de/products
|
8
8
|
# params: params[:locale] = 'de'
|
9
|
-
#
|
9
|
+
#
|
10
10
|
# You can install the filter like this:
|
11
11
|
#
|
12
12
|
# # in config/routes.rb
|
@@ -14,7 +14,7 @@
|
|
14
14
|
# filter :locale
|
15
15
|
# end
|
16
16
|
#
|
17
|
-
# To make your named_route helpers or url_for add the pagination segments you
|
17
|
+
# To make your named_route helpers or url_for add the pagination segments you
|
18
18
|
# can use:
|
19
19
|
#
|
20
20
|
# products_path(:locale => 'de')
|
@@ -46,29 +46,24 @@ module RoutingFilter
|
|
46
46
|
end
|
47
47
|
|
48
48
|
def around_recognize(path, env, &block)
|
49
|
-
locale =
|
50
|
-
yield.tap do |params|
|
51
|
-
params[:locale] = locale if locale
|
49
|
+
locale = extract_segment!(self.class.locales_pattern, path) # remove the locale from the beginning of the path
|
50
|
+
yield.tap do |params| # invoke the given block (calls more filters and finally routing)
|
51
|
+
params[:locale] = locale if locale # set recognized locale to the resulting params hash
|
52
52
|
end
|
53
53
|
end
|
54
54
|
|
55
|
-
def around_generate(
|
56
|
-
locale =
|
57
|
-
locale = I18n.locale if locale.nil?
|
58
|
-
locale = nil unless valid_locale?(locale)
|
55
|
+
def around_generate(params, &block)
|
56
|
+
locale = params.delete(:locale) # extract the passed :locale option
|
57
|
+
locale = I18n.locale if locale.nil? # default to I18n.locale when locale is nil (could also be false)
|
58
|
+
locale = nil unless valid_locale?(locale) # reset to no locale when locale is not valid
|
59
59
|
|
60
60
|
yield.tap do |result|
|
61
|
-
|
61
|
+
prepend_segment!(result, locale) if prepend_locale?(locale)
|
62
62
|
end
|
63
63
|
end
|
64
64
|
|
65
65
|
protected
|
66
66
|
|
67
|
-
def extract_locale!(path)
|
68
|
-
path.sub!(self.class.locales_pattern, '')
|
69
|
-
$1
|
70
|
-
end
|
71
|
-
|
72
67
|
def valid_locale?(locale)
|
73
68
|
locale && self.class.locales.include?(locale.to_sym)
|
74
69
|
end
|
@@ -80,10 +75,5 @@ module RoutingFilter
|
|
80
75
|
def prepend_locale?(locale)
|
81
76
|
locale && (self.class.include_default_locale? || !default_locale?(locale))
|
82
77
|
end
|
83
|
-
|
84
|
-
def prepend_locale!(result, locale)
|
85
|
-
url = result.is_a?(Array) ? result.first : result
|
86
|
-
url.sub!(%r(^(http.?://[^/]*)?(.*))) { "#{$1}/#{locale}#{$2}" }
|
87
|
-
end
|
88
78
|
end
|
89
79
|
end
|
@@ -1,12 +1,12 @@
|
|
1
|
-
# The Pagination filter extracts segments matching /page/:page from the end of
|
1
|
+
# The Pagination filter extracts segments matching /page/:page from the end of
|
2
2
|
# the recognized url and exposes the page parameter as params[:page]. When a
|
3
3
|
# url is generated the filter adds the segments to the url accordingly if the
|
4
4
|
# page parameter is passed to the url helper.
|
5
|
-
#
|
5
|
+
#
|
6
6
|
# incoming url: /products/page/1
|
7
7
|
# filtered url: /products
|
8
8
|
# params: params[:page] = 1
|
9
|
-
#
|
9
|
+
#
|
10
10
|
# You can install the filter like this:
|
11
11
|
#
|
12
12
|
# # in config/routes.rb
|
@@ -14,7 +14,7 @@
|
|
14
14
|
# filter :pagination
|
15
15
|
# end
|
16
16
|
#
|
17
|
-
# To make your named_route helpers or url_for add the pagination segments you
|
17
|
+
# To make your named_route helpers or url_for add the pagination segments you
|
18
18
|
# can use:
|
19
19
|
#
|
20
20
|
# products_path(:page => 1)
|
@@ -22,30 +22,26 @@
|
|
22
22
|
|
23
23
|
module RoutingFilter
|
24
24
|
class Pagination < Filter
|
25
|
+
PAGINATION_SEGMENT = %r(/page/([\d]+)/?$)
|
26
|
+
|
25
27
|
def around_recognize(path, env, &block)
|
26
|
-
page =
|
28
|
+
page = extract_segment!(PAGINATION_SEGMENT, path)
|
27
29
|
yield(path, env).tap do |params|
|
28
30
|
params[:page] = page.to_i if page
|
29
31
|
end
|
30
32
|
end
|
31
33
|
|
32
|
-
def around_generate(
|
33
|
-
page =
|
34
|
+
def around_generate(params, &block)
|
35
|
+
page = params.delete(:page)
|
34
36
|
yield.tap do |result|
|
35
|
-
|
37
|
+
append_segment!(result, "page/#{page}") if append_page?(page)
|
36
38
|
end
|
37
39
|
end
|
38
40
|
|
39
41
|
protected
|
40
42
|
|
41
|
-
def
|
42
|
-
|
43
|
-
$1
|
44
|
-
end
|
45
|
-
|
46
|
-
def append_page!(result, page)
|
47
|
-
url = result.is_a?(Array) ? result.first : result
|
48
|
-
url.sub!(/($|\?)/) { "/page/#{page}#{$1}" }
|
43
|
+
def append_page?(page)
|
44
|
+
page && page.to_i != 1
|
49
45
|
end
|
50
46
|
end
|
51
47
|
end
|
@@ -21,32 +21,20 @@
|
|
21
21
|
|
22
22
|
module RoutingFilter
|
23
23
|
class Uuid < Filter
|
24
|
-
UUID_SEGMENT = %r(^/?([a-z\d]{8}\-[a-z\d]{4}\-[a-z\d]{4}\-[a-z\d]{4}\-[a-z\d]{12})
|
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
|
-
uuid =
|
27
|
+
uuid = extract_segment!(UUID_SEGMENT, path)
|
28
28
|
yield.tap do |params|
|
29
29
|
params[:uuid] = uuid if uuid
|
30
30
|
end
|
31
31
|
end
|
32
32
|
|
33
|
-
def around_generate(
|
34
|
-
uuid =
|
33
|
+
def around_generate(params, &block)
|
34
|
+
uuid = params.delete(:uuid)
|
35
35
|
yield.tap do |result|
|
36
|
-
|
36
|
+
prepend_segment!(result, uuid) if uuid
|
37
37
|
end
|
38
38
|
end
|
39
|
-
|
40
|
-
protected
|
41
|
-
|
42
|
-
def extract_uuid!(path)
|
43
|
-
path.sub!(UUID_SEGMENT, '/')
|
44
|
-
$1
|
45
|
-
end
|
46
|
-
|
47
|
-
def prepend_uuid!(result, uuid)
|
48
|
-
url = result.is_a?(Array) ? result.first : result
|
49
|
-
url.sub!(%r(^(http.?://[^/]*)?(.*))) { "#{$1}/#{uuid}#{$2}" }
|
50
|
-
end
|
51
39
|
end
|
52
40
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: routing-filter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 19
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 4
|
10
|
+
version: 0.1.4
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Sven Fuchs
|