sticky_params 1.0.0 → 1.0.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 +5 -5
- data/LICENSE.txt +1 -1
- data/README.md +23 -31
- data/lib/sticky_params/railtie.rb +0 -4
- data/lib/sticky_params/session_params.rb +7 -18
- data/lib/sticky_params/version.rb +1 -1
- data/lib/sticky_params.rb +2 -8
- data/sticky_params.gemspec +8 -9
- metadata +4 -33
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
|
-
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 44455740df636d34ee63027b34b7733b24fe241a70313533d0c698ddfa38ba87
|
|
4
|
+
data.tar.gz: cab9e1b6c9bf1a728f147abd475fba7f4aeef9c8988c990ab906e213b8fc822b
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: d2cee252c15971b0acb0891c5169f595896f8e047f5792fa3bef29dbd7fcf58b2b06fa0afd470a9cb4fd06facc67ac61e2b8044d18e7aff2c387a6236b67d0ec
|
|
7
|
+
data.tar.gz: '05283ece5a2a758f24b150b5f6bc0a38394a65902d04434a3b866bf188eeb1d72b357af2614eeaf0c2a14f6cb9bc891b05554bb39375b0ce3b870c8d64a358c7'
|
data/LICENSE.txt
CHANGED
data/README.md
CHANGED
|
@@ -1,20 +1,17 @@
|
|
|
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
|
|
8
|
-
|
|
9
8
|
def index
|
|
10
9
|
@order = sticky_params[:order]
|
|
11
10
|
@q = sticky_params[:q]
|
|
12
11
|
end
|
|
13
|
-
|
|
14
12
|
end
|
|
15
13
|
```
|
|
16
14
|
|
|
17
|
-
|
|
18
15
|
## Installation
|
|
19
16
|
|
|
20
17
|
Add this line to your application's Gemfile:
|
|
@@ -25,8 +22,9 @@ gem 'sticky_params'
|
|
|
25
22
|
|
|
26
23
|
And then execute:
|
|
27
24
|
|
|
25
|
+
```bash
|
|
28
26
|
$ bundle
|
|
29
|
-
|
|
27
|
+
```
|
|
30
28
|
|
|
31
29
|
## Usage
|
|
32
30
|
|
|
@@ -34,12 +32,10 @@ Just start using it, by simply replacing the normal params call with a sticky_pa
|
|
|
34
32
|
|
|
35
33
|
```ruby
|
|
36
34
|
class MyController < ApplicationController
|
|
37
|
-
|
|
38
35
|
def index
|
|
39
36
|
@order = sticky_params[:order]
|
|
40
37
|
@q = sticky_params[:q]
|
|
41
38
|
end
|
|
42
|
-
|
|
43
39
|
end
|
|
44
40
|
```
|
|
45
41
|
|
|
@@ -49,7 +45,6 @@ You can also store stuff in the sticky params hash
|
|
|
49
45
|
sticky_params["keyname"] = "value"
|
|
50
46
|
```
|
|
51
47
|
|
|
52
|
-
|
|
53
48
|
### Remove sticky stuff
|
|
54
49
|
|
|
55
50
|
Sticky parameters can be removed one by one, by simply passing a empty string.
|
|
@@ -64,21 +59,20 @@ To remove all sticky stuff for the current controller/action you can simply call
|
|
|
64
59
|
sticky_params.clear!
|
|
65
60
|
```
|
|
66
61
|
|
|
67
|
-
To remove all sticky stuff, for every action/controller you can use clear_all!
|
|
62
|
+
To remove all sticky stuff, for every action/controller you can use clear_all!
|
|
68
63
|
But probably you don't want this ;)
|
|
64
|
+
|
|
69
65
|
```ruby
|
|
70
|
-
sticky_params.clear_all!
|
|
66
|
+
sticky_params.clear_all!
|
|
71
67
|
```
|
|
72
68
|
|
|
73
|
-
|
|
74
|
-
|
|
75
69
|
### Inner workings
|
|
76
70
|
|
|
77
71
|
Sticky params are stored in the session storage. They are stored in a
|
|
78
72
|
session variable named 'sticky_params'.
|
|
79
73
|
|
|
80
74
|
This session variable is a hash and it uses a prefix for storing the variables.
|
|
81
|
-
By default the prefix is "controller_action_"
|
|
75
|
+
By default the prefix is "controller_action_"
|
|
82
76
|
|
|
83
77
|
When retrieving a parameter from sticky_params it first tries to retrieve it from
|
|
84
78
|
the normal params hash. When it's in the params hash, it stores the result in the
|
|
@@ -86,7 +80,6 @@ sticky_params hash.
|
|
|
86
80
|
If the parameter isn't in the normal params hash it does a lookup in the session hash.
|
|
87
81
|
Pretty simple.
|
|
88
82
|
|
|
89
|
-
|
|
90
83
|
## Common Pattern
|
|
91
84
|
|
|
92
85
|
A pattern I used often with sticky_params, is using a request parameter 'reset' .
|
|
@@ -94,33 +87,32 @@ A pattern I used often with sticky_params, is using a request parameter 'reset'
|
|
|
94
87
|
|
|
95
88
|
For example for a basic rest controller, I have an index action which shows all users.
|
|
96
89
|
The list of users is sortable, searchable and paged. When selecting a user in this table
|
|
97
|
-
I goto the user screen. When going back I want my selection to persist.
|
|
90
|
+
I goto the user screen. When going back I want my selection to persist.
|
|
98
91
|
|
|
99
92
|
But when I enter the users index screen from my application-menu, I want to reset all my
|
|
100
93
|
filters. I supply a parameter reset:
|
|
94
|
+
|
|
101
95
|
```ruby
|
|
102
|
-
|
|
96
|
+
link_to("Users", users_path(reset: 1))
|
|
103
97
|
```
|
|
104
98
|
|
|
105
99
|
Normal links, for example the list button in the user screen
|
|
100
|
+
|
|
106
101
|
```ruby
|
|
107
|
-
|
|
102
|
+
link_to("Users", users_path)
|
|
108
103
|
```
|
|
109
104
|
|
|
110
105
|
In the controller the index action is implemented like this
|
|
111
106
|
|
|
112
107
|
```ruby
|
|
113
108
|
class UsersController < ApplicationController
|
|
109
|
+
def index
|
|
110
|
+
sticky_params.clear! if params[:reset]
|
|
114
111
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
@page = sticky_params[:page]
|
|
120
|
-
|
|
121
|
-
# ... other fancy controller stuf ...
|
|
122
|
-
end
|
|
123
|
-
|
|
112
|
+
@order = sticky_params[:order]
|
|
113
|
+
@page = sticky_params[:page]
|
|
114
|
+
# ... other fancy controller stuf ...
|
|
115
|
+
end
|
|
124
116
|
end
|
|
125
117
|
```
|
|
126
118
|
|
|
@@ -129,12 +121,12 @@ you could force the reset parameter to always work in your application.
|
|
|
129
121
|
|
|
130
122
|
```ruby
|
|
131
123
|
class ApplicationController < ActionController::Base
|
|
132
|
-
|
|
133
|
-
|
|
124
|
+
before_filter :sticky_params_reset
|
|
125
|
+
|
|
134
126
|
protected:
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
127
|
+
def sticky_params_reset
|
|
128
|
+
sticky_params.clear! if params[:reset]
|
|
129
|
+
end
|
|
138
130
|
end
|
|
139
131
|
```
|
|
140
132
|
|
|
@@ -1,17 +1,13 @@
|
|
|
1
1
|
module StickyParams
|
|
2
|
-
|
|
3
2
|
class SessionParams
|
|
4
|
-
|
|
5
3
|
attr_reader :controller
|
|
6
4
|
attr_accessor :prefix
|
|
7
|
-
|
|
8
5
|
|
|
9
|
-
def initialize(
|
|
10
|
-
@controller = controller
|
|
6
|
+
def initialize(controller)
|
|
7
|
+
@controller = controller
|
|
11
8
|
@prefix = "#{controller.controller_name}_#{controller.action_name}_"
|
|
12
9
|
end
|
|
13
10
|
|
|
14
|
-
|
|
15
11
|
def [](name)
|
|
16
12
|
session_param_name = "#{prefix}#{name}"
|
|
17
13
|
controller.session['sticky_params'] ||= {}
|
|
@@ -24,34 +20,27 @@ module StickyParams
|
|
|
24
20
|
end
|
|
25
21
|
elsif controller.session['sticky_params'][session_param_name]
|
|
26
22
|
controller.session['sticky_params'][session_param_name]
|
|
27
|
-
else
|
|
28
|
-
nil
|
|
29
23
|
end
|
|
30
24
|
end
|
|
31
25
|
|
|
32
|
-
|
|
33
|
-
def []=(name,value)
|
|
26
|
+
def []=(name, value)
|
|
34
27
|
session_param_name = "#{prefix}#{name}"
|
|
35
28
|
controller.session['sticky_params'] ||= {}
|
|
36
29
|
controller.session['sticky_params'][session_param_name] = controller.params[name] = value
|
|
37
30
|
end
|
|
38
31
|
|
|
39
|
-
|
|
40
|
-
# clears all sticky params for the given controller/action name
|
|
32
|
+
# clears all sticky params for the current controller/action name
|
|
41
33
|
def clear!
|
|
42
34
|
if controller.session['sticky_params'].present?
|
|
43
|
-
controller.session['sticky_params'].reject! do |key,
|
|
44
|
-
key.index(
|
|
35
|
+
controller.session['sticky_params'].reject! do |key, _value|
|
|
36
|
+
key.index(prefix) == 0
|
|
45
37
|
end
|
|
46
38
|
end
|
|
47
39
|
end
|
|
48
40
|
|
|
49
|
-
|
|
50
41
|
# clears all sticky parameters
|
|
51
42
|
def clear_all!
|
|
52
|
-
controller.session.delete('sticky_params')
|
|
43
|
+
controller.session.delete('sticky_params')
|
|
53
44
|
end
|
|
54
|
-
|
|
55
45
|
end
|
|
56
|
-
|
|
57
46
|
end
|
data/lib/sticky_params.rb
CHANGED
|
@@ -2,8 +2,6 @@ require "sticky_params/version"
|
|
|
2
2
|
require "sticky_params/railtie" if defined?(Rails)
|
|
3
3
|
require "sticky_params/session_params"
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
#
|
|
7
5
|
# Sticky Parameters is a nice hack that allows the 'remembering' a given param
|
|
8
6
|
# between page requests. When a param is supplied it is stored in a session.
|
|
9
7
|
#
|
|
@@ -12,18 +10,14 @@ require "sticky_params/session_params"
|
|
|
12
10
|
# When an 'empty-string' is supplied as param, the sticky_param is removed
|
|
13
11
|
#
|
|
14
12
|
# Use it the following way. Simply use sticky_params in stead of params
|
|
15
|
-
# <pre>
|
|
13
|
+
# <pre>
|
|
16
14
|
# selection = sticky_params[:name]
|
|
17
15
|
# </pre>
|
|
18
|
-
#
|
|
19
16
|
|
|
20
17
|
module StickyParams
|
|
21
|
-
|
|
22
|
-
|
|
23
18
|
# A sicky parameter is a parameter that 'keeps' it state between pages,
|
|
24
19
|
# by storing the data in a session
|
|
25
20
|
def sticky_params
|
|
26
21
|
@sticky_params ||= ::StickyParams::SessionParams.new(self)
|
|
27
22
|
end
|
|
28
|
-
|
|
29
|
-
end
|
|
23
|
+
end
|
data/sticky_params.gemspec
CHANGED
|
@@ -1,24 +1,23 @@
|
|
|
1
|
-
# coding: utf-8
|
|
2
1
|
lib = File.expand_path('../lib', __FILE__)
|
|
3
2
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
3
|
require 'sticky_params/version'
|
|
5
4
|
|
|
6
5
|
Gem::Specification.new do |spec|
|
|
7
|
-
spec.name =
|
|
6
|
+
spec.name = 'sticky_params'
|
|
8
7
|
spec.version = StickyParams::VERSION
|
|
9
8
|
|
|
10
|
-
spec.authors = [
|
|
11
|
-
spec.email = [
|
|
12
|
-
spec.summary =
|
|
9
|
+
spec.authors = ['Rick Blommers']
|
|
10
|
+
spec.email = ['rick@blommersit.nl']
|
|
11
|
+
spec.summary = 'A rails gem that automaticly remembers request parameters between requests'
|
|
13
12
|
spec.description = spec.summary
|
|
14
13
|
spec.homepage = "https://github.com/gamecreature/sticky_params"
|
|
15
|
-
spec.license =
|
|
14
|
+
spec.license = 'MIT'
|
|
16
15
|
|
|
17
16
|
spec.files = `git ls-files -z`.split("\x0")
|
|
18
17
|
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
|
19
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
|
20
|
-
spec.require_paths = [
|
|
19
|
+
spec.require_paths = ['lib']
|
|
21
20
|
|
|
22
|
-
spec.add_development_dependency "bundler", "~> 1.7"
|
|
23
|
-
spec.add_development_dependency "rake", "~> 10.0"
|
|
21
|
+
# spec.add_development_dependency "bundler", "~> 1.7"
|
|
22
|
+
# spec.add_development_dependency "rake", "~> 10.0"
|
|
24
23
|
end
|
metadata
CHANGED
|
@@ -1,43 +1,15 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: sticky_params
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.0.
|
|
4
|
+
version: 1.0.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Rick Blommers
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date:
|
|
12
|
-
dependencies:
|
|
13
|
-
- !ruby/object:Gem::Dependency
|
|
14
|
-
name: bundler
|
|
15
|
-
requirement: !ruby/object:Gem::Requirement
|
|
16
|
-
requirements:
|
|
17
|
-
- - "~>"
|
|
18
|
-
- !ruby/object:Gem::Version
|
|
19
|
-
version: '1.7'
|
|
20
|
-
type: :development
|
|
21
|
-
prerelease: false
|
|
22
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
-
requirements:
|
|
24
|
-
- - "~>"
|
|
25
|
-
- !ruby/object:Gem::Version
|
|
26
|
-
version: '1.7'
|
|
27
|
-
- !ruby/object:Gem::Dependency
|
|
28
|
-
name: rake
|
|
29
|
-
requirement: !ruby/object:Gem::Requirement
|
|
30
|
-
requirements:
|
|
31
|
-
- - "~>"
|
|
32
|
-
- !ruby/object:Gem::Version
|
|
33
|
-
version: '10.0'
|
|
34
|
-
type: :development
|
|
35
|
-
prerelease: false
|
|
36
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
-
requirements:
|
|
38
|
-
- - "~>"
|
|
39
|
-
- !ruby/object:Gem::Version
|
|
40
|
-
version: '10.0'
|
|
11
|
+
date: 2020-04-29 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
41
13
|
description: A rails gem that automaticly remembers request parameters between requests
|
|
42
14
|
email:
|
|
43
15
|
- rick@blommersit.nl
|
|
@@ -74,8 +46,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
74
46
|
- !ruby/object:Gem::Version
|
|
75
47
|
version: '0'
|
|
76
48
|
requirements: []
|
|
77
|
-
|
|
78
|
-
rubygems_version: 2.4.5
|
|
49
|
+
rubygems_version: 3.0.6
|
|
79
50
|
signing_key:
|
|
80
51
|
specification_version: 4
|
|
81
52
|
summary: A rails gem that automaticly remembers request parameters between requests
|