redirect_with_params 1.0.0 → 1.1.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3f2923ffdb029f993ac7d6f8f5a3d942ce116e6b
4
- data.tar.gz: 12fb2f56170563f0ab6bf475f36ba8b70ae34c12
3
+ metadata.gz: e91cf9f9f9da14b4c1b964ab4a9873aa9b2d6c8d
4
+ data.tar.gz: 127bb1e015b4ab7fc2f6dfa1d8d2232e947cef46
5
5
  SHA512:
6
- metadata.gz: fa35cc65896456283e797efc572bbf4d5abbe5a3b4e5412429ce9c5aa82e02e71606ee30e6795d722744de9d150362c077110b8f52f947dc218723eae66b5196
7
- data.tar.gz: fcb6f20bbb678a2b10066fe9205f367d5791811589cee218d340f83933408aa7f253fe081e1ef330d7ce830f0bc7c47066cd932ebb845cc4c30c0be4d07362af
6
+ metadata.gz: a6dd0152c00a71a4e32635f6c95b75f21bff57d99af5673a8ea64afbb626979cae456100632fc7e367e0ba03953580c6ca369a7b529ac000ea3675747583cc48
7
+ data.tar.gz: 33b8426e6746d8a9441597345ebff777a17293525149039bbb729d7b30b991705941cd4f598f786fbe51d0295f4d05851885377e2807e5813c637a10d7988937
data/README.md CHANGED
@@ -7,7 +7,7 @@ Preserve query parameters through Rails routing redirects
7
7
  Add this line to your application's `Gemfile`:
8
8
 
9
9
  ```ruby
10
- gem 'redirect_with_params', require: false
10
+ gem 'redirect_with_params'
11
11
  ```
12
12
 
13
13
  And then execute:
@@ -20,21 +20,27 @@ Or install it yourself as:
20
20
 
21
21
  Add this line to your `config/routes.rb`:
22
22
 
23
+ ## Usage
24
+
25
+ Use `redirect_with_params` in place of `redirect` in your Routes file. This will preserve the query parameters in the redirect.
26
+
23
27
  ```ruby
24
- require 'redirect_with_params'
28
+ My::Application.routes.draw do
29
+ get "some/route/:id" => redirect_with_params { |path_params, request| "/some/new/route/#{path_params[:id]}" }
30
+ end
25
31
  ```
26
32
 
27
- ## Usage
33
+ Navigating your browser to `/some/route/123?my_param=zebra&source=web` will redirect you to `/some/new/route/123?my_param=zebra&source=web`
28
34
 
29
- Use `redirect_with_params` in place of `redirect` in your Routes file. This will perserve the query parameters in the redirect.
35
+ Optionally, you can specify a param whitelist:
30
36
 
31
37
  ```ruby
32
38
  My::Application.routes.draw do
33
- get "some/route/:id" => redirect_with_params { |path_params, request| "/some/new/route/#{path_params[:id]}" }
39
+ get "some/route/:id" => redirect_with_params(:source) { |path_params, request| "/some/new/route/#{path_params[:id]}" }
34
40
  end
35
41
  ```
36
42
 
37
- Navigating your browser to `/some/route/123?my_param=zebra` will redirect you to `/some/new/route/123?my_param=zebra`
43
+ Navigating your browser to `/some/route/123?my_param=zebra&source=web` will redirect you to `/some/new/route/123?source=web`
38
44
 
39
45
  ## Contributing
40
46
 
@@ -0,0 +1,40 @@
1
+ require 'action_dispatch/routing'
2
+ require 'action_dispatch/routing/redirection'
3
+
4
+ module RedirectWithParams
5
+ module Extensions
6
+
7
+ module Routing
8
+ class ParamPreservingRedirect < ActionDispatch::Routing::Redirect
9
+ attr_reader :permitted_params
10
+ def initialize(status, query_params = [], block)
11
+ @permitted_params = query_params
12
+ @permitted_params.map! { |k| k.to_sym }
13
+ super(status, block)
14
+ end
15
+
16
+ def path(params, request)
17
+ path_builder = block.call(params, request)
18
+
19
+ allowed_keys = permitted_params
20
+ allowed_keys = request.params.keys.map(&:to_sym) if permitted_params.empty?
21
+ allowed_keys -= params.keys(&:to_sym)
22
+
23
+ allowed_params = request.params.select { |k,v| allowed_keys.include?(k.to_sym) }
24
+ path_builder << "?" << allowed_params.to_query unless allowed_params.empty?
25
+
26
+ path_builder
27
+ end
28
+ end
29
+ end
30
+
31
+ module Redirection
32
+ def redirect_with_params(*args, &block)
33
+ options = args.extract_options!
34
+ status = options.delete(:status) || 301
35
+ RedirectWithParams::Extensions::Routing::ParamPreservingRedirect.new(status, args.to_a, block)
36
+ end
37
+ end
38
+
39
+ end
40
+ end
@@ -0,0 +1,10 @@
1
+ module RedirectWithParams
2
+ class Railtie < Rails::Railtie
3
+ initializer 'redirect_with_params.initialize' do
4
+ require 'redirect_with_params/extensions'
5
+
6
+ ActionDispatch::Routing.send(:include, RedirectWithParams::Extensions::Routing)
7
+ ActionDispatch::Routing::Redirection.send(:include, RedirectWithParams::Extensions::Redirection)
8
+ end
9
+ end
10
+ end
@@ -1,3 +1,3 @@
1
1
  module RedirectWithParams
2
- VERSION = "1.0.0"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -1,41 +1,7 @@
1
- require "redirect_with_params/version"
1
+ require 'redirect_with_params/version'
2
2
 
3
3
  if defined?(Rails)
4
- module ActionDispatch::Routing
5
- class RedirectWithParams < ActionDispatch::Routing::Redirect
6
- attr_reader :permitted_params
7
- def initialize(status, query_params = [], block)
8
- @permitted_params = query_params
9
- @permitted_params.map! { |k| k.to_sym }
10
- super(status, block)
11
- end
12
-
13
- def path(params, request)
14
- path_builder = block.call(params, request)
15
-
16
- allowed_keys = permitted_params
17
- allowed_keys = request.params.keys.map(&:to_sym) if permitted_params.empty?
18
- allowed_keys -= params.keys(&:to_sym)
19
-
20
- allowed_params = request.params.select { |k,v| allowed_keys.include?(k.to_sym) }
21
- path_builder << "?" << allowed_params.to_query unless allowed_params.empty?
22
-
23
- path_builder
24
- end
25
-
26
- end
27
-
28
- module Redirection
29
- def redirect_with_params(*args, &block)
30
- options = args.extract_options!
31
- status = options.delete(:status) || 301
32
- ActionDispatch::Routing::RedirectWithParams.new(status, args.to_a, block)
33
- end
34
- end
35
- end
36
-
4
+ require 'redirect_with_params/railtie'
37
5
  else
38
- STDERR.puts "redirect_with_params is a rails gem, but rails was not found."
6
+ STDERR.puts "redirect_with_params requires Rails, but Rails was not found"
39
7
  end
40
-
41
- module RedirectWithParams; end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redirect_with_params
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kyle Dayton
@@ -65,6 +65,8 @@ files:
65
65
  - README.md
66
66
  - Rakefile
67
67
  - lib/redirect_with_params.rb
68
+ - lib/redirect_with_params/extensions.rb
69
+ - lib/redirect_with_params/railtie.rb
68
70
  - lib/redirect_with_params/version.rb
69
71
  - redirect_with_params.gemspec
70
72
  homepage: https://github.com/kyledayton/redirect_with_params