sticky_params 1.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 +7 -0
- data/.gitignore +14 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +141 -0
- data/Rakefile +2 -0
- data/lib/sticky_params.rb +29 -0
- data/lib/sticky_params/railtie.rb +11 -0
- data/lib/sticky_params/session_params.rb +57 -0
- data/lib/sticky_params/version.rb +3 -0
- data/sticky_params.gemspec +24 -0
- metadata +82 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: 13294e2e60564fda27ffa1ead2f59cccbbf6af95
|
|
4
|
+
data.tar.gz: 8775f676a8991a9a294a0edb441cd6f3bd9258e6
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 74ade0f427847a50dc1b67fab75e9fa5caa52e77be70e0c06ecce28a9d3f9f6c5387cc29d4872df33b9c2015fa2e767a3f7e1ade07180f61e6b45c2d8d904752
|
|
7
|
+
data.tar.gz: c971993f6172b86b0c202334ee4d364eb356f9d37ff82d701aba84c7aae0bf65ec9bf7b9f00003e25a292976a25caa169d822a19455a105d240ed6acc5ada273
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
Copyright (c) 2015 Rick Blommers
|
|
2
|
+
|
|
3
|
+
MIT License
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
6
|
+
a copy of this software and associated documentation files (the
|
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
11
|
+
the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be
|
|
14
|
+
included in all copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
# StickyParams
|
|
2
|
+
|
|
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.
|
|
5
|
+
|
|
6
|
+
```ruby
|
|
7
|
+
class MyController < ApplicationController
|
|
8
|
+
|
|
9
|
+
def index
|
|
10
|
+
@order = sticky_params[:order]
|
|
11
|
+
@q = sticky_params[:q]
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
end
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
## Installation
|
|
19
|
+
|
|
20
|
+
Add this line to your application's Gemfile:
|
|
21
|
+
|
|
22
|
+
```ruby
|
|
23
|
+
gem 'sticky_params'
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
And then execute:
|
|
27
|
+
|
|
28
|
+
$ bundle
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
## Usage
|
|
32
|
+
|
|
33
|
+
Just start using it, by simply replacing the normal params call with a sticky_params call:
|
|
34
|
+
|
|
35
|
+
```ruby
|
|
36
|
+
class MyController < ApplicationController
|
|
37
|
+
|
|
38
|
+
def index
|
|
39
|
+
@order = sticky_params[:order]
|
|
40
|
+
@q = sticky_params[:q]
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
end
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
You can also store stuff in the sticky params hash
|
|
47
|
+
|
|
48
|
+
```ruby
|
|
49
|
+
sticky_params["keyname"] = "value"
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
### Remove sticky stuff
|
|
54
|
+
|
|
55
|
+
Sticky parameters can be removed one by one, by simply passing a empty string.
|
|
56
|
+
|
|
57
|
+
```ruby
|
|
58
|
+
sticky_params[:order] = "" # removes the keyname order from the session
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
To remove all sticky stuff for the current controller/action you can simply call clear!
|
|
62
|
+
|
|
63
|
+
```ruby
|
|
64
|
+
sticky_params.clear!
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
To remove all sticky stuff, for every action/controller you can use clear_all!
|
|
68
|
+
But probably you don't want this ;)
|
|
69
|
+
```ruby
|
|
70
|
+
sticky_params.clear_all!
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
### Inner workings
|
|
76
|
+
|
|
77
|
+
Sticky params are stored in the session storage. They are stored in a
|
|
78
|
+
session variable named 'sticky_params'.
|
|
79
|
+
|
|
80
|
+
This session variable is a hash and it uses a prefix for storing the variables.
|
|
81
|
+
By default the prefix is "controller_action_"
|
|
82
|
+
|
|
83
|
+
When retrieving a parameter from sticky_params it first tries to retrieve it from
|
|
84
|
+
the normal params hash. When it's in the params hash, it stores the result in the
|
|
85
|
+
sticky_params hash.
|
|
86
|
+
If the parameter isn't in the normal params hash it does a lookup in the session hash.
|
|
87
|
+
Pretty simple.
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
## Common Pattern
|
|
91
|
+
|
|
92
|
+
A pattern I used often with sticky_params, is using a request parameter 'reset' .
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
For example for a basic rest controller, I have an index action which shows all users.
|
|
96
|
+
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.
|
|
98
|
+
|
|
99
|
+
But when I enter the users index screen from my application-menu, I want to reset all my
|
|
100
|
+
filters. I supply a parameter reset:
|
|
101
|
+
```ruby
|
|
102
|
+
= link_to "Users", users_path( reset: 1 )
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
Normal links, for example the list button in the user screen
|
|
106
|
+
```ruby
|
|
107
|
+
= link_to "Users", users_path
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
In the controller the index action is implemented like this
|
|
111
|
+
|
|
112
|
+
```ruby
|
|
113
|
+
class UsersController < ApplicationController
|
|
114
|
+
|
|
115
|
+
def index
|
|
116
|
+
sticky_params.reset! if params[:reset]
|
|
117
|
+
|
|
118
|
+
@order = sticky_params[:order]
|
|
119
|
+
@page = sticky_params[:page]
|
|
120
|
+
|
|
121
|
+
# ... other fancy controller stuf ...
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
end
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
For the lazy developer:
|
|
128
|
+
you could force the reset parameter to always work in your application.
|
|
129
|
+
|
|
130
|
+
```ruby
|
|
131
|
+
class ApplicationController < ActionController::Base
|
|
132
|
+
before_filter :sticky_params_reset
|
|
133
|
+
|
|
134
|
+
protected:
|
|
135
|
+
def sticky_params_reset
|
|
136
|
+
sticky_params.reset! if params[:reset]
|
|
137
|
+
end
|
|
138
|
+
end
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
have fun ;)
|
data/Rakefile
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
require "sticky_params/version"
|
|
2
|
+
require "sticky_params/railtie" if defined?(Rails)
|
|
3
|
+
require "sticky_params/session_params"
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
#
|
|
7
|
+
# Sticky Parameters is a nice hack that allows the 'remembering' a given param
|
|
8
|
+
# between page requests. When a param is supplied it is stored in a session.
|
|
9
|
+
#
|
|
10
|
+
# When the param isn't supplied it tries to recieve it from the session.
|
|
11
|
+
#
|
|
12
|
+
# When an 'empty-string' is supplied as param, the sticky_param is removed
|
|
13
|
+
#
|
|
14
|
+
# Use it the following way. Simply use sticky_params in stead of params
|
|
15
|
+
# <pre>
|
|
16
|
+
# selection = sticky_params[:name]
|
|
17
|
+
# </pre>
|
|
18
|
+
#
|
|
19
|
+
|
|
20
|
+
module StickyParams
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
# A sicky parameter is a parameter that 'keeps' it state between pages,
|
|
24
|
+
# by storing the data in a session
|
|
25
|
+
def sticky_params
|
|
26
|
+
@sticky_params ||= ::StickyParams::SessionParams.new(self)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
end
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
module StickyParams
|
|
2
|
+
|
|
3
|
+
class SessionParams
|
|
4
|
+
|
|
5
|
+
attr_reader :controller
|
|
6
|
+
attr_accessor :prefix
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def initialize( controller )
|
|
10
|
+
@controller = controller
|
|
11
|
+
@prefix = "#{controller.controller_name}_#{controller.action_name}_"
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def [](name)
|
|
16
|
+
session_param_name = "#{prefix}#{name}"
|
|
17
|
+
controller.session['sticky_params'] ||= {}
|
|
18
|
+
if controller.params[name]
|
|
19
|
+
if controller.params[name].present?
|
|
20
|
+
controller.session['sticky_params'][session_param_name] = controller.params[name]
|
|
21
|
+
else
|
|
22
|
+
controller.session['sticky_params'].delete session_param_name
|
|
23
|
+
nil
|
|
24
|
+
end
|
|
25
|
+
elsif controller.session['sticky_params'][session_param_name]
|
|
26
|
+
controller.session['sticky_params'][session_param_name]
|
|
27
|
+
else
|
|
28
|
+
nil
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def []=(name,value)
|
|
34
|
+
session_param_name = "#{prefix}#{name}"
|
|
35
|
+
controller.session['sticky_params'] ||= {}
|
|
36
|
+
controller.session['sticky_params'][session_param_name] = controller.params[name] = value
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
# clears all sticky params for the given controller/action name
|
|
41
|
+
def clear!
|
|
42
|
+
if controller.session['sticky_params'].present?
|
|
43
|
+
controller.session['sticky_params'].reject! do |key,value|
|
|
44
|
+
key.index( prefix ) == 0
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
# clears all sticky parameters
|
|
51
|
+
def clear_all!
|
|
52
|
+
controller.session.delete('sticky_params')
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# coding: utf-8
|
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
+
require 'sticky_params/version'
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |spec|
|
|
7
|
+
spec.name = "sticky_params"
|
|
8
|
+
spec.version = StickyParams::VERSION
|
|
9
|
+
|
|
10
|
+
spec.authors = ["Rick Blommers"]
|
|
11
|
+
spec.email = ["rick@blommersit.nl"]
|
|
12
|
+
spec.summary = "A rails gem that automaticly remembers request parameters between requests"
|
|
13
|
+
spec.description = spec.summary
|
|
14
|
+
spec.homepage = "https://github.com/gamecreature/sticky_params"
|
|
15
|
+
spec.license = "MIT"
|
|
16
|
+
|
|
17
|
+
spec.files = `git ls-files -z`.split("\x0")
|
|
18
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
|
19
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
|
20
|
+
spec.require_paths = ["lib"]
|
|
21
|
+
|
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
|
24
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: sticky_params
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.0.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Rick Blommers
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2015-02-11 00:00:00.000000000 Z
|
|
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'
|
|
41
|
+
description: A rails gem that automaticly remembers request parameters between requests
|
|
42
|
+
email:
|
|
43
|
+
- rick@blommersit.nl
|
|
44
|
+
executables: []
|
|
45
|
+
extensions: []
|
|
46
|
+
extra_rdoc_files: []
|
|
47
|
+
files:
|
|
48
|
+
- ".gitignore"
|
|
49
|
+
- Gemfile
|
|
50
|
+
- LICENSE.txt
|
|
51
|
+
- README.md
|
|
52
|
+
- Rakefile
|
|
53
|
+
- lib/sticky_params.rb
|
|
54
|
+
- lib/sticky_params/railtie.rb
|
|
55
|
+
- lib/sticky_params/session_params.rb
|
|
56
|
+
- lib/sticky_params/version.rb
|
|
57
|
+
- sticky_params.gemspec
|
|
58
|
+
homepage: https://github.com/gamecreature/sticky_params
|
|
59
|
+
licenses:
|
|
60
|
+
- MIT
|
|
61
|
+
metadata: {}
|
|
62
|
+
post_install_message:
|
|
63
|
+
rdoc_options: []
|
|
64
|
+
require_paths:
|
|
65
|
+
- lib
|
|
66
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
67
|
+
requirements:
|
|
68
|
+
- - ">="
|
|
69
|
+
- !ruby/object:Gem::Version
|
|
70
|
+
version: '0'
|
|
71
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
72
|
+
requirements:
|
|
73
|
+
- - ">="
|
|
74
|
+
- !ruby/object:Gem::Version
|
|
75
|
+
version: '0'
|
|
76
|
+
requirements: []
|
|
77
|
+
rubyforge_project:
|
|
78
|
+
rubygems_version: 2.4.5
|
|
79
|
+
signing_key:
|
|
80
|
+
specification_version: 4
|
|
81
|
+
summary: A rails gem that automaticly remembers request parameters between requests
|
|
82
|
+
test_files: []
|