preserve 1.0.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (203) hide show
  1. checksums.yaml +4 -4
  2. data/{MIT-LICENSE → LICENSE} +0 -0
  3. data/README.md +50 -32
  4. data/Rakefile +1 -1
  5. data/lib/preserve.rb +6 -29
  6. data/lib/preserve/callback.rb +72 -0
  7. data/lib/preserve/compatibility.rb +18 -0
  8. data/lib/preserve/extension.rb +16 -0
  9. data/lib/preserve/session_key.rb +27 -0
  10. data/lib/preserve/version.rb +1 -1
  11. data/spec/dummy/common/app/controllers/admin/parameters_controller.rb +7 -0
  12. data/spec/dummy/common/app/controllers/application_controller.rb +7 -0
  13. data/spec/dummy/common/app/controllers/parameters_controller.rb +9 -0
  14. data/spec/dummy/common/config/routes.rb +7 -0
  15. data/spec/dummy/rails-3/README.rdoc +261 -0
  16. data/spec/dummy/rails-3/Rakefile +7 -0
  17. data/spec/dummy/rails-3/app/assets/javascripts/application.js +15 -0
  18. data/spec/dummy/rails-3/app/assets/stylesheets/application.css +13 -0
  19. data/spec/dummy/rails-3/app/controllers/admin/parameters_controller.rb +1 -0
  20. data/spec/dummy/rails-3/app/controllers/application_controller.rb +1 -0
  21. data/spec/dummy/rails-3/app/controllers/parameters_controller.rb +1 -0
  22. data/spec/dummy/{app → rails-3/app}/helpers/application_helper.rb +0 -0
  23. data/spec/dummy/rails-3/app/views/layouts/application.html.erb +14 -0
  24. data/spec/dummy/rails-3/config.ru +4 -0
  25. data/spec/dummy/rails-3/config/application.rb +65 -0
  26. data/spec/dummy/rails-3/config/boot.rb +10 -0
  27. data/spec/dummy/rails-3/config/database.yml +25 -0
  28. data/spec/dummy/rails-3/config/environment.rb +5 -0
  29. data/spec/dummy/rails-3/config/environments/development.rb +37 -0
  30. data/spec/dummy/rails-3/config/environments/production.rb +67 -0
  31. data/spec/dummy/rails-3/config/environments/test.rb +37 -0
  32. data/spec/dummy/{config → rails-3/config}/initializers/backtrace_silencers.rb +0 -0
  33. data/spec/dummy/rails-3/config/initializers/inflections.rb +15 -0
  34. data/spec/dummy/rails-3/config/initializers/mime_types.rb +5 -0
  35. data/spec/dummy/rails-3/config/initializers/secret_token.rb +7 -0
  36. data/spec/dummy/rails-3/config/initializers/session_store.rb +8 -0
  37. data/spec/dummy/rails-3/config/initializers/wrap_parameters.rb +14 -0
  38. data/spec/dummy/rails-3/config/locales/en.yml +5 -0
  39. data/spec/dummy/rails-3/config/routes.rb +1 -0
  40. data/spec/dummy/{db → rails-3/db}/test.sqlite3 +0 -0
  41. data/spec/dummy/rails-3/log/development.log +5 -0
  42. data/spec/dummy/rails-3/log/test.log +7777 -0
  43. data/spec/dummy/rails-3/public/404.html +26 -0
  44. data/spec/dummy/rails-3/public/422.html +26 -0
  45. data/spec/dummy/rails-3/public/500.html +25 -0
  46. data/spec/dummy/{public → rails-3/public}/favicon.ico +0 -0
  47. data/spec/dummy/rails-3/script/rails +6 -0
  48. data/spec/dummy/{README.rdoc → rails-4/README.rdoc} +0 -0
  49. data/spec/dummy/{Rakefile → rails-4/Rakefile} +0 -0
  50. data/spec/dummy/{app → rails-4/app}/assets/javascripts/application.js +0 -0
  51. data/spec/dummy/{app → rails-4/app}/assets/stylesheets/application.css +0 -0
  52. data/spec/dummy/rails-4/app/controllers/admin/parameters_controller.rb +1 -0
  53. data/spec/dummy/rails-4/app/controllers/application_controller.rb +1 -0
  54. data/spec/dummy/rails-4/app/controllers/parameters_controller.rb +1 -0
  55. data/spec/dummy/rails-4/app/helpers/application_helper.rb +2 -0
  56. data/spec/dummy/{app → rails-4/app}/views/layouts/application.html.erb +0 -0
  57. data/spec/dummy/{bin → rails-4/bin}/bundle +0 -0
  58. data/spec/dummy/{bin → rails-4/bin}/rails +0 -0
  59. data/spec/dummy/{bin → rails-4/bin}/rake +0 -0
  60. data/spec/dummy/{bin → rails-4/bin}/setup +0 -0
  61. data/spec/dummy/{config.ru → rails-4/config.ru} +0 -0
  62. data/spec/dummy/{config → rails-4/config}/application.rb +1 -0
  63. data/spec/dummy/{config → rails-4/config}/boot.rb +0 -0
  64. data/spec/dummy/{config → rails-4/config}/database.yml +0 -0
  65. data/spec/dummy/{config → rails-4/config}/environment.rb +0 -0
  66. data/spec/dummy/{config → rails-4/config}/environments/development.rb +0 -0
  67. data/spec/dummy/{config → rails-4/config}/environments/production.rb +0 -0
  68. data/spec/dummy/{config → rails-4/config}/environments/test.rb +0 -0
  69. data/spec/dummy/{config → rails-4/config}/initializers/assets.rb +0 -0
  70. data/spec/dummy/rails-4/config/initializers/backtrace_silencers.rb +7 -0
  71. data/spec/dummy/{config → rails-4/config}/initializers/cookies_serializer.rb +0 -0
  72. data/spec/dummy/{config → rails-4/config}/initializers/filter_parameter_logging.rb +0 -0
  73. data/spec/dummy/{config → rails-4/config}/initializers/inflections.rb +0 -0
  74. data/spec/dummy/{config → rails-4/config}/initializers/mime_types.rb +0 -0
  75. data/spec/dummy/{config → rails-4/config}/initializers/session_store.rb +0 -0
  76. data/spec/dummy/rails-4/config/initializers/to_time_preserves_timezone.rb +10 -0
  77. data/spec/dummy/{config → rails-4/config}/initializers/wrap_parameters.rb +0 -0
  78. data/spec/dummy/{config → rails-4/config}/locales/en.yml +0 -0
  79. data/spec/dummy/rails-4/config/routes.rb +1 -0
  80. data/spec/dummy/{config → rails-4/config}/secrets.yml +2 -2
  81. data/spec/dummy/{public → rails-4/public}/404.html +0 -0
  82. data/spec/dummy/{public → rails-4/public}/422.html +0 -0
  83. data/spec/dummy/{public → rails-4/public}/500.html +0 -0
  84. data/spec/dummy/rails-4/public/favicon.ico +0 -0
  85. data/spec/dummy/rails-5/Rakefile +6 -0
  86. data/spec/dummy/rails-5/app/assets/config/manifest.js +3 -0
  87. data/spec/dummy/rails-5/app/assets/javascripts/application.js +15 -0
  88. data/spec/dummy/rails-5/app/assets/javascripts/cable.js +13 -0
  89. data/spec/dummy/rails-5/app/assets/stylesheets/application.css +15 -0
  90. data/spec/dummy/rails-5/app/channels/application_cable/channel.rb +4 -0
  91. data/spec/dummy/rails-5/app/channels/application_cable/connection.rb +4 -0
  92. data/spec/dummy/rails-5/app/controllers/admin/parameters_controller.rb +1 -0
  93. data/spec/dummy/rails-5/app/controllers/application_controller.rb +1 -0
  94. data/spec/dummy/rails-5/app/controllers/parameters_controller.rb +1 -0
  95. data/spec/dummy/rails-5/app/helpers/application_helper.rb +2 -0
  96. data/spec/dummy/rails-5/app/jobs/application_job.rb +2 -0
  97. data/spec/dummy/rails-5/app/mailers/application_mailer.rb +4 -0
  98. data/spec/dummy/rails-5/app/models/application_record.rb +3 -0
  99. data/spec/dummy/rails-5/app/views/layouts/application.html.erb +15 -0
  100. data/spec/dummy/rails-5/app/views/layouts/mailer.html.erb +13 -0
  101. data/spec/dummy/rails-5/app/views/layouts/mailer.text.erb +1 -0
  102. data/spec/dummy/rails-5/bin/bundle +3 -0
  103. data/spec/dummy/rails-5/bin/rails +4 -0
  104. data/spec/dummy/rails-5/bin/rake +4 -0
  105. data/spec/dummy/rails-5/bin/setup +36 -0
  106. data/spec/dummy/rails-5/bin/update +31 -0
  107. data/spec/dummy/rails-5/bin/yarn +11 -0
  108. data/spec/dummy/rails-5/config.ru +5 -0
  109. data/spec/dummy/rails-5/config/application.rb +19 -0
  110. data/spec/dummy/rails-5/config/boot.rb +5 -0
  111. data/spec/dummy/rails-5/config/cable.yml +10 -0
  112. data/spec/dummy/rails-5/config/database.yml +25 -0
  113. data/spec/dummy/rails-5/config/environment.rb +5 -0
  114. data/spec/dummy/rails-5/config/environments/development.rb +61 -0
  115. data/spec/dummy/rails-5/config/environments/production.rb +94 -0
  116. data/spec/dummy/rails-5/config/environments/test.rb +46 -0
  117. data/spec/dummy/rails-5/config/initializers/application_controller_renderer.rb +8 -0
  118. data/spec/dummy/rails-5/config/initializers/assets.rb +14 -0
  119. data/spec/dummy/rails-5/config/initializers/backtrace_silencers.rb +7 -0
  120. data/spec/dummy/rails-5/config/initializers/content_security_policy.rb +25 -0
  121. data/spec/dummy/rails-5/config/initializers/cookies_serializer.rb +5 -0
  122. data/spec/dummy/rails-5/config/initializers/filter_parameter_logging.rb +4 -0
  123. data/spec/dummy/rails-5/config/initializers/inflections.rb +16 -0
  124. data/spec/dummy/rails-5/config/initializers/mime_types.rb +4 -0
  125. data/spec/dummy/rails-5/config/initializers/wrap_parameters.rb +14 -0
  126. data/spec/dummy/rails-5/config/locales/en.yml +33 -0
  127. data/spec/dummy/rails-5/config/puma.rb +37 -0
  128. data/spec/dummy/rails-5/config/routes.rb +1 -0
  129. data/spec/dummy/rails-5/config/spring.rb +6 -0
  130. data/spec/dummy/rails-5/config/storage.yml +34 -0
  131. data/spec/dummy/rails-5/db/test.sqlite3 +0 -0
  132. data/spec/dummy/rails-5/log/test.log +119 -0
  133. data/spec/dummy/rails-5/package.json +5 -0
  134. data/spec/dummy/rails-5/public/404.html +67 -0
  135. data/spec/dummy/rails-5/public/422.html +67 -0
  136. data/spec/dummy/rails-5/public/500.html +66 -0
  137. data/spec/dummy/rails-5/public/apple-touch-icon-precomposed.png +0 -0
  138. data/spec/dummy/rails-5/public/apple-touch-icon.png +0 -0
  139. data/spec/dummy/rails-5/public/favicon.ico +0 -0
  140. data/spec/dummy/rails-5/tmp/development_secret.txt +1 -0
  141. data/spec/dummy/rails-6/Rakefile +6 -0
  142. data/spec/dummy/rails-6/app/assets/config/manifest.js +2 -0
  143. data/spec/dummy/rails-6/app/assets/stylesheets/application.css +15 -0
  144. data/spec/dummy/rails-6/app/channels/application_cable/channel.rb +4 -0
  145. data/spec/dummy/rails-6/app/channels/application_cable/connection.rb +4 -0
  146. data/spec/dummy/rails-6/app/controllers/admin/parameters_controller.rb +1 -0
  147. data/spec/dummy/rails-6/app/controllers/application_controller.rb +1 -0
  148. data/spec/dummy/rails-6/app/controllers/parameters_controller.rb +1 -0
  149. data/spec/dummy/rails-6/app/helpers/application_helper.rb +2 -0
  150. data/spec/dummy/rails-6/app/javascript/packs/application.js +15 -0
  151. data/spec/dummy/rails-6/app/jobs/application_job.rb +7 -0
  152. data/spec/dummy/rails-6/app/mailers/application_mailer.rb +4 -0
  153. data/spec/dummy/rails-6/app/models/application_record.rb +3 -0
  154. data/spec/dummy/rails-6/app/views/layouts/application.html.erb +14 -0
  155. data/spec/dummy/rails-6/app/views/layouts/mailer.html.erb +13 -0
  156. data/spec/dummy/rails-6/app/views/layouts/mailer.text.erb +1 -0
  157. data/spec/dummy/rails-6/bin/rails +4 -0
  158. data/spec/dummy/rails-6/bin/rake +4 -0
  159. data/spec/dummy/rails-6/bin/setup +33 -0
  160. data/spec/dummy/rails-6/config.ru +5 -0
  161. data/spec/dummy/rails-6/config/application.rb +19 -0
  162. data/spec/dummy/rails-6/config/boot.rb +5 -0
  163. data/spec/dummy/rails-6/config/cable.yml +10 -0
  164. data/spec/dummy/rails-6/config/database.yml +25 -0
  165. data/spec/dummy/rails-6/config/environment.rb +5 -0
  166. data/spec/dummy/rails-6/config/environments/development.rb +62 -0
  167. data/spec/dummy/rails-6/config/environments/production.rb +112 -0
  168. data/spec/dummy/rails-6/config/environments/test.rb +49 -0
  169. data/spec/dummy/rails-6/config/initializers/application_controller_renderer.rb +8 -0
  170. data/spec/dummy/rails-6/config/initializers/assets.rb +12 -0
  171. data/spec/dummy/rails-6/config/initializers/backtrace_silencers.rb +7 -0
  172. data/spec/dummy/rails-6/config/initializers/content_security_policy.rb +28 -0
  173. data/spec/dummy/rails-6/config/initializers/cookies_serializer.rb +5 -0
  174. data/spec/dummy/rails-6/config/initializers/filter_parameter_logging.rb +4 -0
  175. data/spec/dummy/rails-6/config/initializers/inflections.rb +16 -0
  176. data/spec/dummy/rails-6/config/initializers/mime_types.rb +4 -0
  177. data/spec/dummy/rails-6/config/initializers/wrap_parameters.rb +14 -0
  178. data/spec/dummy/rails-6/config/locales/en.yml +33 -0
  179. data/spec/dummy/rails-6/config/puma.rb +38 -0
  180. data/spec/dummy/rails-6/config/routes.rb +1 -0
  181. data/spec/dummy/rails-6/config/spring.rb +6 -0
  182. data/spec/dummy/rails-6/config/storage.yml +34 -0
  183. data/spec/dummy/rails-6/db/development.sqlite3 +0 -0
  184. data/spec/dummy/rails-6/db/test.sqlite3 +0 -0
  185. data/spec/dummy/rails-6/log/development.log +2332 -0
  186. data/spec/dummy/rails-6/log/test.log +16735 -0
  187. data/spec/dummy/rails-6/public/404.html +67 -0
  188. data/spec/dummy/rails-6/public/422.html +67 -0
  189. data/spec/dummy/rails-6/public/500.html +66 -0
  190. data/spec/dummy/rails-6/public/apple-touch-icon-precomposed.png +0 -0
  191. data/spec/dummy/rails-6/public/apple-touch-icon.png +0 -0
  192. data/spec/dummy/rails-6/public/favicon.ico +0 -0
  193. data/spec/dummy/rails-6/tmp/development_secret.txt +1 -0
  194. data/spec/preserve/session_key_spec.rb +31 -0
  195. data/spec/preserve_spec.rb +78 -20
  196. data/spec/spec_helper.rb +11 -5
  197. data/spec/support/legacy_helpers.rb +7 -0
  198. data/spec/support/request_helpers.rb +14 -2
  199. metadata +425 -83
  200. data/spec/dummy/app/controllers/application_controller.rb +0 -5
  201. data/spec/dummy/app/controllers/parameters_controller.rb +0 -19
  202. data/spec/dummy/config/routes.rb +0 -3
  203. data/spec/dummy/log/test.log +0 -6954
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 76397ea6ad52a116d49b2d28f4e011e294e14c2f9fc6afa7e4e6ec7e397c1145
4
- data.tar.gz: 3424237b322a89b4d4ccad66e226b48fe6b123be88926ce062d763e04650801b
3
+ metadata.gz: d5976d2703bee78f1a94fadc77405eb808730c805b248d6e8e57695c02857d65
4
+ data.tar.gz: 33c61bab02329c7f6ad662fff52bfec9f924067c923eaad0f2606af473bcee95
5
5
  SHA512:
6
- metadata.gz: e724d2f28ff988ee39804288f889914995ce96c0597d129f168e8d1fc6572d69cfa49745a366130f0a3560bc7b2be16d91df75029e02e197cc816b6ab4843b47
7
- data.tar.gz: 2f1431d6660743505520a425aaa29b7e154d75c3f1bcd6bc6f655b9bb6a6c29d048d809225326223fc4dff120aad0e99a7794deb24a5fe4706cafa1958e30516
6
+ metadata.gz: 7c33d635321d025fa41a3a3736bdd5621c8fa93614b14a05a139a4cea46957e2a79d104fcd208712c769c1ef348c6aa0e64976a88e6e280c674e1e60fe43fc75
7
+ data.tar.gz: bf1cd4bf2bed473f53204ff8bfdb0b298201d71607e78d6907d68288c72ce459919ceadf72a15a5e2cb8f518b1a0107c92fceac2d30f157008ce367175bda815
File without changes
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Preserve [![Gem version](https://img.shields.io/gem/v/preserve.svg)](https://rubygems.org/gems/preserve) [![Build status](https://img.shields.io/travis/pienkowb/preserve.svg)](https://travis-ci.org/pienkowb/preserve)
1
+ # Preserve [![Gem version](https://img.shields.io/gem/v/preserve)](https://rubygems.org/gems/preserve) [![Build status](https://img.shields.io/travis/pienkowb/preserve/develop)](https://travis-ci.org/pienkowb/preserve) [![Coverage status](https://img.shields.io/coveralls/github/pienkowb/preserve/develop)](https://coveralls.io/github/pienkowb/preserve) [![Maintainability status](https://img.shields.io/codeclimate/maintainability/pienkowb/preserve)](https://codeclimate.com/github/pienkowb/preserve)
2
2
 
3
3
  Preserve is a Rails plugin which stores selected parameters in the session to make them available in subsequent requests.
4
4
 
@@ -29,12 +29,12 @@ For the sole purpose of this example, let's assume we have a Rails application w
29
29
  ```ruby
30
30
  class ParametersController < ApplicationController
31
31
  def index
32
- render json: request_parameters
32
+ render json: parameters
33
33
  end
34
34
 
35
35
  private
36
36
 
37
- def request_parameters
37
+ def parameters
38
38
  params.except(:controller, :action)
39
39
  end
40
40
  end
@@ -48,16 +48,16 @@ Rails.application.routes.draw do
48
48
  end
49
49
  ```
50
50
 
51
- Let's start the application and test its behaviour using [cURL](https://curl.haxx.se/).
52
- The whole concept is based on the session, so in order for this to work, the cookie engine must be enabled (hence the `-c` and `-b` options).
51
+ Let's start the application and test its behavior using [cURL](https://curl.haxx.se/).
52
+ The whole concept is based on the session, so in order for this to work the cookie engine must be enabled (hence the `-c` and `-b` options).
53
53
 
54
- In the first request, the `per_page` parameter is set to 20.
54
+ In the first request, the `status` parameter is set to `active`.
55
55
 
56
56
  ```
57
- $ curl -c cookies http://localhost:3000/parameters?per_page=20
57
+ $ curl -c cookies http://localhost:3000/parameters?status=active
58
58
  ```
59
59
  ```json
60
- {"per_page":"20"}
60
+ {"status":"active"}
61
61
  ```
62
62
 
63
63
  As expected, the application returns the parameter and its value.
@@ -71,35 +71,35 @@ $ curl -b cookies http://localhost:3000/parameters
71
71
  {}
72
72
  ```
73
73
 
74
- Obviously, the `per_page` parameter is no longer available.
74
+ Obviously, the `status` parameter is no longer available.
75
75
 
76
76
  Now, let's call the `preserve` macro inside the `ParametersController` class with the parameter name as an argument.
77
77
 
78
78
  ```ruby
79
79
  class ParametersController < ApplicationController
80
- preserve :per_page
80
+ preserve :status
81
81
 
82
82
  # ...
83
83
  end
84
84
  ```
85
85
 
86
- Sending the same two requests again gives a different result.
86
+ Sending the same two requests again yields a different result.
87
87
 
88
88
  ```
89
- $ curl -c cookies http://localhost:3000/parameters?per_page=20
89
+ $ curl -c cookies http://localhost:3000/parameters?status=active
90
90
  ```
91
91
  ```json
92
- {"per_page":"20"}
92
+ {"status":"active"}
93
93
  ```
94
94
 
95
95
  ```
96
96
  $ curl -b cookies http://localhost:3000/parameters
97
97
  ```
98
98
  ```json
99
- {"per_page":"20"}
99
+ {"status":"active"}
100
100
  ```
101
101
 
102
- This time, the `per_page` parameter is still available when the second request is made, even though it wasn't sent particularly in that request.
102
+ This time, the `status` parameter is still available when the second request is made, even though it wasn't sent particularly in that request.
103
103
 
104
104
  ### Multiple arguments
105
105
 
@@ -112,17 +112,17 @@ preserve :page, :per_page
112
112
  ### Action restrictions
113
113
 
114
114
  Limiting functionality provided by the gem to a certain set of controller actions can be achieved by applying the `only` (or `except`) option.
115
+ For example:
115
116
 
116
117
  ```ruby
117
- preserve :per_page, only: :index
118
+ preserve :status, only: :index
118
119
  ```
119
120
 
120
- It behaves exactly like the `only` option of an [Action Controller filter](https://guides.rubyonrails.org/action_controller_overview.html#filters).
121
- In fact, the gem sets such filter underneath, so you can make use of all its options – they will be passed to that filter.
121
+ The behavior is exactly the same as with an [Action Controller filter](https://guides.rubyonrails.org/action_controller_overview.html#filters).
122
122
 
123
123
  ### Application-wide parameters
124
124
 
125
- When there's a need to store a parameter used across the whole application, the `preserve` macro should be called inside the `ApplicationController`.
125
+ Parameters used across the whole application can be persisted by adding the `preserve` macro to the `ApplicationController`.
126
126
 
127
127
  ```ruby
128
128
  class ApplicationController < ActionController::Base
@@ -132,28 +132,46 @@ end
132
132
 
133
133
  In more complex scenarios, controller inheritance can be utilized to further adjust the scope.
134
134
 
135
- ### Blank parameter values
135
+ ### Nested parameters
136
136
 
137
- Normally, when a parameter value is an empty string, it is overwritten by a value stored in the session.
138
- To change this behavior, you can use the `allow_blank` option.
137
+ Storing a value of a nested parameter requires calling the `preserve` macro with an array of consecutive keys leading to that parameter in the parameters hash (similar to the [`Hash#dig`](https://apidock.com/ruby/Hash/dig) method).
138
+
139
+ For example, in case of the JSON payload below:
140
+
141
+ ```json
142
+ {
143
+ "sort": {
144
+ "column": "name",
145
+ "direction": "desc"
146
+ }
147
+ }
148
+ ```
149
+
150
+ The `column` parameter can be persisted with the following line:
139
151
 
140
152
  ```ruby
141
- preserve :status, allow_blank: true
153
+ preserve [:sort, :column]
142
154
  ```
143
155
 
144
- As a result, the parameter value would be restored from the session only when the parameter was not sent in a request.
156
+ ### Default parameter value
145
157
 
146
- ### Setting a session key prefix
158
+ There might be a situation where neither the parameters hash nor the session contains a parameter value.
159
+ To provide a fallback value for such a scenario, you can use the `default` option.
160
+ For instance:
147
161
 
148
- By default, parameter values are stored in the session with a key that consists of a controller name and parameter name (e.g. `users_order` for the `order` parameter in the `UsersController`).
162
+ ```ruby
163
+ preserve :status, default: 'active'
164
+ ```
165
+
166
+ Note that default values are not stored in the session, but assigned on every request instead.
167
+
168
+ ### Blank parameter values
149
169
 
150
- In most cases such combination results in a unique session key, but there might be a situation when it's necessary to add a prefix in order to avoid conflicts with a session key that is already in use.
151
- It can be done by passing the `prefix` option.
170
+ Normally, when a parameter value is an empty string it is overwritten by a value stored in the session.
171
+ To change this behavior, you can use the `allow_blank` option.
152
172
 
153
173
  ```ruby
154
- class UsersController < ApplicationController
155
- preserve :order, prefix: 'preserved'
156
- end
174
+ preserve :status, allow_blank: true
157
175
  ```
158
176
 
159
- From now on, the parameter will be stored in the session with the `preserved_users_order` key.
177
+ As a result, the parameter value would be restored from the session only when the parameter was not sent in a request.
data/Rakefile CHANGED
@@ -16,7 +16,7 @@ end
16
16
 
17
17
  RDoc::Task.new(:rdoc) do |rdoc|
18
18
  rdoc.rdoc_dir = 'rdoc'
19
- rdoc.title = 'Preserve'
19
+ rdoc.title = 'Preserve'
20
20
  rdoc.options << '--line-numbers'
21
21
  rdoc.rdoc_files.include('README.rdoc')
22
22
  rdoc.rdoc_files.include('lib/**/*.rb')
@@ -1,35 +1,12 @@
1
1
  require 'active_support'
2
2
 
3
- module Preserve
4
- def preserve(*parameter_names)
5
- options = parameter_names.extract_options!
3
+ require 'preserve/version'
4
+ require 'preserve/compatibility'
6
5
 
7
- allow_blank = options.delete(:allow_blank)
8
- prefix = options.delete(:prefix)
9
-
10
- parameter_names.each do |name|
11
- key = [prefix, controller_name, name].compact.join('_')
12
- predicate = allow_blank ? :nil? : :blank?
13
-
14
- _set_preserve_callback(options) do
15
- if params[name].send(predicate)
16
- params[name] = session[key] if session.key?(key)
17
- else
18
- session[key] = params[name]
19
- end
20
- end
21
- end
22
- end
23
-
24
- def _set_preserve_callback(options, &block)
25
- if respond_to?(:before_action)
26
- before_action(options, &block)
27
- else
28
- before_filter(options, &block)
29
- end
30
- end
31
- end
6
+ require 'preserve/session_key'
7
+ require 'preserve/callback'
8
+ require 'preserve/extension'
32
9
 
33
10
  ActiveSupport.on_load(:action_controller) do
34
- extend Preserve
11
+ extend Preserve::Extension
35
12
  end
@@ -0,0 +1,72 @@
1
+ require 'preserve/compatibility'
2
+ require 'preserve/session_key'
3
+
4
+ module Preserve
5
+ class Callback
6
+ delegate :params, to: :controller
7
+ delegate :session, to: :controller
8
+
9
+ def initialize(source_class, parameter_key, options)
10
+ @source_class = source_class
11
+ @parameter_key = parameter_key
12
+ @options = options
13
+ end
14
+
15
+ def before(controller)
16
+ @controller = controller
17
+
18
+ if parameter_blank?
19
+ parameter_stored? ? restore_parameter : set_default_value
20
+ else
21
+ store_parameter
22
+ end
23
+ end
24
+
25
+ private
26
+
27
+ attr_reader :source_class
28
+ attr_reader :parameter_key
29
+ attr_reader :options
30
+ attr_reader :controller
31
+
32
+ def parameter_blank?
33
+ predicate = options[:allow_blank] ? :nil? : :blank?
34
+ parameter_value.__send__(predicate)
35
+ end
36
+
37
+ def parameter_stored?
38
+ session.key?(session_key)
39
+ end
40
+
41
+ def restore_parameter
42
+ self.parameter_value = session[session_key]
43
+ end
44
+
45
+ def set_default_value
46
+ self.parameter_value = options[:default] if options[:default]
47
+ end
48
+
49
+ def store_parameter
50
+ session[session_key] = parameter_value
51
+ end
52
+
53
+ def parameter_value
54
+ keys = Array(parameter_key)
55
+ keys.reduce(params) { |h, k| h[k] if h.is_a?(HASH_CLASS) }
56
+ end
57
+
58
+ def parameter_value=(value)
59
+ *keys, last_key = parameter_key
60
+
61
+ nested_hash = keys.reduce(params) do |hash, key|
62
+ hash[key] ||= HASH_CLASS.new
63
+ end
64
+
65
+ nested_hash[last_key] = value
66
+ end
67
+
68
+ def session_key
69
+ SessionKey.new(source_class, parameter_key).build
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,18 @@
1
+ require 'action_pack'
2
+ require 'active_support/hash_with_indifferent_access'
3
+
4
+ module Preserve
5
+ BEFORE_METHOD =
6
+ if ActionPack::VERSION::MAJOR >= 4
7
+ :before_action
8
+ else
9
+ :before_filter
10
+ end
11
+
12
+ HASH_CLASS =
13
+ if ActionPack::VERSION::MAJOR >= 4
14
+ ActionController::Parameters
15
+ else
16
+ ActiveSupport::HashWithIndifferentAccess
17
+ end
18
+ end
@@ -0,0 +1,16 @@
1
+ require 'preserve/callback'
2
+ require 'preserve/compatibility'
3
+
4
+ module Preserve
5
+ module Extension
6
+ def preserve(*parameter_keys)
7
+ options = parameter_keys.extract_options!
8
+ filter_options = options.slice(:only, :except)
9
+
10
+ parameter_keys.each do |parameter_key|
11
+ callback = Callback.new(self, parameter_key, options)
12
+ __send__(BEFORE_METHOD, callback, filter_options)
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,27 @@
1
+ require 'digest/sha1'
2
+
3
+ module Preserve
4
+ class SessionKey
5
+ def initialize(controller_class, parameter_key)
6
+ @controller_class = controller_class
7
+ @parameter_key = parameter_key
8
+ end
9
+
10
+ def build
11
+ [:preserve, calculate_digest].join('_')
12
+ end
13
+
14
+ private
15
+
16
+ attr_reader :controller_class
17
+ attr_reader :parameter_key
18
+
19
+ def calculate_digest
20
+ Digest::SHA1.hexdigest(input_data)
21
+ end
22
+
23
+ def input_data
24
+ [controller_class, parameter_key.class, parameter_key].join('_')
25
+ end
26
+ end
27
+ end
@@ -1,3 +1,3 @@
1
1
  module Preserve
2
- VERSION = '1.0.0'.freeze
2
+ VERSION = '2.0.0'.freeze
3
3
  end
@@ -0,0 +1,7 @@
1
+ module Admin
2
+ class ParametersController < ApplicationController
3
+ def index
4
+ render json: parameters
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ class ApplicationController < ActionController::Base
2
+ private
3
+
4
+ def parameters
5
+ params.except(:controller, :action)
6
+ end
7
+ end
@@ -0,0 +1,9 @@
1
+ class ParametersController < ApplicationController
2
+ def index
3
+ render json: parameters
4
+ end
5
+
6
+ def create
7
+ render json: parameters
8
+ end
9
+ end
@@ -0,0 +1,7 @@
1
+ Rails.application.routes.draw do
2
+ resources :parameters, only: %i[index create]
3
+
4
+ namespace :admin do
5
+ resources :parameters, only: :index
6
+ end
7
+ end
@@ -0,0 +1,261 @@
1
+ == Welcome to Rails
2
+
3
+ Rails is a web-application framework that includes everything needed to create
4
+ database-backed web applications according to the Model-View-Control pattern.
5
+
6
+ This pattern splits the view (also called the presentation) into "dumb"
7
+ templates that are primarily responsible for inserting pre-built data in between
8
+ HTML tags. The model contains the "smart" domain objects (such as Account,
9
+ Product, Person, Post) that holds all the business logic and knows how to
10
+ persist themselves to a database. The controller handles the incoming requests
11
+ (such as Save New Account, Update Product, Show Post) by manipulating the model
12
+ and directing data to the view.
13
+
14
+ In Rails, the model is handled by what's called an object-relational mapping
15
+ layer entitled Active Record. This layer allows you to present the data from
16
+ database rows as objects and embellish these data objects with business logic
17
+ methods. You can read more about Active Record in
18
+ link:files/vendor/rails/activerecord/README.html.
19
+
20
+ The controller and view are handled by the Action Pack, which handles both
21
+ layers by its two parts: Action View and Action Controller. These two layers
22
+ are bundled in a single package due to their heavy interdependence. This is
23
+ unlike the relationship between the Active Record and Action Pack that is much
24
+ more separate. Each of these packages can be used independently outside of
25
+ Rails. You can read more about Action Pack in
26
+ link:files/vendor/rails/actionpack/README.html.
27
+
28
+
29
+ == Getting Started
30
+
31
+ 1. At the command prompt, create a new Rails application:
32
+ <tt>rails new myapp</tt> (where <tt>myapp</tt> is the application name)
33
+
34
+ 2. Change directory to <tt>myapp</tt> and start the web server:
35
+ <tt>cd myapp; rails server</tt> (run with --help for options)
36
+
37
+ 3. Go to http://localhost:3000/ and you'll see:
38
+ "Welcome aboard: You're riding Ruby on Rails!"
39
+
40
+ 4. Follow the guidelines to start developing your application. You can find
41
+ the following resources handy:
42
+
43
+ * The Getting Started Guide: http://guides.rubyonrails.org/getting_started.html
44
+ * Ruby on Rails Tutorial Book: http://www.railstutorial.org/
45
+
46
+
47
+ == Debugging Rails
48
+
49
+ Sometimes your application goes wrong. Fortunately there are a lot of tools that
50
+ will help you debug it and get it back on the rails.
51
+
52
+ First area to check is the application log files. Have "tail -f" commands
53
+ running on the server.log and development.log. Rails will automatically display
54
+ debugging and runtime information to these files. Debugging info will also be
55
+ shown in the browser on requests from 127.0.0.1.
56
+
57
+ You can also log your own messages directly into the log file from your code
58
+ using the Ruby logger class from inside your controllers. Example:
59
+
60
+ class WeblogController < ActionController::Base
61
+ def destroy
62
+ @weblog = Weblog.find(params[:id])
63
+ @weblog.destroy
64
+ logger.info("#{Time.now} Destroyed Weblog ID ##{@weblog.id}!")
65
+ end
66
+ end
67
+
68
+ The result will be a message in your log file along the lines of:
69
+
70
+ Mon Oct 08 14:22:29 +1000 2007 Destroyed Weblog ID #1!
71
+
72
+ More information on how to use the logger is at http://www.ruby-doc.org/core/
73
+
74
+ Also, Ruby documentation can be found at http://www.ruby-lang.org/. There are
75
+ several books available online as well:
76
+
77
+ * Programming Ruby: http://www.ruby-doc.org/docs/ProgrammingRuby/ (Pickaxe)
78
+ * Learn to Program: http://pine.fm/LearnToProgram/ (a beginners guide)
79
+
80
+ These two books will bring you up to speed on the Ruby language and also on
81
+ programming in general.
82
+
83
+
84
+ == Debugger
85
+
86
+ Debugger support is available through the debugger command when you start your
87
+ Mongrel or WEBrick server with --debugger. This means that you can break out of
88
+ execution at any point in the code, investigate and change the model, and then,
89
+ resume execution! You need to install ruby-debug to run the server in debugging
90
+ mode. With gems, use <tt>sudo gem install ruby-debug</tt>. Example:
91
+
92
+ class WeblogController < ActionController::Base
93
+ def index
94
+ @posts = Post.all
95
+ debugger
96
+ end
97
+ end
98
+
99
+ So the controller will accept the action, run the first line, then present you
100
+ with a IRB prompt in the server window. Here you can do things like:
101
+
102
+ >> @posts.inspect
103
+ => "[#<Post:0x14a6be8
104
+ @attributes={"title"=>nil, "body"=>nil, "id"=>"1"}>,
105
+ #<Post:0x14a6620
106
+ @attributes={"title"=>"Rails", "body"=>"Only ten..", "id"=>"2"}>]"
107
+ >> @posts.first.title = "hello from a debugger"
108
+ => "hello from a debugger"
109
+
110
+ ...and even better, you can examine how your runtime objects actually work:
111
+
112
+ >> f = @posts.first
113
+ => #<Post:0x13630c4 @attributes={"title"=>nil, "body"=>nil, "id"=>"1"}>
114
+ >> f.
115
+ Display all 152 possibilities? (y or n)
116
+
117
+ Finally, when you're ready to resume execution, you can enter "cont".
118
+
119
+
120
+ == Console
121
+
122
+ The console is a Ruby shell, which allows you to interact with your
123
+ application's domain model. Here you'll have all parts of the application
124
+ configured, just like it is when the application is running. You can inspect
125
+ domain models, change values, and save to the database. Starting the script
126
+ without arguments will launch it in the development environment.
127
+
128
+ To start the console, run <tt>rails console</tt> from the application
129
+ directory.
130
+
131
+ Options:
132
+
133
+ * Passing the <tt>-s, --sandbox</tt> argument will rollback any modifications
134
+ made to the database.
135
+ * Passing an environment name as an argument will load the corresponding
136
+ environment. Example: <tt>rails console production</tt>.
137
+
138
+ To reload your controllers and models after launching the console run
139
+ <tt>reload!</tt>
140
+
141
+ More information about irb can be found at:
142
+ link:http://www.rubycentral.org/pickaxe/irb.html
143
+
144
+
145
+ == dbconsole
146
+
147
+ You can go to the command line of your database directly through <tt>rails
148
+ dbconsole</tt>. You would be connected to the database with the credentials
149
+ defined in database.yml. Starting the script without arguments will connect you
150
+ to the development database. Passing an argument will connect you to a different
151
+ database, like <tt>rails dbconsole production</tt>. Currently works for MySQL,
152
+ PostgreSQL and SQLite 3.
153
+
154
+ == Description of Contents
155
+
156
+ The default directory structure of a generated Ruby on Rails application:
157
+
158
+ |-- app
159
+ | |-- assets
160
+ | | |-- images
161
+ | | |-- javascripts
162
+ | | `-- stylesheets
163
+ | |-- controllers
164
+ | |-- helpers
165
+ | |-- mailers
166
+ | |-- models
167
+ | `-- views
168
+ | `-- layouts
169
+ |-- config
170
+ | |-- environments
171
+ | |-- initializers
172
+ | `-- locales
173
+ |-- db
174
+ |-- doc
175
+ |-- lib
176
+ | |-- assets
177
+ | `-- tasks
178
+ |-- log
179
+ |-- public
180
+ |-- script
181
+ |-- test
182
+ | |-- fixtures
183
+ | |-- functional
184
+ | |-- integration
185
+ | |-- performance
186
+ | `-- unit
187
+ |-- tmp
188
+ | `-- cache
189
+ | `-- assets
190
+ `-- vendor
191
+ |-- assets
192
+ | |-- javascripts
193
+ | `-- stylesheets
194
+ `-- plugins
195
+
196
+ app
197
+ Holds all the code that's specific to this particular application.
198
+
199
+ app/assets
200
+ Contains subdirectories for images, stylesheets, and JavaScript files.
201
+
202
+ app/controllers
203
+ Holds controllers that should be named like weblogs_controller.rb for
204
+ automated URL mapping. All controllers should descend from
205
+ ApplicationController which itself descends from ActionController::Base.
206
+
207
+ app/models
208
+ Holds models that should be named like post.rb. Models descend from
209
+ ActiveRecord::Base by default.
210
+
211
+ app/views
212
+ Holds the template files for the view that should be named like
213
+ weblogs/index.html.erb for the WeblogsController#index action. All views use
214
+ eRuby syntax by default.
215
+
216
+ app/views/layouts
217
+ Holds the template files for layouts to be used with views. This models the
218
+ common header/footer method of wrapping views. In your views, define a layout
219
+ using the <tt>layout :default</tt> and create a file named default.html.erb.
220
+ Inside default.html.erb, call <% yield %> to render the view using this
221
+ layout.
222
+
223
+ app/helpers
224
+ Holds view helpers that should be named like weblogs_helper.rb. These are
225
+ generated for you automatically when using generators for controllers.
226
+ Helpers can be used to wrap functionality for your views into methods.
227
+
228
+ config
229
+ Configuration files for the Rails environment, the routing map, the database,
230
+ and other dependencies.
231
+
232
+ db
233
+ Contains the database schema in schema.rb. db/migrate contains all the
234
+ sequence of Migrations for your schema.
235
+
236
+ doc
237
+ This directory is where your application documentation will be stored when
238
+ generated using <tt>rake doc:app</tt>
239
+
240
+ lib
241
+ Application specific libraries. Basically, any kind of custom code that
242
+ doesn't belong under controllers, models, or helpers. This directory is in
243
+ the load path.
244
+
245
+ public
246
+ The directory available for the web server. Also contains the dispatchers and the
247
+ default HTML files. This should be set as the DOCUMENT_ROOT of your web
248
+ server.
249
+
250
+ script
251
+ Helper scripts for automation and generation.
252
+
253
+ test
254
+ Unit and functional tests along with fixtures. When using the rails generate
255
+ command, template test files will be generated for you and placed in this
256
+ directory.
257
+
258
+ vendor
259
+ External libraries that the application depends on. Also includes the plugins
260
+ subdirectory. If the app has frozen rails, those gems also go here, under
261
+ vendor/rails/. This directory is in the load path.