sticky_params 1.0.1 → 2.0.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 +4 -4
- data/README.md +20 -8
- data/lib/sticky_params.rb +2 -1
- data/lib/sticky_params/strong_session_params.rb +62 -0
- data/lib/sticky_params/version.rb +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 9038e51804afe83d423ecc4b4903df48a31fd04923a2ba0e966589ebbbd45585
|
|
4
|
+
data.tar.gz: fad2fdf5976803a1f4ca0bc4c913ea410a85bd0ded788886b7400b7744d8fca4
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 76ee65c4c4889c7e54e3310a0f16bcee2414d98ab79353be63e12685c30b2df98b7971bf8f311ed24d9b5f144b276ba563ed69c695e877e2f56762b3be4e246b
|
|
7
|
+
data.tar.gz: 273163c5f66741711f666326b3960bb120ee657f880a1868eedea7cdc847112cf9152cf5dcf0d50276f6d79b13eff41cffece3c563b24eb849d1b15ac55edbd3
|
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,
|
|
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,20 @@ class MyController < ApplicationController
|
|
|
12
12
|
end
|
|
13
13
|
```
|
|
14
14
|
|
|
15
|
+
## Release Notes 2.0
|
|
16
|
+
|
|
17
|
+
Release 2.0 uses ActionController::Parameters instead of hashes.
|
|
18
|
+
This enable the usage of the strong parameter permit construct.
|
|
19
|
+
(In other words, sticky_params will work just like normal params)
|
|
20
|
+
|
|
21
|
+
To get the 1.0 behaviour, you can add the following to your ApplicationController.
|
|
22
|
+
|
|
23
|
+
```ruby
|
|
24
|
+
def sticky_params
|
|
25
|
+
@sticky_params ||= ::StickyParams::SessionParams.new(self)
|
|
26
|
+
end
|
|
27
|
+
```
|
|
28
|
+
|
|
15
29
|
## Installation
|
|
16
30
|
|
|
17
31
|
Add this line to your application's Gemfile:
|
|
@@ -23,7 +37,7 @@ gem 'sticky_params'
|
|
|
23
37
|
And then execute:
|
|
24
38
|
|
|
25
39
|
```bash
|
|
26
|
-
|
|
40
|
+
bundle
|
|
27
41
|
```
|
|
28
42
|
|
|
29
43
|
## Usage
|
|
@@ -75,16 +89,14 @@ This session variable is a hash and it uses a prefix for storing the variables.
|
|
|
75
89
|
By default the prefix is "controller_action_"
|
|
76
90
|
|
|
77
91
|
When retrieving a parameter from sticky_params it first tries to retrieve it from
|
|
78
|
-
the normal
|
|
79
|
-
|
|
80
|
-
If the parameter isn't in the normal params hash it does a lookup in the session hash.
|
|
92
|
+
the normal parameters. When it's found in the parameters, it stores the result in the sticky_params storage.
|
|
93
|
+
If the parameter isn't in the normal aparameters it does a lookup in the session hash.
|
|
81
94
|
Pretty simple.
|
|
82
95
|
|
|
83
96
|
## Common Pattern
|
|
84
97
|
|
|
85
98
|
A pattern I used often with sticky_params, is using a request parameter 'reset' .
|
|
86
99
|
|
|
87
|
-
|
|
88
100
|
For example for a basic rest controller, I have an index action which shows all users.
|
|
89
101
|
The list of users is sortable, searchable and paged. When selecting a user in this table
|
|
90
102
|
I goto the user screen. When going back I want my selection to persist.
|
|
@@ -116,12 +128,12 @@ class UsersController < ApplicationController
|
|
|
116
128
|
end
|
|
117
129
|
```
|
|
118
130
|
|
|
119
|
-
For the lazy
|
|
131
|
+
For the lazy developers.
|
|
120
132
|
you could force the reset parameter to always work in your application.
|
|
121
133
|
|
|
122
134
|
```ruby
|
|
123
135
|
class ApplicationController < ActionController::Base
|
|
124
|
-
|
|
136
|
+
before_action :sticky_params_reset
|
|
125
137
|
|
|
126
138
|
protected:
|
|
127
139
|
def sticky_params_reset
|
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::
|
|
22
|
+
@sticky_params ||= ::StickyParams::StrongSessionParams.new(self)
|
|
22
23
|
end
|
|
23
24
|
end
|
|
@@ -0,0 +1,62 @@
|
|
|
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
|
+
controller.session['sticky_params'][session_param_name] = controller.params[name]
|
|
14
|
+
else
|
|
15
|
+
controller.session['sticky_params'].delete session_param_name
|
|
16
|
+
nil
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def fetch_from_session(session_param_name)
|
|
21
|
+
result = controller.session['sticky_params'][session_param_name]
|
|
22
|
+
|
|
23
|
+
# 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
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def [](name)
|
|
33
|
+
session_param_name = "#{prefix}#{name}"
|
|
34
|
+
controller.session['sticky_params'] ||= ActionController::Parameters.new
|
|
35
|
+
if controller.params[name]
|
|
36
|
+
fetch_from_params(name, session_param_name)
|
|
37
|
+
elsif controller.session['sticky_params'][session_param_name]
|
|
38
|
+
fetch_from_session(session_param_name)
|
|
39
|
+
end
|
|
40
|
+
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
|
+
end
|
|
62
|
+
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: sticky_params
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version:
|
|
4
|
+
version: 2.0.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Rick Blommers
|
|
@@ -25,6 +25,7 @@ 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
|