params_keeper_rails 1.1.2 → 1.2.2

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: 0e45dfc5db4050646b12ac16f67cd674add86504f033ea0e53994b70899fe965
4
+ data.tar.gz: ed817a1bea1033e7f72a6e606b886af52bdd4c0f7d408caf83dcec2837cfb93d
5
5
  SHA512:
6
- metadata.gz: c0cd32d51cbd4ff13e175ce8f9ada37458325c9d51e464569fc7969661ef29f1966f155bba0850de004ad11a773fd2b646a2cfcf3aff3cc488c0dd10a0364fb9
7
- data.tar.gz: bf5d57f3cb6c783a6af545c0d22b183556ad0280b4708b9521b286f00cc065ed1f36bd07c296757e4817c8fc8d948ecbbff542643e7dcc7f50762b33a0d5d102
6
+ metadata.gz: dd080fe7ffe4708d8d79c2cbb0918b485d6f1fe5d437662a0b39935267264a23ce2edbad4f5128ab67526bb6054301ab27d7fcec67b4a15c7c37af8621d5ed80
7
+ data.tar.gz: 825ef3b8765a00fc087e6e0c989e86c0fb92953a04542bb3d6f489ec0effd4505bde9de6091eba58420cf99815632e3b5fb3dd29a1c9cc74869d8050917800aa
@@ -0,0 +1,56 @@
1
+ name: CI
2
+
3
+ on: [push, pull_request]
4
+
5
+ jobs:
6
+ test:
7
+ runs-on: ubuntu-20.04
8
+ strategy:
9
+ fail-fast: false
10
+ matrix:
11
+ ruby: [2.3, 2.4, 2.5, 2.6, 2.7, '3.0', 3.1]
12
+ gemfile: ['rails50', 'rails51', 'rails52', 'rails60', 'rails61', 'rails70']
13
+ exclude:
14
+ - ruby: 2.3
15
+ gemfile: rails60
16
+ - ruby: 2.3
17
+ gemfile: rails61
18
+ - ruby: 2.3
19
+ gemfile: rails70
20
+ - ruby: 2.4
21
+ gemfile: rails60
22
+ - ruby: 2.4
23
+ gemfile: rails61
24
+ - ruby: 2.4
25
+ gemfile: rails70
26
+ - ruby: 2.5
27
+ gemfile: rails70
28
+ - ruby: 2.6
29
+ gemfile: rails70
30
+ - ruby: 3.0
31
+ gemfile: rails50
32
+ - ruby: 3.0
33
+ gemfile: rails51
34
+ - ruby: 3.0
35
+ gemfile: rails52
36
+ - ruby: 3.1
37
+ gemfile: rails50
38
+ - ruby: 3.1
39
+ gemfile: rails51
40
+ - ruby: 3.1
41
+ gemfile: rails52
42
+
43
+ name: ruby ${{ matrix.ruby }}, ${{ matrix.gemfile }}
44
+
45
+ env:
46
+ BUNDLE_GEMFILE: gemfiles/${{ matrix.gemfile }}.gemfile
47
+
48
+ steps:
49
+ - uses: actions/checkout@v2
50
+ - uses: ruby/setup-ruby@v1
51
+ with:
52
+ ruby-version: ${{ matrix.ruby }}
53
+ bundler-cache: true
54
+ - name: Run test
55
+ run: |
56
+ bundle exec rspec
data/.gitignore CHANGED
@@ -1,8 +1,13 @@
1
1
  /.bundle/
2
- /.project
3
2
  /Gemfile.lock
4
3
  /gemfiles/*gemfile.lock
5
4
  /coverage/
6
5
  /log/
7
6
  /pkg/
7
+ /tmp/
8
8
  /spec/dummy/log/
9
+ /spec/dummy/config/database.yml
10
+ /spec/dummy/db/schema*.rb
11
+ /spec/dummy/db/*.sqlite3
12
+ /spec/dummy/log/*.log
13
+ /spec/dummy/tmp/*
data/CHANGELOG.md CHANGED
@@ -1,5 +1,18 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 1.2.2
4
+
5
+ * Put hidden fields after beginning tag of form.
6
+
7
+ ## 1.2.1
8
+
9
+ * Fix order of options to be resolved for hidden fields.
10
+
11
+ ## 1.2.0
12
+
13
+ * Support multiple configs.
14
+ * Support hidden fields for `form_with` with GET method. (Rails => 5.1)
15
+
3
16
  ## 1.1.2
4
17
 
5
18
  * 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.
@@ -1,5 +1,6 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
3
  gem "rails", "~> 6.0.0"
4
+ gem "psych", "~> 3.3.0"
4
5
 
5
6
  gemspec path: "../"
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem "rails", "~> 6.1.0"
4
+
5
+ gemspec path: "../"
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem "rails", "~> 7.0.1"
4
+
5
+ gemspec path: "../"
@@ -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[:url] || options[:model]
15
+ hidden_fields = ParamsKeeper::HiddenFields.new(controller, url_options).call
16
+ if hidden_fields.present?
17
+ html.sub(/^(<form[^>]*>)/) { "#{$1}#{hidden_fields}" }.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.2"
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'
@@ -27,4 +27,5 @@ Gem::Specification.new do |spec|
27
27
  spec.add_development_dependency "rspec"
28
28
  spec.add_development_dependency "rspec-rails"
29
29
  spec.add_development_dependency "simplecov"
30
+ spec.add_development_dependency "webrick"
30
31
  end
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.2
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: 2022-07-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -80,6 +80,20 @@ dependencies:
80
80
  - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: webrick
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
83
97
  description: A rails controller extension for keeping specific parameters through
84
98
  links.
85
99
  email:
@@ -88,24 +102,27 @@ executables: []
88
102
  extensions: []
89
103
  extra_rdoc_files: []
90
104
  files:
105
+ - ".github/workflows/ci.yml"
91
106
  - ".gitignore"
92
107
  - ".rspec"
93
- - ".travis.yml"
94
108
  - CHANGELOG.md
95
109
  - CODE_OF_CONDUCT.md
96
110
  - Gemfile
97
111
  - LICENSE.txt
98
112
  - README.md
99
113
  - Rakefile
100
- - bin/console
101
- - bin/setup
102
114
  - gemfiles/rails50.gemfile
103
115
  - gemfiles/rails51.gemfile
104
116
  - gemfiles/rails52.gemfile
105
117
  - gemfiles/rails60.gemfile
118
+ - gemfiles/rails61.gemfile
119
+ - gemfiles/rails70.gemfile
120
+ - lib/params_keeper/config.rb
106
121
  - lib/params_keeper/controller.rb
107
122
  - lib/params_keeper/helper.rb
123
+ - lib/params_keeper/hidden_fields.rb
108
124
  - lib/params_keeper/resolver.rb
125
+ - lib/params_keeper/url_for.rb
109
126
  - lib/params_keeper/version.rb
110
127
  - lib/params_keeper_rails.rb
111
128
  - params_keeper_rails.gemspec
@@ -128,7 +145,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
128
145
  - !ruby/object:Gem::Version
129
146
  version: '0'
130
147
  requirements: []
131
- rubygems_version: 3.0.3
148
+ rubygems_version: 3.3.3
132
149
  signing_key:
133
150
  specification_version: 4
134
151
  summary: keep specific parameters through links.
data/.travis.yml DELETED
@@ -1,19 +0,0 @@
1
- language: ruby
2
- rvm:
3
- - 2.3
4
- - 2.4
5
- - 2.5
6
- - 2.6
7
- gemfile:
8
- - gemfiles/rails50.gemfile
9
- - gemfiles/rails51.gemfile
10
- - gemfiles/rails52.gemfile
11
- - gemfiles/rails60.gemfile
12
- matrix:
13
- exclude:
14
- - rvm: 2.3
15
- gemfile: gemfiles/rails60.gemfile
16
- - rvm: 2.4
17
- gemfile: gemfiles/rails60.gemfile
18
- script:
19
- - bundle exec rspec
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
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