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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 44455740df636d34ee63027b34b7733b24fe241a70313533d0c698ddfa38ba87
4
- data.tar.gz: cab9e1b6c9bf1a728f147abd475fba7f4aeef9c8988c990ab906e213b8fc822b
3
+ metadata.gz: 9038e51804afe83d423ecc4b4903df48a31fd04923a2ba0e966589ebbbd45585
4
+ data.tar.gz: fad2fdf5976803a1f4ca0bc4c913ea410a85bd0ded788886b7400b7744d8fca4
5
5
  SHA512:
6
- metadata.gz: d2cee252c15971b0acb0891c5169f595896f8e047f5792fa3bef29dbd7fcf58b2b06fa0afd470a9cb4fd06facc67ac61e2b8044d18e7aff2c387a6236b67d0ec
7
- data.tar.gz: '05283ece5a2a758f24b150b5f6bc0a38394a65902d04434a3b866bf188eeb1d72b357af2614eeaf0c2a14f6cb9bc891b05554bb39375b0ce3b870c8d64a358c7'
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, 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,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
- $ bundle
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 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.
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 developer:
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
- before_filter :sticky_params_reset
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::SessionParams.new(self)
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
@@ -1,3 +1,3 @@
1
1
  module StickyParams
2
- VERSION = "1.0.1"
2
+ VERSION = "2.0.0"
3
3
  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: 1.0.1
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