routing-filter 0.3.1 → 0.6.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 8d325ee34a8fc83f65430e6341ce97f86767f4b03d52252820ccf2eec4ce0f20
4
+ data.tar.gz: 0eeff41dc5c2556854828223f8219b92941249fe332c6c73f42a27ebb38068ab
5
+ SHA512:
6
+ metadata.gz: be99776a974207014dc3ea4ef43811a37be9277ee6ee12f84552fd8cc992ea6e7419a2219d0fbb3bb81fcb77c3eef8b4074e4f3e92f5b814b80a5b09470e097f
7
+ data.tar.gz: 85eabfac1078e4c520ca316b0321027e54e1fa8e2eb900df76af40f726c3974dac695cbf5ad6d889c5aec0f48fa8320f273940a97f01612dfa5af15238f1ec7d
data/CHANGELOG.md CHANGED
@@ -1,3 +1,31 @@
1
+ ## 0.6.3
2
+
3
+ * Rails 6.0 support
4
+
5
+ ## 0.6.2
6
+
7
+ * Rails 5.2 support
8
+
9
+ ## 0.6.1
10
+
11
+ * Rails 5.1 support
12
+
13
+ ## 0.6.0
14
+
15
+ * Rails 5.0 support
16
+
17
+ ## 0.5.1
18
+
19
+ * Handle env as Hash and object at the same time in journey overrides.
20
+
21
+ ## 0.5.0
22
+
23
+ * Rails 4.2 only support
24
+
25
+ ## 0.4.0
26
+
27
+ * Rails 4 support
28
+
1
29
  ## 0.3.1
2
30
  * Filters support exclude option.
3
31
 
data/README.markdown CHANGED
@@ -1,5 +1,7 @@
1
1
  # Routing Filter
2
2
 
3
+ [![Build Status](https://secure.travis-ci.org/svenfuchs/routing-filter.png)](http://travis-ci.org/svenfuchs/routing-filter)
4
+
3
5
  Routing filters wrap around the complex beast that the Rails routing system is
4
6
  to allow for unseen flexibility and power in Rails URL recognition and
5
7
  generation.
@@ -17,15 +19,14 @@ implement custom ones. Maybe the most popular one is the Locale routing filter:
17
19
  * `Uuid` - prepends a uuid for authentication or other purposes (e.g. /d00fbbd1-82b6-4c1a-a57d-098d529d6854/products/1)
18
20
  * `Extension` - appends an extension (e.g. /products.html)
19
21
 
20
- Please note that Rails 3's routing system is much more powerful and flexible
21
- than Rails 2 was. There are many usecases that now can be covered with just
22
- Rails 3 default routing features that weren't doable in Rails 2. For an example
23
- of a quite complex and flexible route see this [gist by Andrew White](http://gist.github.com/653543)
24
22
 
25
23
  ## Requirements
26
24
 
27
- routing-filter currently only works with Rails. It should not be all too hard
28
- to get it working with plain Rack::Mount but I haven't had that usecase, yet.
25
+ Latest routing-filter (~> 0.6.0) currently only works with Rails >= 4.2. It should
26
+ not be all too hard to get it working with plain Rack::Mount but I haven't had that
27
+ usecase, yet.
28
+
29
+ For older Rails use `0-4-stable` branch.
29
30
 
30
31
  ## Installation
31
32
 
@@ -33,48 +34,61 @@ Just install the Gem:
33
34
 
34
35
  $ gem install routing-filter
35
36
 
36
- The Gem should work out of the box for Rails 3 after specifying it in your
37
+ The Gem should work out of the box with Rails 4.2 after specifying it in your
37
38
  application's Gemfile.
38
39
 
39
- # Gemfile
40
- gem 'routing-filter'
41
-
42
- In order to use it with Rails 2.x you could specify it in your environment.rb
43
-
44
- # config/environment.rb
45
- gem 'routing-filter'
40
+ ```ruby
41
+ # Gemfile
42
+ gem 'routing-filter'
43
+ ```
46
44
 
47
45
  ## Usage
48
46
 
49
47
  Once the Gem has loaded you can setup the filters in your routes file like this:
50
48
 
51
- # in config/routes.rb
52
- Rails.application.routes.draw do
53
- filter :pagination, :uuid
54
- end
49
+ ```ruby
50
+ # in config/routes.rb
51
+ Rails.application.routes.draw do
52
+ filter :pagination, :uuid
53
+ end
54
+ ```
55
55
 
56
56
  Filters can also accept options:
57
57
 
58
- Rails.application.routes.draw do
59
- filter :extension, :exclude => %r(^admin/)
60
- filter :locale, :exclude => /^\/admin/
61
- end
58
+ ```ruby
59
+ Rails.application.routes.draw do
60
+ filter :extension, :exclude => %r(^admin/)
61
+ filter :locale, :exclude => /^\/admin/
62
+ end
63
+ ```
62
64
 
63
- ## Running the tests
65
+ The locale filter may be configured to not include the default locale:
64
66
 
65
- To run the tests against different dependencies [appraisal](https://github.com/thoughtbot/appraisal) is used.
67
+ # in config/initializers/routing_filter.rb
68
+ # Do not include default locale in generated URLs
69
+ RoutingFilter::Locale.include_default_locale = false
70
+
71
+ # Then if the default locale is :de
72
+ # products_path(:locale => 'de') => /products
73
+ # products_path(:locale => 'en') => /en/products
66
74
 
67
- Running the tests for all supported Rails versions:
75
+ ### Testing
68
76
 
69
- $ bundle install
70
- $ bundle exec rake all
77
+ RoutingFilter should not be enabled in functional tests by default since the Rails router gets
78
+ bypassed for most testcases. Having RoutingFilter enabled in this setup can cause missing parameters
79
+ in the test environment. Routing tests can/should re-enable RoutingFilter since the whole routing stack
80
+ gets executed for these testcases.
71
81
 
72
- Running the tests for a single version, for example Rails 3.1:
82
+ To disable RoutingFilter in your test suite add the following to your test_helper.rb / spec_helper.rb:
73
83
 
74
- $ bundle install
75
- $ bundle exec rake appraisal:rails-3.1 test
84
+ ```ruby
85
+ RoutingFilter.active = false
86
+ ```
87
+
88
+ ## Running the tests
76
89
 
77
- Valid appraisal targets include rails-2.3, rails-3.0, rails-3.1 and rails-3.2
90
+ $ bundle install
91
+ $ bundle exec rake test
78
92
 
79
93
  ## Filter order
80
94
 
@@ -96,36 +110,40 @@ For example implementations have a look at the existing filters in
96
110
 
97
111
  The following would be a sceleton of an empty filter:
98
112
 
99
- module RoutingFilter
100
- class Awesomeness < Filter
101
- def around_recognize(path, env, &block)
102
- # Alter the path here before it gets recognized.
103
- # Make sure to yield (calls the next around filter if present and
104
- # eventually `recognize_path` on the routeset):
105
- yield.tap do |params|
106
- # You can additionally modify the params here before they get passed
107
- # to the controller.
108
- end
109
- end
110
-
111
- def around_generate(params, &block)
112
- # Alter arguments here before they are passed to `url_for`.
113
- # Make sure to yield (calls the next around filter if present and
114
- # eventually `url_for` on the controller):
115
- yield.tap do |result|
116
- # You can change the generated url_or_path here. Make sure to use
117
- # one of the "in-place" modifying String methods though (like sub!
118
- # and friends).
119
- end
120
- end
113
+ ```ruby
114
+ module RoutingFilter
115
+ class Awesomeness < Filter
116
+ def around_recognize(path, env, &block)
117
+ # Alter the path here before it gets recognized.
118
+ # Make sure to yield (calls the next around filter if present and
119
+ # eventually `recognize_path` on the routeset):
120
+ yield.tap do |params|
121
+ # You can additionally modify the params here before they get passed
122
+ # to the controller.
121
123
  end
122
124
  end
123
125
 
126
+ def around_generate(params, &block)
127
+ # Alter arguments here before they are passed to `url_for`.
128
+ # Make sure to yield (calls the next around filter if present and
129
+ # eventually `url_for` on the controller):
130
+ yield.tap do |result|
131
+ # You can change the generated url_or_path here. Make sure to use
132
+ # one of the "in-place" modifying String methods though (like sub!
133
+ # and friends).
134
+ end
135
+ end
136
+ end
137
+ end
138
+ ```
139
+
124
140
  You can specify the filter explicitely in your routes.rb:
125
141
 
126
- Rails.application.routes.draw do
127
- filter :awesomeness
128
- end
142
+ ```ruby
143
+ Rails.application.routes.draw do
144
+ filter :awesomeness
145
+ end
146
+ ```
129
147
 
130
148
  (I am not sure if it makes sense to provide more technical information than
131
149
  this because the usage of this plugin definitely requires some advanced
@@ -0,0 +1,49 @@
1
+ require 'action_dispatch'
2
+ require 'active_support/core_ext/module/aliasing'
3
+ require 'active_support/core_ext/hash/reverse_merge'
4
+
5
+ mappers = [ActionDispatch::Routing::Mapper]
6
+ mappers << ActionDispatch::Routing::DeprecatedMapper if defined?(ActionDispatch::Routing::DeprecatedMapper)
7
+ mappers.each do |mapper|
8
+ mapper.class_eval do
9
+ def filter(*args)
10
+ @set.add_filters(*args)
11
+ end
12
+ end
13
+ end
14
+
15
+ module ActionDispatchRoutingRouteSetWithFiltering
16
+ def filters
17
+ @set.filters if @set
18
+ end
19
+
20
+ def add_filters(*names)
21
+ options = names.extract_options!
22
+ names.each { |name| filters.unshift(RoutingFilter.build(name, options)) }
23
+ end
24
+
25
+ def generate(route_key, options, recall = {})
26
+ options = options.symbolize_keys
27
+
28
+ # `around_generate` is destructive method and it breaks url. To avoid this, `dup` is required.
29
+ filters.run(:around_generate, options, &lambda{
30
+ super(route_key, options, recall).map(&:dup)
31
+ })
32
+ end
33
+
34
+ def clear!
35
+ filters.clear if filters
36
+ super
37
+ end
38
+ end
39
+
40
+ ActionDispatch::Routing::RouteSet.send(:prepend, ActionDispatchRoutingRouteSetWithFiltering)
41
+
42
+
43
+ ActionDispatch::Journey::Routes.class_eval do
44
+ def filters
45
+ @filters ||= RoutingFilter::Chain.new.tap { |f| @filters = f unless frozen? }
46
+ end
47
+ end
48
+
49
+ require 'routing_filter/adapters/routers/journey'
@@ -1,23 +1,24 @@
1
- require 'journey/routes'
2
- require 'journey/router'
3
-
4
- Journey::Routes.class_eval do
5
- def filters
6
- @filters || RoutingFilter::Chain.new.tap { |f| @filters = f unless frozen? }
7
- end
8
- end
9
-
10
- Journey::Router.class_eval do
11
- def find_routes_with_filtering env
12
- path, filter_parameters = env['PATH_INFO'], {}
1
+ module ActionDispatchJourneyRouterWithFiltering
2
+ def find_routes(env)
3
+ path = env.is_a?(Hash) ? env['PATH_INFO'] : env.path_info
4
+ filter_parameters = {}
5
+ original_path = path.dup
13
6
 
14
7
  @routes.filters.run(:around_recognize, path, env) do
15
8
  filter_parameters
16
9
  end
17
10
 
18
- find_routes_without_filtering(env).map do |match, parameters, route|
11
+ super(env).map do |match, parameters, route|
19
12
  [ match, parameters.merge(filter_parameters), route ]
13
+ end.tap do |match, parameters, route|
14
+ # restore the original path
15
+ if env.is_a?(Hash)
16
+ env['PATH_INFO'] = original_path
17
+ else
18
+ env.path_info = original_path
19
+ end
20
20
  end
21
21
  end
22
- alias_method_chain :find_routes, :filtering
23
22
  end
23
+
24
+ ActionDispatch::Journey::Router.send(:prepend, ActionDispatchJourneyRouterWithFiltering)
@@ -27,7 +27,7 @@ module RoutingFilter
27
27
  def around_recognize(path, env, &block)
28
28
  page = extract_segment!(PAGINATION_SEGMENT, path)
29
29
  yield.tap do |params|
30
- params[:page] = page.to_i if page
30
+ params[:page] = page if page
31
31
  end
32
32
  end
33
33
 
@@ -1,3 +1,3 @@
1
1
  module RoutingFilter
2
- VERSION = '0.3.1'
2
+ VERSION = '0.6.3'
3
3
  end
@@ -24,4 +24,4 @@ module RoutingFilter
24
24
  end
25
25
  end
26
26
 
27
- require "routing_filter/adapters/rails_#{ActionPack::VERSION::MAJOR}"
27
+ require "routing_filter/adapters/rails"
metadata CHANGED
@@ -1,60 +1,113 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: routing-filter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
5
- prerelease:
4
+ version: 0.6.3
6
5
  platform: ruby
7
6
  authors:
8
7
  - Sven Fuchs
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2012-04-28 00:00:00.000000000 Z
11
+ date: 2019-09-09 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: actionpack
16
- requirement: &2156259340 !ruby/object:Gem::Requirement
17
- none: false
15
+ requirement: !ruby/object:Gem::Requirement
18
16
  requirements:
19
- - - ! '>='
17
+ - - ">="
20
18
  - !ruby/object:Gem::Version
21
- version: '0'
19
+ version: '4.2'
22
20
  type: :runtime
23
21
  prerelease: false
24
- version_requirements: *2156259340
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '4.2'
25
27
  - !ruby/object:Gem::Dependency
26
- name: appraisal
27
- requirement: &2156258620 !ruby/object:Gem::Requirement
28
- none: false
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
29
30
  requirements:
30
- - - ! '>='
31
+ - - ">="
31
32
  - !ruby/object:Gem::Version
32
- version: '0'
33
- type: :development
33
+ version: '4.2'
34
+ type: :runtime
34
35
  prerelease: false
35
- version_requirements: *2156258620
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '4.2'
36
41
  - !ruby/object:Gem::Dependency
37
42
  name: i18n
38
- requirement: &2156257900 !ruby/object:Gem::Requirement
39
- none: false
43
+ requirement: !ruby/object:Gem::Requirement
40
44
  requirements:
41
- - - ! '>='
45
+ - - ">="
42
46
  - !ruby/object:Gem::Version
43
47
  version: '0'
44
48
  type: :development
45
49
  prerelease: false
46
- version_requirements: *2156257900
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
47
55
  - !ruby/object:Gem::Dependency
48
56
  name: test_declarative
49
- requirement: &2156273620 !ruby/object:Gem::Requirement
50
- none: false
57
+ requirement: !ruby/object:Gem::Requirement
51
58
  requirements:
52
- - - ! '>='
59
+ - - ">="
53
60
  - !ruby/object:Gem::Version
54
61
  version: '0'
55
62
  type: :development
56
63
  prerelease: false
57
- version_requirements: *2156273620
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rack-test
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.6.2
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.6.2
83
+ - !ruby/object:Gem::Dependency
84
+ name: rails
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '4.2'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '4.2'
97
+ - !ruby/object:Gem::Dependency
98
+ name: minitest
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "<"
102
+ - !ruby/object:Gem::Version
103
+ version: 5.10.2
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "<"
109
+ - !ruby/object:Gem::Version
110
+ version: 5.10.2
58
111
  description: Routing filters wraps around the complex beast that the Rails routing
59
112
  system is, allowing for unseen flexibility and power in Rails URL recognition and
60
113
  generation.
@@ -64,12 +117,12 @@ extensions: []
64
117
  extra_rdoc_files: []
65
118
  files:
66
119
  - CHANGELOG.md
67
- - README.markdown
68
120
  - MIT-LICENSE
69
- - lib/routing/filter.rb
121
+ - README.markdown
70
122
  - lib/routing-filter.rb
71
- - lib/routing_filter/adapters/rails_2.rb
72
- - lib/routing_filter/adapters/rails_3.rb
123
+ - lib/routing/filter.rb
124
+ - lib/routing_filter.rb
125
+ - lib/routing_filter/adapters/rails.rb
73
126
  - lib/routing_filter/adapters/routers/journey.rb
74
127
  - lib/routing_filter/adapters/routers/rack_mount.rb
75
128
  - lib/routing_filter/chain.rb
@@ -79,30 +132,28 @@ files:
79
132
  - lib/routing_filter/filters/pagination.rb
80
133
  - lib/routing_filter/filters/uuid.rb
81
134
  - lib/routing_filter/version.rb
82
- - lib/routing_filter.rb
83
135
  homepage: http://github.com/svenfuchs/routing-filter
84
136
  licenses: []
137
+ metadata: {}
85
138
  post_install_message:
86
139
  rdoc_options: []
87
140
  require_paths:
88
141
  - lib
89
142
  required_ruby_version: !ruby/object:Gem::Requirement
90
- none: false
91
143
  requirements:
92
- - - ! '>='
144
+ - - ">="
93
145
  - !ruby/object:Gem::Version
94
- version: '0'
146
+ version: '2.0'
95
147
  required_rubygems_version: !ruby/object:Gem::Requirement
96
- none: false
97
148
  requirements:
98
- - - ! '>='
149
+ - - ">="
99
150
  - !ruby/object:Gem::Version
100
151
  version: '0'
101
152
  requirements: []
102
- rubyforge_project: ! '[none]'
103
- rubygems_version: 1.8.17
153
+ rubyforge_project: "[none]"
154
+ rubygems_version: 2.7.6.2
104
155
  signing_key:
105
- specification_version: 3
156
+ specification_version: 4
106
157
  summary: Routing filters wraps around the complex beast that the Rails routing system
107
158
  is, allowing for unseen flexibility and power in Rails URL recognition and generation
108
159
  test_files: []
@@ -1,69 +0,0 @@
1
- require 'action_controller'
2
-
3
- # allows to install a filter to the route set by calling: map.filter 'locale'
4
- ActionController::Routing::RouteSet::Mapper.class_eval do
5
- def filter(*args)
6
- @set.add_filters(*args)
7
- end
8
- end
9
-
10
- # same here for the optimized url generation in named routes
11
- ActionController::Routing::RouteSet::NamedRouteCollection.class_eval do
12
- # gosh. monkey engineering optimization code
13
- def generate_optimisation_block_with_filtering(*args)
14
- code = generate_optimisation_block_without_filtering(*args)
15
- if match = code.match(%r(^return (.*) if (.*)))
16
- # returned string must not contain newlines, or we'll spill out of inline code comments in
17
- # ActionController::Routing::RouteSet::NamedRouteCollection#define_url_helper
18
- "#{match[1]}.tap { |result|" +
19
- " ActionController::Routing::Routes.filters.run(:around_generate, *args, &lambda{ result }) " +
20
- "} if #{match[2]}"
21
- end
22
- end
23
- alias_method_chain :generate_optimisation_block, :filtering
24
- end
25
-
26
- ActionController::Routing::RouteSet.class_eval do
27
- attr_writer :filters
28
-
29
- def filters
30
- @filters ||= RoutingFilter::Chain.new
31
- end
32
-
33
- def add_filters(*names)
34
- options = names.extract_options!
35
- names.each { |name| filters.unshift(RoutingFilter.build(name, options)) }
36
- end
37
-
38
- def recognize_path_with_filtering(path, env = {})
39
- path = ::URI.unescape(path.dup) # string is frozen due to memoize
40
- filters.run(:around_recognize, path, env, &lambda{ recognize_path_without_filtering(path, env) })
41
- end
42
- alias_method_chain :recognize_path, :filtering
43
-
44
- def generate_with_filtering(*args)
45
- filters.run(:around_generate, args.first, &lambda{ generate_without_filtering(*args) })
46
- end
47
- alias_method_chain :generate, :filtering
48
-
49
- def clear_with_filtering!
50
- @filters.clear if @filters
51
- clear_without_filtering!
52
- end
53
- alias_method_chain :clear!, :filtering
54
-
55
- # add some useful information to the request environment
56
- # right, this is from jamis buck's excellent article about routes internals
57
- # http://weblog.jamisbuck.org/2006/10/26/monkey-patching-rails-extending-routes-2
58
- # TODO move this ... where?
59
- alias_method :extract_request_environment_without_host, :extract_request_environment unless method_defined? :extract_request_environment_without_host
60
- def extract_request_environment(request)
61
- extract_request_environment_without_host(request).tap do |env|
62
- env.merge! :host => request.host,
63
- :port => request.port,
64
- :host_with_port => request.host_with_port,
65
- :domain => request.domain,
66
- :subdomain => request.subdomains.first
67
- end
68
- end
69
- end
@@ -1,47 +0,0 @@
1
- require 'action_dispatch'
2
- require 'active_support/core_ext/module/aliasing'
3
- require 'active_support/core_ext/hash/reverse_merge'
4
-
5
- mappers = [ActionDispatch::Routing::Mapper]
6
- mappers << ActionDispatch::Routing::DeprecatedMapper if defined?(ActionDispatch::Routing::DeprecatedMapper)
7
- mappers.each do |mapper|
8
- mapper.class_eval do
9
- def filter(*args)
10
- @set.add_filters(*args)
11
- end
12
- end
13
- end
14
-
15
- ActionDispatch::Routing::RouteSet.class_eval do
16
- def filters
17
- @set.filters if @set
18
- end
19
-
20
- def add_filters(*names)
21
- options = names.extract_options!
22
- names.each { |name| filters.unshift(RoutingFilter.build(name, options)) }
23
- end
24
-
25
- # def recognize_path_with_filtering(path, env = {})
26
- # @set.filters.run(:around_recognize, path.dup, env, &lambda{ recognize_path_without_filtering(path.dup, env) })
27
- # end
28
- # alias_method_chain :recognize_path, :filtering
29
-
30
- def generate_with_filtering(options, recall = {}, extras = false)
31
- filters.run(:around_generate, options, &lambda{ generate_without_filtering(options, recall, extras) })
32
- end
33
- alias_method_chain :generate, :filtering
34
-
35
- def clear_with_filtering!
36
- filters.clear if filters
37
- clear_without_filtering!
38
- end
39
- alias_method_chain :clear!, :filtering
40
- end
41
-
42
- case ActionPack::VERSION::MINOR
43
- when 2
44
- require 'routing_filter/adapters/routers/journey'
45
- when 0,1
46
- require 'routing_filter/adapters/routers/rack_mount'
47
- end