sticky_params 2.0.0 → 2.1.1

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
  SHA256:
3
- metadata.gz: 9038e51804afe83d423ecc4b4903df48a31fd04923a2ba0e966589ebbbd45585
4
- data.tar.gz: fad2fdf5976803a1f4ca0bc4c913ea410a85bd0ded788886b7400b7744d8fca4
3
+ metadata.gz: dd99a57715a3846a6f11b9e4922883177861cececfa89b11c815db6d1723d8f1
4
+ data.tar.gz: 2ccceedbeeecd024d6074ea9ba02c2d52b9385983392a11cd23a6f4317275ce5
5
5
  SHA512:
6
- metadata.gz: 76ee65c4c4889c7e54e3310a0f16bcee2414d98ab79353be63e12685c30b2df98b7971bf8f311ed24d9b5f144b276ba563ed69c695e877e2f56762b3be4e246b
7
- data.tar.gz: 273163c5f66741711f666326b3960bb120ee657f880a1868eedea7cdc847112cf9152cf5dcf0d50276f6d79b13eff41cffece3c563b24eb849d1b15ac55edbd3
6
+ metadata.gz: 163f301446312d74547fb8c31d5cc986ae12a01c611620104c9109a733f33af70e2123e69024a1c7b8312bb30976a87b357791d09e3fe6d05a96784067fb399b
7
+ data.tar.gz: 262fe467d410e449540c10605450f3bb0b839ecd73330f0d1fe1b77ae3d4a8dda5e2a4daac0661a709684e151075a65257f34ac7add620ffea637d92cdfe926b
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # StickyParams
2
2
 
3
- A little gem that automaticly remembers the request parameters between requests without hassle.
3
+ A little gem that automatically remembers the request parameters between requests without hassle.
4
4
  For example for remembering the filtering and sorting of a list, when switching to a detail screen and back.
5
5
 
6
6
  ```ruby
@@ -12,13 +12,20 @@ class MyController < ApplicationController
12
12
  end
13
13
  ```
14
14
 
15
- ## Release Notes 2.0
15
+ ## Release Notes 2.1
16
16
 
17
- Release 2.0 uses ActionController::Parameters instead of hashes.
17
+ Release 2.1.1, Add the method `with_prefix`, to invoke a block of code with another sticky_params prefix.
18
+ It also deduplicates code by moving it to the `StickyParams::BaseParams` class.
19
+
20
+ Release 2.1, Sticky Params doesn't put ActionController::Parameters in a session anymore.
21
+ ActionController::Parameters were put directly into a session in 2.0. Which worked in rails < 7.
22
+ In rails 7 this results in a `TypeError (can't dump IO)` when the session data is serialized.
23
+
24
+ Release 2.0, uses ActionController::Parameters instead of hashes.
18
25
  This enable the usage of the strong parameter permit construct.
19
26
  (In other words, sticky_params will work just like normal params)
20
27
 
21
- To get the 1.0 behaviour, you can add the following to your ApplicationController.
28
+ To get the 1.0 behavior, you can add the following to your ApplicationController.
22
29
 
23
30
  ```ruby
24
31
  def sticky_params
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ module StickyParams
4
+ class BaseParams
5
+ attr_reader :controller
6
+ attr_accessor :prefix
7
+
8
+ def initialize(controller)
9
+ @controller = controller
10
+ @prefix = "#{controller.controller_name}_#{controller.action_name}_"
11
+ end
12
+
13
+ def []=(name, value)
14
+ session_param_name = "#{prefix}#{name}"
15
+ controller.session['sticky_params'] ||= {}
16
+ controller.session['sticky_params'][session_param_name] = controller.params[name] = value
17
+ end
18
+
19
+ # clears all sticky params for the current controller/action name
20
+ def clear!
21
+ if controller.session['sticky_params'].present?
22
+ controller.session['sticky_params'].reject! do |key, _value|
23
+ key.index(prefix) == 0
24
+ end
25
+ end
26
+ end
27
+
28
+ # clears all sticky parameters
29
+ def clear_all!
30
+ controller.session.delete('sticky_params')
31
+ end
32
+
33
+ # invokes the given block with another sticky_params prefix
34
+ # (accessing other session-params withing the block)
35
+ def with_prefix(new_prefix, &)
36
+ old_prefix = prefix
37
+ begin
38
+ self.prefix = new_prefix
39
+ yield self
40
+ ensure
41
+ self.prefix = old_prefix
42
+ end
43
+ end
44
+ end
45
+ end
@@ -1,13 +1,5 @@
1
1
  module StickyParams
2
- class SessionParams
3
- attr_reader :controller
4
- attr_accessor :prefix
5
-
6
- def initialize(controller)
7
- @controller = controller
8
- @prefix = "#{controller.controller_name}_#{controller.action_name}_"
9
- end
10
-
2
+ class SessionParams < BaseParams
11
3
  def [](name)
12
4
  session_param_name = "#{prefix}#{name}"
13
5
  controller.session['sticky_params'] ||= {}
@@ -22,25 +14,5 @@ module StickyParams
22
14
  controller.session['sticky_params'][session_param_name]
23
15
  end
24
16
  end
25
-
26
- def []=(name, value)
27
- session_param_name = "#{prefix}#{name}"
28
- controller.session['sticky_params'] ||= {}
29
- controller.session['sticky_params'][session_param_name] = controller.params[name] = value
30
- end
31
-
32
- # clears all sticky params for the current controller/action name
33
- def clear!
34
- if controller.session['sticky_params'].present?
35
- controller.session['sticky_params'].reject! do |key, _value|
36
- key.index(prefix) == 0
37
- end
38
- end
39
- end
40
-
41
- # clears all sticky parameters
42
- def clear_all!
43
- controller.session.delete('sticky_params')
44
- end
45
17
  end
46
18
  end
@@ -1,16 +1,13 @@
1
1
  module StickyParams
2
- class StrongSessionParams
3
- attr_reader :controller
4
- attr_accessor :prefix
5
-
6
- def initialize(controller)
7
- @controller = controller
8
- @prefix = "#{controller.controller_name}_#{controller.action_name}_"
9
- end
10
-
2
+ class StrongSessionParams < BaseParams
11
3
  def fetch_from_params(name, session_param_name)
12
4
  if controller.params[name].present?
13
- controller.session['sticky_params'][session_param_name] = controller.params[name]
5
+ if controller.params[name].is_a?(ActionController::Parameters)
6
+ controller.session['sticky_params'][session_param_name] = controller.params[name].to_unsafe_hash
7
+ else
8
+ controller.session['sticky_params'][session_param_name] = controller.params[name]
9
+ end
10
+ controller.params[name]
14
11
  else
15
12
  controller.session['sticky_params'].delete session_param_name
16
13
  nil
@@ -19,44 +16,18 @@ module StickyParams
19
16
 
20
17
  def fetch_from_session(session_param_name)
21
18
  result = controller.session['sticky_params'][session_param_name]
22
-
23
19
  # covert hash to action parameters for simulating a normal param
24
- if result.is_a?(Hash)
25
- result = ActionController::Parameters.new(result)
26
- controller.session['sticky_params'][session_param_name] = result
27
- end
28
-
29
- result
20
+ result.is_a?(Hash) ? ActionController::Parameters.new(result) : result
30
21
  end
31
22
 
32
23
  def [](name)
33
24
  session_param_name = "#{prefix}#{name}"
34
- controller.session['sticky_params'] ||= ActionController::Parameters.new
25
+ controller.session['sticky_params'] ||= {}
35
26
  if controller.params[name]
36
27
  fetch_from_params(name, session_param_name)
37
28
  elsif controller.session['sticky_params'][session_param_name]
38
29
  fetch_from_session(session_param_name)
39
30
  end
40
31
  end
41
-
42
- def []=(name, value)
43
- session_param_name = "#{prefix}#{name}"
44
- controller.session['sticky_params'] ||= ActionController::Parameters.new
45
- controller.session['sticky_params'][session_param_name] = controller.params[name] = value
46
- end
47
-
48
- # clears all sticky params for the current controller/action name
49
- def clear!
50
- if controller.session['sticky_params'].present?
51
- controller.session['sticky_params'].reject! do |key, _value|
52
- key.index(prefix) == 0
53
- end
54
- end
55
- end
56
-
57
- # clears all sticky parameters
58
- def clear_all!
59
- controller.session.delete('sticky_params')
60
- end
61
32
  end
62
- end
33
+ end
@@ -1,3 +1,3 @@
1
1
  module StickyParams
2
- VERSION = "2.0.0"
2
+ VERSION = "2.1.1"
3
3
  end
data/lib/sticky_params.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require "sticky_params/version"
2
2
  require "sticky_params/railtie" if defined?(Rails)
3
+ require "sticky_params/base_params"
3
4
  require "sticky_params/session_params"
4
5
  require "sticky_params/strong_session_params"
5
6
 
@@ -1,23 +1,21 @@
1
- lib = File.expand_path('../lib', __FILE__)
1
+ lib = File.expand_path("lib", __dir__)
2
2
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
3
  require 'sticky_params/version'
4
4
 
5
5
  Gem::Specification.new do |spec|
6
- spec.name = 'sticky_params'
7
- spec.version = StickyParams::VERSION
6
+ spec.name = 'sticky_params'
7
+ spec.version = StickyParams::VERSION
8
8
 
9
- spec.authors = ['Rick Blommers']
10
- spec.email = ['rick@blommersit.nl']
11
- spec.summary = 'A rails gem that automaticly remembers request parameters between requests'
12
- spec.description = spec.summary
13
- spec.homepage = "https://github.com/gamecreature/sticky_params"
14
- spec.license = 'MIT'
9
+ spec.authors = ['Rick Blommers']
10
+ spec.email = ['rick@blommersit.nl']
11
+ spec.summary = 'A rails gem that automaticly remembers request parameters between requests'
12
+ spec.description = spec.summary
13
+ spec.homepage = "https://github.com/gamecreature/sticky_params"
14
+ spec.license = 'MIT'
15
15
 
16
- spec.files = `git ls-files -z`.split("\x0")
17
- spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
- spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
- spec.require_paths = ['lib']
20
-
21
- # spec.add_development_dependency "bundler", "~> 1.7"
22
- # spec.add_development_dependency "rake", "~> 10.0"
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.required_ruby_version = ">= 2.0"
20
+ spec.require_paths = ['lib']
23
21
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sticky_params
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rick Blommers
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-04-29 00:00:00.000000000 Z
11
+ date: 2023-07-04 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A rails gem that automaticly remembers request parameters between requests
14
14
  email:
@@ -23,6 +23,7 @@ files:
23
23
  - README.md
24
24
  - Rakefile
25
25
  - lib/sticky_params.rb
26
+ - lib/sticky_params/base_params.rb
26
27
  - lib/sticky_params/railtie.rb
27
28
  - lib/sticky_params/session_params.rb
28
29
  - lib/sticky_params/strong_session_params.rb
@@ -32,7 +33,7 @@ homepage: https://github.com/gamecreature/sticky_params
32
33
  licenses:
33
34
  - MIT
34
35
  metadata: {}
35
- post_install_message:
36
+ post_install_message:
36
37
  rdoc_options: []
37
38
  require_paths:
38
39
  - lib
@@ -40,15 +41,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
40
41
  requirements:
41
42
  - - ">="
42
43
  - !ruby/object:Gem::Version
43
- version: '0'
44
+ version: '2.0'
44
45
  required_rubygems_version: !ruby/object:Gem::Requirement
45
46
  requirements:
46
47
  - - ">="
47
48
  - !ruby/object:Gem::Version
48
49
  version: '0'
49
50
  requirements: []
50
- rubygems_version: 3.0.6
51
- signing_key:
51
+ rubygems_version: 3.4.15
52
+ signing_key:
52
53
  specification_version: 4
53
54
  summary: A rails gem that automaticly remembers request parameters between requests
54
55
  test_files: []