sticky_params 1.0.1 → 2.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
  SHA256:
3
- metadata.gz: 44455740df636d34ee63027b34b7733b24fe241a70313533d0c698ddfa38ba87
4
- data.tar.gz: cab9e1b6c9bf1a728f147abd475fba7f4aeef9c8988c990ab906e213b8fc822b
3
+ metadata.gz: 95f2ebe1001e88bac518483e27d299e187c09e5f5002516bee8ce2e259222a75
4
+ data.tar.gz: df3e8ea4586ddd41cc7b759051f51077910d4a6629637e0d0b9586d8cb92f26e
5
5
  SHA512:
6
- metadata.gz: d2cee252c15971b0acb0891c5169f595896f8e047f5792fa3bef29dbd7fcf58b2b06fa0afd470a9cb4fd06facc67ac61e2b8044d18e7aff2c387a6236b67d0ec
7
- data.tar.gz: '05283ece5a2a758f24b150b5f6bc0a38394a65902d04434a3b866bf188eeb1d72b357af2614eeaf0c2a14f6cb9bc891b05554bb39375b0ce3b870c8d64a358c7'
6
+ metadata.gz: cbe4b35d6555c246096d4a90fe404eb2f340336646e30d4e5dde718e3ddfaf17488387f2246775f7a1f4dba2efd5737223a97e68ce3eb18ecc5df5f63374ed69
7
+ data.tar.gz: 1e3d546c073fd3e29ca6578ab6c6776d80d905efcf68a6a45092fed579b16053dc61ee7ad899c43e79eda135d6e85f5d3774edcd86f1dd514c358253054c6608
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  # StickyParams
2
2
 
3
3
  A little gem that automaticly remembers the request parameters between requests without hassle.
4
- For example for remembering the filtering and sorting of a list, when switching to a detail screen and back.
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
7
7
  class MyController < ApplicationController
@@ -12,6 +12,25 @@ class MyController < ApplicationController
12
12
  end
13
13
  ```
14
14
 
15
+ ## Release Notes 2.1
16
+
17
+
18
+ Release 2.1, Sticky Params doesn't put ActionController::Parameters in a sesssion anymore.
19
+ ActionController::Parameters were put directly into a session in 2.0. Which worked in rails < 7.
20
+ In rails 7 this results in a `TypeError (can't dump IO)` when the session data is serialized.
21
+
22
+ Release 2.0, uses ActionController::Parameters instead of hashes.
23
+ This enable the usage of the strong parameter permit construct.
24
+ (In other words, sticky_params will work just like normal params)
25
+
26
+ To get the 1.0 behaviour, you can add the following to your ApplicationController.
27
+
28
+ ```ruby
29
+ def sticky_params
30
+ @sticky_params ||= ::StickyParams::SessionParams.new(self)
31
+ end
32
+ ```
33
+
15
34
  ## Installation
16
35
 
17
36
  Add this line to your application's Gemfile:
@@ -23,7 +42,7 @@ gem 'sticky_params'
23
42
  And then execute:
24
43
 
25
44
  ```bash
26
- $ bundle
45
+ bundle
27
46
  ```
28
47
 
29
48
  ## Usage
@@ -75,16 +94,14 @@ This session variable is a hash and it uses a prefix for storing the variables.
75
94
  By default the prefix is "controller_action_"
76
95
 
77
96
  When retrieving a parameter from sticky_params it first tries to retrieve it from
78
- the normal params hash. When it's in the params hash, it stores the result in the
79
- sticky_params hash.
80
- If the parameter isn't in the normal params hash it does a lookup in the session hash.
97
+ the normal parameters. When it's found in the parameters, it stores the result in the sticky_params storage.
98
+ If the parameter isn't in the normal aparameters it does a lookup in the session hash.
81
99
  Pretty simple.
82
100
 
83
101
  ## Common Pattern
84
102
 
85
103
  A pattern I used often with sticky_params, is using a request parameter 'reset' .
86
104
 
87
-
88
105
  For example for a basic rest controller, I have an index action which shows all users.
89
106
  The list of users is sortable, searchable and paged. When selecting a user in this table
90
107
  I goto the user screen. When going back I want my selection to persist.
@@ -116,12 +133,12 @@ class UsersController < ApplicationController
116
133
  end
117
134
  ```
118
135
 
119
- For the lazy developer:
136
+ For the lazy developers.
120
137
  you could force the reset parameter to always work in your application.
121
138
 
122
139
  ```ruby
123
140
  class ApplicationController < ActionController::Base
124
- before_filter :sticky_params_reset
141
+ before_action :sticky_params_reset
125
142
 
126
143
  protected:
127
144
  def sticky_params_reset
@@ -0,0 +1,61 @@
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
+
11
+ def fetch_from_params(name, session_param_name)
12
+ if controller.params[name].present?
13
+ if controller.params[name].is_a?(ActionController::Parameters)
14
+ controller.session['sticky_params'][session_param_name] = controller.params[name].to_unsafe_hash
15
+ else
16
+ controller.session['sticky_params'][session_param_name] = controller.params[name]
17
+ end
18
+ controller.params[name]
19
+ else
20
+ controller.session['sticky_params'].delete session_param_name
21
+ nil
22
+ end
23
+ end
24
+
25
+ def fetch_from_session(session_param_name)
26
+ result = controller.session['sticky_params'][session_param_name]
27
+ # covert hash to action parameters for simulating a normal param
28
+ result.is_a?(Hash) ? ActionController::Parameters.new(result) : result
29
+ end
30
+
31
+ def [](name)
32
+ session_param_name = "#{prefix}#{name}"
33
+ controller.session['sticky_params'] ||= {}
34
+ if controller.params[name]
35
+ fetch_from_params(name, session_param_name)
36
+ elsif controller.session['sticky_params'][session_param_name]
37
+ fetch_from_session(session_param_name)
38
+ end
39
+ end
40
+
41
+ def []=(name, value)
42
+ session_param_name = "#{prefix}#{name}"
43
+ controller.session['sticky_params'] ||= {}
44
+ controller.session['sticky_params'][session_param_name] = controller.params[name] = value
45
+ end
46
+
47
+ # clears all sticky params for the current controller/action name
48
+ def clear!
49
+ if controller.session['sticky_params'].present?
50
+ controller.session['sticky_params'].reject! do |key, _value|
51
+ key.index(prefix) == 0
52
+ end
53
+ end
54
+ end
55
+
56
+ # clears all sticky parameters
57
+ def clear_all!
58
+ controller.session.delete('sticky_params')
59
+ end
60
+ end
61
+ end
@@ -1,3 +1,3 @@
1
1
  module StickyParams
2
- VERSION = "1.0.1"
2
+ VERSION = "2.1.0"
3
3
  end
data/lib/sticky_params.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require "sticky_params/version"
2
2
  require "sticky_params/railtie" if defined?(Rails)
3
3
  require "sticky_params/session_params"
4
+ require "sticky_params/strong_session_params"
4
5
 
5
6
  # Sticky Parameters is a nice hack that allows the 'remembering' a given param
6
7
  # between page requests. When a param is supplied it is stored in a session.
@@ -18,6 +19,6 @@ module StickyParams
18
19
  # A sicky parameter is a parameter that 'keeps' it state between pages,
19
20
  # by storing the data in a session
20
21
  def sticky_params
21
- @sticky_params ||= ::StickyParams::SessionParams.new(self)
22
+ @sticky_params ||= ::StickyParams::StrongSessionParams.new(self)
22
23
  end
23
24
  end
@@ -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: 1.0.1
4
+ version: 2.1.0
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: 2022-11-09 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A rails gem that automaticly remembers request parameters between requests
14
14
  email:
@@ -25,13 +25,14 @@ files:
25
25
  - lib/sticky_params.rb
26
26
  - lib/sticky_params/railtie.rb
27
27
  - lib/sticky_params/session_params.rb
28
+ - lib/sticky_params/strong_session_params.rb
28
29
  - lib/sticky_params/version.rb
29
30
  - sticky_params.gemspec
30
31
  homepage: https://github.com/gamecreature/sticky_params
31
32
  licenses:
32
33
  - MIT
33
34
  metadata: {}
34
- post_install_message:
35
+ post_install_message:
35
36
  rdoc_options: []
36
37
  require_paths:
37
38
  - lib
@@ -39,15 +40,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
39
40
  requirements:
40
41
  - - ">="
41
42
  - !ruby/object:Gem::Version
42
- version: '0'
43
+ version: '2.0'
43
44
  required_rubygems_version: !ruby/object:Gem::Requirement
44
45
  requirements:
45
46
  - - ">="
46
47
  - !ruby/object:Gem::Version
47
48
  version: '0'
48
49
  requirements: []
49
- rubygems_version: 3.0.6
50
- signing_key:
50
+ rubygems_version: 3.3.7
51
+ signing_key:
51
52
  specification_version: 4
52
53
  summary: A rails gem that automaticly remembers request parameters between requests
53
54
  test_files: []