params_keeper_rails 1.1.2 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +1 -0
- data/CHANGELOG.md +5 -0
- data/README.md +42 -14
- data/lib/params_keeper/config.rb +12 -0
- data/lib/params_keeper/controller.rb +27 -19
- data/lib/params_keeper/helper.rb +20 -6
- data/lib/params_keeper/hidden_fields.rb +19 -0
- data/lib/params_keeper/resolver.rb +67 -65
- data/lib/params_keeper/url_for.rb +25 -0
- data/lib/params_keeper/version.rb +1 -1
- data/lib/params_keeper_rails.rb +3 -0
- metadata +6 -5
- data/bin/console +0 -14
- data/bin/setup +0 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7fe873eb03f68ceb7c4a7f3e222f58e3b293f9a72866305707498c5a8f6cb778
|
4
|
+
data.tar.gz: b6f41e0ff74d1a44a165bdbf9ba9368cfb0cd98b4add0dcbe80e4d906add36b1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ff2288172a6d89a2bcecd4e8e7ba21375232478cc41d4488451f9e44ebcfb9bd29f5cf08a2418083d8699c78dfea18d72e0a7567ab788d09100f9c0fb5db6bd4
|
7
|
+
data.tar.gz: dc765ac84630960b6f0d70dcc3de0d1bedc7e21592053eadc9f6f6e34e13142ac46d097c3735ef55667788024fe77aaa35ef6620198b2c6ae0cc8a0af4acfde6
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -29,59 +29,87 @@ class ExamplesController < ApplicationController
|
|
29
29
|
keep_params :key1, :key2
|
30
30
|
```
|
31
31
|
|
32
|
-
Parameters are kept if destination controller is same as current controller.
|
32
|
+
Parameters are kept via `url_for` if destination controller is same as current controller.
|
33
33
|
For example:
|
34
34
|
|
35
35
|
```ruby
|
36
36
|
GET "/examples?key1=**&key2=**"
|
37
37
|
|
38
|
-
# hash
|
39
|
-
url_for(action: :
|
38
|
+
# hash arg
|
39
|
+
url_for(action: :index) #=> '/examples?key1=**&key2=**'
|
40
40
|
|
41
|
-
#
|
41
|
+
# parameters are not kept if string or model arg is specified
|
42
42
|
url_for('/examples') #=> '/examples'
|
43
43
|
url_for(@example) #=> '/examples/:id'
|
44
44
|
|
45
45
|
# parameters are not kept if destination controller is different from current controller
|
46
46
|
url_for(controller: 'examples2', action: :index) #=> '/examples2'
|
47
47
|
|
48
|
-
# parameters are not kept if you
|
48
|
+
# parameters are not kept if you put "keep_params: false" into args
|
49
49
|
url_for(action: :show, keep_params: false) #=> '/examples/:id'
|
50
50
|
```
|
51
51
|
|
52
|
-
|
52
|
+
Parameters are kept for form with GET method via hidden fields.
|
53
|
+
This feature is supported by `form_with` for rails >= 5.1.
|
53
54
|
|
54
55
|
```ruby
|
55
|
-
|
56
|
+
<%= form_with url: { action: :index}, method: :get do %>
|
57
|
+
<%= submit_tag 'submit' %>
|
58
|
+
<% end %>
|
59
|
+
#=> ...<input type="hidden" name="key1" value="**" />
|
60
|
+
# <input type="hidden" name="key2" value="**" /></form>
|
61
|
+
```
|
62
|
+
|
63
|
+
## Options
|
64
|
+
|
65
|
+
### Argument type
|
66
|
+
|
67
|
+
Enable only specific argument type of url_for:
|
68
|
+
|
69
|
+
```ruby
|
70
|
+
# hash arg (same as default behaviour)
|
56
71
|
keep_params :key1, :key2, for: :hash
|
72
|
+
url_for(action: :index) #=> '/examples?key1=**&key2=**'
|
57
73
|
|
58
|
-
#
|
74
|
+
# string arg
|
59
75
|
keep_params :key1, :key2, for: :string
|
76
|
+
url_for('/examples') #=> '/examples?key1=**&key2=**'
|
60
77
|
|
61
|
-
#
|
78
|
+
# model arg
|
62
79
|
keep_params :key1, :key2, for: :model
|
80
|
+
url_for(@example) #=> '/examples/:id?key1=**&key2=**'
|
63
81
|
```
|
64
82
|
|
65
|
-
|
83
|
+
`:for` allows to set multiple argument type as follows:
|
66
84
|
|
67
85
|
```ruby
|
68
|
-
keep_params :key1, :key2,
|
86
|
+
keep_params :key1, :key2, for: [:hash, :model]
|
69
87
|
```
|
70
88
|
|
89
|
+
### Multiple controllers
|
90
|
+
|
71
91
|
Keep parameters throught multiple controllers:
|
72
92
|
|
73
93
|
```ruby
|
74
94
|
class ExamplesController < ApplicationController
|
75
95
|
include ParamsKeeper::Controller
|
76
|
-
keep_params :key1, :key2, to: %w(examples
|
96
|
+
keep_params :key1, :key2, to: %w(examples examples2)
|
77
97
|
end
|
78
98
|
|
79
|
-
class
|
99
|
+
class Examples2Controller < ApplicationController
|
80
100
|
include ParamsKeeper::Controller
|
81
|
-
keep_params :key1, :key2, to: %w(examples
|
101
|
+
keep_params :key1, :key2, to: %w(examples examples2)
|
82
102
|
end
|
83
103
|
```
|
84
104
|
|
105
|
+
### Default parameters
|
106
|
+
|
107
|
+
Specify default parameters:
|
108
|
+
|
109
|
+
```ruby
|
110
|
+
keep_params :key1, :key2, url_options: { fixed_param: :something }
|
111
|
+
```
|
112
|
+
|
85
113
|
## Contributing
|
86
114
|
|
87
115
|
Bug reports and pull requests are welcome on GitHub at https://github.com/kanety/params_keeper_rails.
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module ParamsKeeper
|
2
|
+
class Config
|
3
|
+
attr_accessor :keys, :to, :for, :url_options
|
4
|
+
|
5
|
+
def initialize(keys, options = {})
|
6
|
+
@keys = Array(keys)
|
7
|
+
@to = Array(options[:to])
|
8
|
+
@for = Array(options[:for] || :hash)
|
9
|
+
@url_options = options[:url_options] || {}
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -1,25 +1,33 @@
|
|
1
|
-
module ParamsKeeper
|
2
|
-
|
1
|
+
module ParamsKeeper
|
2
|
+
module Controller
|
3
|
+
extend ActiveSupport::Concern
|
3
4
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
5
|
+
included do
|
6
|
+
class_attribute :keep_params_configs
|
7
|
+
helper ParamsKeeper::Helper
|
8
|
+
end
|
8
9
|
|
9
|
-
|
10
|
-
|
11
|
-
|
10
|
+
def url_for(options = nil)
|
11
|
+
ParamsKeeper::UrlFor.new(self, self, options).call || super
|
12
|
+
end
|
13
|
+
|
14
|
+
def redirect_to(options = {}, response_options = {})
|
15
|
+
return super unless options.is_a?(String)
|
16
|
+
|
17
|
+
url = ParamsKeeper::UrlFor.new(self, self, options).call
|
18
|
+
url ? super(url, response_options) : super
|
19
|
+
end
|
20
|
+
|
21
|
+
class_methods do
|
22
|
+
def keep_params(*args)
|
23
|
+
options = args.last.is_a?(Hash) ? args.pop : {}
|
24
|
+
config = ParamsKeeper::Config.new(args, options)
|
25
|
+
self.keep_params_configs = keep_params_configs.to_a + [config]
|
26
|
+
end
|
12
27
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
keys: nil,
|
17
|
-
to: nil,
|
18
|
-
for: :hash,
|
19
|
-
url_options: nil
|
20
|
-
}
|
21
|
-
self.keep_params_config.merge!(args.last.is_a?(Hash) ? args.pop : {})
|
22
|
-
self.keep_params_config[:keys] = Array(args)
|
28
|
+
def clear_keep_params!
|
29
|
+
self.keep_params_configs = nil
|
30
|
+
end
|
23
31
|
end
|
24
32
|
end
|
25
33
|
end
|
data/lib/params_keeper/helper.rb
CHANGED
@@ -1,9 +1,23 @@
|
|
1
|
-
module ParamsKeeper
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
super
|
1
|
+
module ParamsKeeper
|
2
|
+
module Helper
|
3
|
+
def url_for(url_options = nil)
|
4
|
+
return super unless controller
|
5
|
+
|
6
|
+
ParamsKeeper::UrlFor.new(self, controller, url_options).call || super
|
7
|
+
end
|
8
|
+
|
9
|
+
def form_with(**options, &block)
|
10
|
+
return super unless controller
|
11
|
+
return super if options[:method].to_s.downcase != 'get'
|
12
|
+
|
13
|
+
html = super
|
14
|
+
url_options = options[:model] || options[:url]
|
15
|
+
hidden_fields = ParamsKeeper::HiddenFields.new(controller, url_options).call
|
16
|
+
if hidden_fields.present?
|
17
|
+
html.sub('</form>') { "#{hidden_fields}</form>" }.html_safe
|
18
|
+
else
|
19
|
+
html
|
20
|
+
end
|
7
21
|
end
|
8
22
|
end
|
9
23
|
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module ParamsKeeper
|
2
|
+
class HiddenFields
|
3
|
+
def initialize(controller, url_options)
|
4
|
+
@controller = controller
|
5
|
+
@url_options = url_options
|
6
|
+
end
|
7
|
+
|
8
|
+
def call
|
9
|
+
return if @controller.class.keep_params_configs.blank?
|
10
|
+
|
11
|
+
params = ParamsKeeper::Resolver.new(@controller, @url_options).call
|
12
|
+
return if params.blank?
|
13
|
+
|
14
|
+
CGI.parse(params.to_query).flat_map do |key, values|
|
15
|
+
values.map { |value| @controller.view_context.hidden_field_tag(key, value, id: nil) }
|
16
|
+
end.join.html_safe
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -1,85 +1,87 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
1
|
+
module ParamsKeeper
|
2
|
+
class Resolver
|
3
|
+
def initialize(controller, url_options)
|
4
|
+
@controller = controller
|
5
|
+
@url_options = url_options
|
6
|
+
@cache = {}
|
7
|
+
end
|
7
8
|
|
8
|
-
|
9
|
-
|
9
|
+
def call
|
10
|
+
return {} if configs.blank? || skip_url_options?
|
10
11
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
12
|
+
configs.each_with_object({}) do |config, params|
|
13
|
+
if target_config?(config)
|
14
|
+
params.merge!(extract_params(config))
|
15
|
+
end
|
16
|
+
end
|
15
17
|
end
|
16
|
-
end
|
17
18
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
base_url_for(@options)
|
19
|
+
def url_options_hash
|
20
|
+
if @url_options.is_a?(Hash)
|
21
|
+
@url_options
|
22
|
+
else
|
23
|
+
recognize_path(base_url_for(@url_options))
|
24
|
+
end
|
25
25
|
end
|
26
|
-
end
|
27
26
|
|
28
|
-
|
29
|
-
url = base_url_for(@options)
|
30
|
-
url_opts = recognize_path(url)
|
27
|
+
private
|
31
28
|
|
32
|
-
|
33
|
-
|
34
|
-
else
|
35
|
-
url
|
29
|
+
def configs
|
30
|
+
@controller.class.keep_params_configs
|
36
31
|
end
|
37
|
-
end
|
38
32
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
controller.blank? || controller.in?([@controller.controller_name, @controller.controller_path])
|
33
|
+
def skip_url_options?
|
34
|
+
if @url_options.is_a?(Hash)
|
35
|
+
@url_options.delete(:keep_params) == false
|
36
|
+
else
|
37
|
+
false
|
38
|
+
end
|
46
39
|
end
|
47
|
-
end
|
48
40
|
|
49
|
-
|
50
|
-
|
51
|
-
|
41
|
+
def target_config?(config)
|
42
|
+
target_url_options?(config) && target_controller?(config)
|
43
|
+
end
|
52
44
|
|
53
|
-
|
54
|
-
|
55
|
-
|
45
|
+
def target_url_options?(config)
|
46
|
+
(config.for.include?(:hash) && @url_options.is_a?(Hash)) ||
|
47
|
+
(config.for.include?(:string) && @url_options.is_a?(String)) ||
|
48
|
+
(config.for.include?(:model) && @url_options.class.respond_to?(:model_name))
|
49
|
+
end
|
56
50
|
|
57
|
-
|
58
|
-
|
59
|
-
|
51
|
+
def target_controller?(config)
|
52
|
+
dests = destination_controllers(url_options_hash)
|
53
|
+
if config.to.present?
|
54
|
+
(dests & config.to.map(&:to_s)).present?
|
55
|
+
else
|
56
|
+
(dests & current_controllers).present?
|
57
|
+
end
|
58
|
+
end
|
60
59
|
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
60
|
+
def destination_controllers(url_options_hash)
|
61
|
+
if url_options_hash[:controller].present?
|
62
|
+
[url_options_hash[:controller].to_s]
|
63
|
+
else
|
64
|
+
current_controllers
|
65
|
+
end
|
66
|
+
end
|
67
67
|
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
nil
|
72
|
-
end
|
68
|
+
def current_controllers
|
69
|
+
[@controller.controller_name, @controller.controller_path]
|
70
|
+
end
|
73
71
|
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
72
|
+
def recognize_path(url)
|
73
|
+
@cache[url] ||= Rails.application.routes.recognize_path(url)
|
74
|
+
rescue ActionController::RoutingError
|
75
|
+
{}
|
76
|
+
end
|
77
|
+
|
78
|
+
def extract_params(config)
|
79
|
+
params = @controller.request.params.deep_symbolize_keys
|
80
|
+
params.slice(*config.keys).merge(config.url_options)
|
81
|
+
end
|
78
82
|
|
79
|
-
|
80
|
-
|
81
|
-
keeps = params.to_unsafe_h.deep_symbolize_keys.slice(*keys.to_a)
|
82
|
-
options.reverse_merge(keeps)
|
83
|
+
def base_url_for(url_options)
|
84
|
+
@controller.method(:url_for).super_method.call(url_options)
|
83
85
|
end
|
84
86
|
end
|
85
87
|
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module ParamsKeeper
|
2
|
+
class UrlFor
|
3
|
+
def initialize(caller, controller, url_options)
|
4
|
+
@caller = caller
|
5
|
+
@controller = controller
|
6
|
+
@url_options = url_options
|
7
|
+
end
|
8
|
+
|
9
|
+
def call
|
10
|
+
return if @controller.class.keep_params_configs.blank?
|
11
|
+
|
12
|
+
resolver = ParamsKeeper::Resolver.new(@controller, @url_options)
|
13
|
+
params = resolver.call
|
14
|
+
return if params.blank?
|
15
|
+
|
16
|
+
base_url_for(resolver.url_options_hash.reverse_merge(params))
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def base_url_for(url_options)
|
22
|
+
@caller.method(:url_for).super_method.call(url_options)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/lib/params_keeper_rails.rb
CHANGED
@@ -1,5 +1,8 @@
|
|
1
1
|
require 'active_support'
|
2
2
|
require 'params_keeper/version'
|
3
|
+
require 'params_keeper/config'
|
3
4
|
require 'params_keeper/resolver'
|
5
|
+
require 'params_keeper/url_for'
|
6
|
+
require 'params_keeper/hidden_fields'
|
4
7
|
require 'params_keeper/helper'
|
5
8
|
require 'params_keeper/controller'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: params_keeper_rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yoshikazu Kaneta
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-09-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -97,15 +97,16 @@ files:
|
|
97
97
|
- LICENSE.txt
|
98
98
|
- README.md
|
99
99
|
- Rakefile
|
100
|
-
- bin/console
|
101
|
-
- bin/setup
|
102
100
|
- gemfiles/rails50.gemfile
|
103
101
|
- gemfiles/rails51.gemfile
|
104
102
|
- gemfiles/rails52.gemfile
|
105
103
|
- gemfiles/rails60.gemfile
|
104
|
+
- lib/params_keeper/config.rb
|
106
105
|
- lib/params_keeper/controller.rb
|
107
106
|
- lib/params_keeper/helper.rb
|
107
|
+
- lib/params_keeper/hidden_fields.rb
|
108
108
|
- lib/params_keeper/resolver.rb
|
109
|
+
- lib/params_keeper/url_for.rb
|
109
110
|
- lib/params_keeper/version.rb
|
110
111
|
- lib/params_keeper_rails.rb
|
111
112
|
- params_keeper_rails.gemspec
|
@@ -128,7 +129,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
128
129
|
- !ruby/object:Gem::Version
|
129
130
|
version: '0'
|
130
131
|
requirements: []
|
131
|
-
rubygems_version: 3.
|
132
|
+
rubygems_version: 3.1.2
|
132
133
|
signing_key:
|
133
134
|
specification_version: 4
|
134
135
|
summary: keep specific parameters through links.
|
data/bin/console
DELETED
@@ -1,14 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
|
3
|
-
require "bundler/setup"
|
4
|
-
require "params_keeper/controller"
|
5
|
-
|
6
|
-
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
-
# with your gem easier. You can also use a different console, if you like.
|
8
|
-
|
9
|
-
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
-
# require "pry"
|
11
|
-
# Pry.start
|
12
|
-
|
13
|
-
require "irb"
|
14
|
-
IRB.start
|