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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fcba9e0da62cef22f6a10be854b3f87576ca42959a07e73ba6f5db15b4cc3832
4
- data.tar.gz: 9f34f3aff5de4d3aaa00b8c1091ac4d577e5eaba0bbe68d35f59af9a8aa720bc
3
+ metadata.gz: 7fe873eb03f68ceb7c4a7f3e222f58e3b293f9a72866305707498c5a8f6cb778
4
+ data.tar.gz: b6f41e0ff74d1a44a165bdbf9ba9368cfb0cd98b4add0dcbe80e4d906add36b1
5
5
  SHA512:
6
- metadata.gz: c0cd32d51cbd4ff13e175ce8f9ada37458325c9d51e464569fc7969661ef29f1966f155bba0850de004ad11a773fd2b646a2cfcf3aff3cc488c0dd10a0364fb9
7
- data.tar.gz: bf5d57f3cb6c783a6af545c0d22b183556ad0280b4708b9521b286f00cc065ed1f36bd07c296757e4817c8fc8d948ecbbff542643e7dcc7f50762b33a0d5d102
6
+ metadata.gz: ff2288172a6d89a2bcecd4e8e7ba21375232478cc41d4488451f9e44ebcfb9bd29f5cf08a2418083d8699c78dfea18d72e0a7567ab788d09100f9c0fb5db6bd4
7
+ data.tar.gz: dc765ac84630960b6f0d70dcc3de0d1bedc7e21592053eadc9f6f6e34e13142ac46d097c3735ef55667788024fe77aaa35ef6620198b2c6ae0cc8a0af4acfde6
@@ -4,6 +4,7 @@ rvm:
4
4
  - 2.4
5
5
  - 2.5
6
6
  - 2.6
7
+ - 2.7
7
8
  gemfile:
8
9
  - gemfiles/rails50.gemfile
9
10
  - gemfiles/rails51.gemfile
@@ -1,5 +1,10 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 1.2.0
4
+
5
+ * Support multiple configs.
6
+ * Support hidden fields for `form_with` with GET method. (Rails => 5.1)
7
+
3
8
  ## 1.1.2
4
9
 
5
10
  * Fix url_for helper outside of controller.
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: :show) #=> '/examples/:id?key1=**&key2=**'
38
+ # hash arg
39
+ url_for(action: :index) #=> '/examples?key1=**&key2=**'
40
40
 
41
- # string and active model don't keep parameters by defalut
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 set keep_params: false
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
- Enable only specific class of url_for:
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
- # enable only hash like url_for(action: :show)
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
- # enable only string like url_for('/examples')
74
+ # string arg
59
75
  keep_params :key1, :key2, for: :string
76
+ url_for('/examples') #=> '/examples?key1=**&key2=**'
60
77
 
61
- # enable only model like url_for(@example)
78
+ # model arg
62
79
  keep_params :key1, :key2, for: :model
80
+ url_for(@example) #=> '/examples/:id?key1=**&key2=**'
63
81
  ```
64
82
 
65
- Specify default options of url_for:
83
+ `:for` allows to set multiple argument type as follows:
66
84
 
67
85
  ```ruby
68
- keep_params :key1, :key2, url_options: { fixed_param: :something }
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 nested_examples)
96
+ keep_params :key1, :key2, to: %w(examples examples2)
77
97
  end
78
98
 
79
- class NestedExamplesController < ApplicationController
99
+ class Examples2Controller < ApplicationController
80
100
  include ParamsKeeper::Controller
81
- keep_params :key1, :key2, to: %w(examples nested_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::Controller
2
- extend ActiveSupport::Concern
1
+ module ParamsKeeper
2
+ module Controller
3
+ extend ActiveSupport::Concern
3
4
 
4
- included do
5
- class_attribute :keep_params_config
6
- helper ParamsKeeper::Helper
7
- end
5
+ included do
6
+ class_attribute :keep_params_configs
7
+ helper ParamsKeeper::Helper
8
+ end
8
9
 
9
- def url_for(options = nil)
10
- ParamsKeeper::Resolver.new(self, self, options).resolve || super
11
- end
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
- class_methods do
14
- def keep_params(*args)
15
- self.keep_params_config = {
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
@@ -1,9 +1,23 @@
1
- module ParamsKeeper::Helper
2
- def url_for(options = nil)
3
- if controller
4
- ParamsKeeper::Resolver.new(self, controller, options).resolve || super
5
- else
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
- class ParamsKeeper::Resolver
2
- def initialize(klass, controller, options)
3
- @klass = klass
4
- @controller = controller
5
- @options = options
6
- end
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
- def resolve
9
- return if !configured? || !enable_options? || !target_options?
9
+ def call
10
+ return {} if configs.blank? || skip_url_options?
10
11
 
11
- if @options.is_a?(Hash)
12
- resolve_from_hash
13
- else
14
- resolve_from_routing
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
- private
19
-
20
- def resolve_from_hash
21
- if link_to_target_controller?(@options)
22
- base_url_for(self.class.merge_params(@options, @controller.params, config[:keys]))
23
- else
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
- def resolve_from_routing
29
- url = base_url_for(@options)
30
- url_opts = recognize_path(url)
27
+ private
31
28
 
32
- if url_opts && link_to_target_controller?(url_opts)
33
- base_url_for(self.class.merge_params(url_opts, @controller.params, config[:keys]))
34
- else
35
- url
29
+ def configs
30
+ @controller.class.keep_params_configs
36
31
  end
37
- end
38
32
 
39
- def link_to_target_controller?(options)
40
- controller = options[:controller].to_s
41
- if config[:to].present?
42
- target = controller.present? ? [controller] : [@controller.controller_name, @controller.controller_path]
43
- (Array(config[:to]).map(&:to_s) & target).present?
44
- else
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
- def config
50
- @controller.class.keep_params_config
51
- end
41
+ def target_config?(config)
42
+ target_url_options?(config) && target_controller?(config)
43
+ end
52
44
 
53
- def configured?
54
- config && config[:keys].present?
55
- end
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
- def enable_options?
58
- !@options.is_a?(Hash) || @options.delete(:keep_params) != false
59
- end
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
- def target_options?
62
- fors = Array(config[:for] || :hash)
63
- (fors.include?(:hash) && @options.is_a?(Hash)) ||
64
- (fors.include?(:string) && @options.is_a?(String)) ||
65
- (fors.include?(:model) && @options.class.respond_to?(:model_name))
66
- end
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
- def recognize_path(url)
69
- Rails.application.routes.recognize_path(url)
70
- rescue ActionController::RoutingError
71
- nil
72
- end
68
+ def current_controllers
69
+ [@controller.controller_name, @controller.controller_path]
70
+ end
73
71
 
74
- def base_url_for(options)
75
- options = options.merge(config[:url_options]) if options.is_a?(Hash) && config[:url_options]
76
- @klass.method(:url_for).super_method.call(options)
77
- end
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
- class << self
80
- def merge_params(options, params, keys)
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
@@ -1,3 +1,3 @@
1
1
  module ParamsKeeper
2
- VERSION = "1.1.2"
2
+ VERSION = "1.2.0"
3
3
  end
@@ -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.1.2
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-03-15 00:00:00.000000000 Z
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.0.3
132
+ rubygems_version: 3.1.2
132
133
  signing_key:
133
134
  specification_version: 4
134
135
  summary: keep specific parameters through links.
@@ -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
data/bin/setup DELETED
@@ -1,8 +0,0 @@
1
- #!/usr/bin/env bash
2
- set -euo pipefail
3
- IFS=$'\n\t'
4
- set -vx
5
-
6
- bundle install
7
-
8
- # Do any other automated setup that you need to do here