preserve 1.0.0 → 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/{MIT-LICENSE → LICENSE} +0 -0
- data/README.md +50 -32
- data/Rakefile +1 -1
- data/lib/preserve.rb +6 -29
- data/lib/preserve/callback.rb +72 -0
- data/lib/preserve/compatibility.rb +18 -0
- data/lib/preserve/extension.rb +16 -0
- data/lib/preserve/session_key.rb +27 -0
- data/lib/preserve/version.rb +1 -1
- data/spec/dummy/common/app/controllers/admin/parameters_controller.rb +7 -0
- data/spec/dummy/common/app/controllers/application_controller.rb +7 -0
- data/spec/dummy/common/app/controllers/parameters_controller.rb +9 -0
- data/spec/dummy/common/config/routes.rb +7 -0
- data/spec/dummy/rails-3/README.rdoc +261 -0
- data/spec/dummy/rails-3/Rakefile +7 -0
- data/spec/dummy/rails-3/app/assets/javascripts/application.js +15 -0
- data/spec/dummy/rails-3/app/assets/stylesheets/application.css +13 -0
- data/spec/dummy/rails-3/app/controllers/admin/parameters_controller.rb +1 -0
- data/spec/dummy/rails-3/app/controllers/application_controller.rb +1 -0
- data/spec/dummy/rails-3/app/controllers/parameters_controller.rb +1 -0
- data/spec/dummy/{app → rails-3/app}/helpers/application_helper.rb +0 -0
- data/spec/dummy/rails-3/app/views/layouts/application.html.erb +14 -0
- data/spec/dummy/rails-3/config.ru +4 -0
- data/spec/dummy/rails-3/config/application.rb +65 -0
- data/spec/dummy/rails-3/config/boot.rb +10 -0
- data/spec/dummy/rails-3/config/database.yml +25 -0
- data/spec/dummy/rails-3/config/environment.rb +5 -0
- data/spec/dummy/rails-3/config/environments/development.rb +37 -0
- data/spec/dummy/rails-3/config/environments/production.rb +67 -0
- data/spec/dummy/rails-3/config/environments/test.rb +37 -0
- data/spec/dummy/{config → rails-3/config}/initializers/backtrace_silencers.rb +0 -0
- data/spec/dummy/rails-3/config/initializers/inflections.rb +15 -0
- data/spec/dummy/rails-3/config/initializers/mime_types.rb +5 -0
- data/spec/dummy/rails-3/config/initializers/secret_token.rb +7 -0
- data/spec/dummy/rails-3/config/initializers/session_store.rb +8 -0
- data/spec/dummy/rails-3/config/initializers/wrap_parameters.rb +14 -0
- data/spec/dummy/rails-3/config/locales/en.yml +5 -0
- data/spec/dummy/rails-3/config/routes.rb +1 -0
- data/spec/dummy/{db → rails-3/db}/test.sqlite3 +0 -0
- data/spec/dummy/rails-3/log/development.log +5 -0
- data/spec/dummy/rails-3/log/test.log +7777 -0
- data/spec/dummy/rails-3/public/404.html +26 -0
- data/spec/dummy/rails-3/public/422.html +26 -0
- data/spec/dummy/rails-3/public/500.html +25 -0
- data/spec/dummy/{public → rails-3/public}/favicon.ico +0 -0
- data/spec/dummy/rails-3/script/rails +6 -0
- data/spec/dummy/{README.rdoc → rails-4/README.rdoc} +0 -0
- data/spec/dummy/{Rakefile → rails-4/Rakefile} +0 -0
- data/spec/dummy/{app → rails-4/app}/assets/javascripts/application.js +0 -0
- data/spec/dummy/{app → rails-4/app}/assets/stylesheets/application.css +0 -0
- data/spec/dummy/rails-4/app/controllers/admin/parameters_controller.rb +1 -0
- data/spec/dummy/rails-4/app/controllers/application_controller.rb +1 -0
- data/spec/dummy/rails-4/app/controllers/parameters_controller.rb +1 -0
- data/spec/dummy/rails-4/app/helpers/application_helper.rb +2 -0
- data/spec/dummy/{app → rails-4/app}/views/layouts/application.html.erb +0 -0
- data/spec/dummy/{bin → rails-4/bin}/bundle +0 -0
- data/spec/dummy/{bin → rails-4/bin}/rails +0 -0
- data/spec/dummy/{bin → rails-4/bin}/rake +0 -0
- data/spec/dummy/{bin → rails-4/bin}/setup +0 -0
- data/spec/dummy/{config.ru → rails-4/config.ru} +0 -0
- data/spec/dummy/{config → rails-4/config}/application.rb +1 -0
- data/spec/dummy/{config → rails-4/config}/boot.rb +0 -0
- data/spec/dummy/{config → rails-4/config}/database.yml +0 -0
- data/spec/dummy/{config → rails-4/config}/environment.rb +0 -0
- data/spec/dummy/{config → rails-4/config}/environments/development.rb +0 -0
- data/spec/dummy/{config → rails-4/config}/environments/production.rb +0 -0
- data/spec/dummy/{config → rails-4/config}/environments/test.rb +0 -0
- data/spec/dummy/{config → rails-4/config}/initializers/assets.rb +0 -0
- data/spec/dummy/rails-4/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/{config → rails-4/config}/initializers/cookies_serializer.rb +0 -0
- data/spec/dummy/{config → rails-4/config}/initializers/filter_parameter_logging.rb +0 -0
- data/spec/dummy/{config → rails-4/config}/initializers/inflections.rb +0 -0
- data/spec/dummy/{config → rails-4/config}/initializers/mime_types.rb +0 -0
- data/spec/dummy/{config → rails-4/config}/initializers/session_store.rb +0 -0
- data/spec/dummy/rails-4/config/initializers/to_time_preserves_timezone.rb +10 -0
- data/spec/dummy/{config → rails-4/config}/initializers/wrap_parameters.rb +0 -0
- data/spec/dummy/{config → rails-4/config}/locales/en.yml +0 -0
- data/spec/dummy/rails-4/config/routes.rb +1 -0
- data/spec/dummy/{config → rails-4/config}/secrets.yml +2 -2
- data/spec/dummy/{public → rails-4/public}/404.html +0 -0
- data/spec/dummy/{public → rails-4/public}/422.html +0 -0
- data/spec/dummy/{public → rails-4/public}/500.html +0 -0
- data/spec/dummy/rails-4/public/favicon.ico +0 -0
- data/spec/dummy/rails-5/Rakefile +6 -0
- data/spec/dummy/rails-5/app/assets/config/manifest.js +3 -0
- data/spec/dummy/rails-5/app/assets/javascripts/application.js +15 -0
- data/spec/dummy/rails-5/app/assets/javascripts/cable.js +13 -0
- data/spec/dummy/rails-5/app/assets/stylesheets/application.css +15 -0
- data/spec/dummy/rails-5/app/channels/application_cable/channel.rb +4 -0
- data/spec/dummy/rails-5/app/channels/application_cable/connection.rb +4 -0
- data/spec/dummy/rails-5/app/controllers/admin/parameters_controller.rb +1 -0
- data/spec/dummy/rails-5/app/controllers/application_controller.rb +1 -0
- data/spec/dummy/rails-5/app/controllers/parameters_controller.rb +1 -0
- data/spec/dummy/rails-5/app/helpers/application_helper.rb +2 -0
- data/spec/dummy/rails-5/app/jobs/application_job.rb +2 -0
- data/spec/dummy/rails-5/app/mailers/application_mailer.rb +4 -0
- data/spec/dummy/rails-5/app/models/application_record.rb +3 -0
- data/spec/dummy/rails-5/app/views/layouts/application.html.erb +15 -0
- data/spec/dummy/rails-5/app/views/layouts/mailer.html.erb +13 -0
- data/spec/dummy/rails-5/app/views/layouts/mailer.text.erb +1 -0
- data/spec/dummy/rails-5/bin/bundle +3 -0
- data/spec/dummy/rails-5/bin/rails +4 -0
- data/spec/dummy/rails-5/bin/rake +4 -0
- data/spec/dummy/rails-5/bin/setup +36 -0
- data/spec/dummy/rails-5/bin/update +31 -0
- data/spec/dummy/rails-5/bin/yarn +11 -0
- data/spec/dummy/rails-5/config.ru +5 -0
- data/spec/dummy/rails-5/config/application.rb +19 -0
- data/spec/dummy/rails-5/config/boot.rb +5 -0
- data/spec/dummy/rails-5/config/cable.yml +10 -0
- data/spec/dummy/rails-5/config/database.yml +25 -0
- data/spec/dummy/rails-5/config/environment.rb +5 -0
- data/spec/dummy/rails-5/config/environments/development.rb +61 -0
- data/spec/dummy/rails-5/config/environments/production.rb +94 -0
- data/spec/dummy/rails-5/config/environments/test.rb +46 -0
- data/spec/dummy/rails-5/config/initializers/application_controller_renderer.rb +8 -0
- data/spec/dummy/rails-5/config/initializers/assets.rb +14 -0
- data/spec/dummy/rails-5/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/rails-5/config/initializers/content_security_policy.rb +25 -0
- data/spec/dummy/rails-5/config/initializers/cookies_serializer.rb +5 -0
- data/spec/dummy/rails-5/config/initializers/filter_parameter_logging.rb +4 -0
- data/spec/dummy/rails-5/config/initializers/inflections.rb +16 -0
- data/spec/dummy/rails-5/config/initializers/mime_types.rb +4 -0
- data/spec/dummy/rails-5/config/initializers/wrap_parameters.rb +14 -0
- data/spec/dummy/rails-5/config/locales/en.yml +33 -0
- data/spec/dummy/rails-5/config/puma.rb +37 -0
- data/spec/dummy/rails-5/config/routes.rb +1 -0
- data/spec/dummy/rails-5/config/spring.rb +6 -0
- data/spec/dummy/rails-5/config/storage.yml +34 -0
- data/spec/dummy/rails-5/db/test.sqlite3 +0 -0
- data/spec/dummy/rails-5/log/test.log +119 -0
- data/spec/dummy/rails-5/package.json +5 -0
- data/spec/dummy/rails-5/public/404.html +67 -0
- data/spec/dummy/rails-5/public/422.html +67 -0
- data/spec/dummy/rails-5/public/500.html +66 -0
- data/spec/dummy/rails-5/public/apple-touch-icon-precomposed.png +0 -0
- data/spec/dummy/rails-5/public/apple-touch-icon.png +0 -0
- data/spec/dummy/rails-5/public/favicon.ico +0 -0
- data/spec/dummy/rails-5/tmp/development_secret.txt +1 -0
- data/spec/dummy/rails-6/Rakefile +6 -0
- data/spec/dummy/rails-6/app/assets/config/manifest.js +2 -0
- data/spec/dummy/rails-6/app/assets/stylesheets/application.css +15 -0
- data/spec/dummy/rails-6/app/channels/application_cable/channel.rb +4 -0
- data/spec/dummy/rails-6/app/channels/application_cable/connection.rb +4 -0
- data/spec/dummy/rails-6/app/controllers/admin/parameters_controller.rb +1 -0
- data/spec/dummy/rails-6/app/controllers/application_controller.rb +1 -0
- data/spec/dummy/rails-6/app/controllers/parameters_controller.rb +1 -0
- data/spec/dummy/rails-6/app/helpers/application_helper.rb +2 -0
- data/spec/dummy/rails-6/app/javascript/packs/application.js +15 -0
- data/spec/dummy/rails-6/app/jobs/application_job.rb +7 -0
- data/spec/dummy/rails-6/app/mailers/application_mailer.rb +4 -0
- data/spec/dummy/rails-6/app/models/application_record.rb +3 -0
- data/spec/dummy/rails-6/app/views/layouts/application.html.erb +14 -0
- data/spec/dummy/rails-6/app/views/layouts/mailer.html.erb +13 -0
- data/spec/dummy/rails-6/app/views/layouts/mailer.text.erb +1 -0
- data/spec/dummy/rails-6/bin/rails +4 -0
- data/spec/dummy/rails-6/bin/rake +4 -0
- data/spec/dummy/rails-6/bin/setup +33 -0
- data/spec/dummy/rails-6/config.ru +5 -0
- data/spec/dummy/rails-6/config/application.rb +19 -0
- data/spec/dummy/rails-6/config/boot.rb +5 -0
- data/spec/dummy/rails-6/config/cable.yml +10 -0
- data/spec/dummy/rails-6/config/database.yml +25 -0
- data/spec/dummy/rails-6/config/environment.rb +5 -0
- data/spec/dummy/rails-6/config/environments/development.rb +62 -0
- data/spec/dummy/rails-6/config/environments/production.rb +112 -0
- data/spec/dummy/rails-6/config/environments/test.rb +49 -0
- data/spec/dummy/rails-6/config/initializers/application_controller_renderer.rb +8 -0
- data/spec/dummy/rails-6/config/initializers/assets.rb +12 -0
- data/spec/dummy/rails-6/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/rails-6/config/initializers/content_security_policy.rb +28 -0
- data/spec/dummy/rails-6/config/initializers/cookies_serializer.rb +5 -0
- data/spec/dummy/rails-6/config/initializers/filter_parameter_logging.rb +4 -0
- data/spec/dummy/rails-6/config/initializers/inflections.rb +16 -0
- data/spec/dummy/rails-6/config/initializers/mime_types.rb +4 -0
- data/spec/dummy/rails-6/config/initializers/wrap_parameters.rb +14 -0
- data/spec/dummy/rails-6/config/locales/en.yml +33 -0
- data/spec/dummy/rails-6/config/puma.rb +38 -0
- data/spec/dummy/rails-6/config/routes.rb +1 -0
- data/spec/dummy/rails-6/config/spring.rb +6 -0
- data/spec/dummy/rails-6/config/storage.yml +34 -0
- data/spec/dummy/rails-6/db/development.sqlite3 +0 -0
- data/spec/dummy/rails-6/db/test.sqlite3 +0 -0
- data/spec/dummy/rails-6/log/development.log +2332 -0
- data/spec/dummy/rails-6/log/test.log +16735 -0
- data/spec/dummy/rails-6/public/404.html +67 -0
- data/spec/dummy/rails-6/public/422.html +67 -0
- data/spec/dummy/rails-6/public/500.html +66 -0
- data/spec/dummy/rails-6/public/apple-touch-icon-precomposed.png +0 -0
- data/spec/dummy/rails-6/public/apple-touch-icon.png +0 -0
- data/spec/dummy/rails-6/public/favicon.ico +0 -0
- data/spec/dummy/rails-6/tmp/development_secret.txt +1 -0
- data/spec/preserve/session_key_spec.rb +31 -0
- data/spec/preserve_spec.rb +78 -20
- data/spec/spec_helper.rb +11 -5
- data/spec/support/legacy_helpers.rb +7 -0
- data/spec/support/request_helpers.rb +14 -2
- metadata +425 -83
- data/spec/dummy/app/controllers/application_controller.rb +0 -5
- data/spec/dummy/app/controllers/parameters_controller.rb +0 -19
- data/spec/dummy/config/routes.rb +0 -3
- data/spec/dummy/log/test.log +0 -6954
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: d5976d2703bee78f1a94fadc77405eb808730c805b248d6e8e57695c02857d65
|
|
4
|
+
data.tar.gz: 33c61bab02329c7f6ad662fff52bfec9f924067c923eaad0f2606af473bcee95
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 7c33d635321d025fa41a3a3736bdd5621c8fa93614b14a05a139a4cea46957e2a79d104fcd208712c769c1ef348c6aa0e64976a88e6e280c674e1e60fe43fc75
|
|
7
|
+
data.tar.gz: bf1cd4bf2bed473f53204ff8bfdb0b298201d71607e78d6907d68288c72ce459919ceadf72a15a5e2cb8f518b1a0107c92fceac2d30f157008ce367175bda815
|
data/{MIT-LICENSE → LICENSE}
RENAMED
|
File without changes
|
data/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# Preserve [](https://rubygems.org/gems/preserve) [](https://travis-ci.org/pienkowb/preserve) [](https://coveralls.io/github/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:
|
|
32
|
+
render json: parameters
|
|
33
33
|
end
|
|
34
34
|
|
|
35
35
|
private
|
|
36
36
|
|
|
37
|
-
def
|
|
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
|
|
52
|
-
The whole concept is based on the session, so in order for this to work
|
|
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 `
|
|
54
|
+
In the first request, the `status` parameter is set to `active`.
|
|
55
55
|
|
|
56
56
|
```
|
|
57
|
-
$ curl -c cookies http://localhost:3000/parameters?
|
|
57
|
+
$ curl -c cookies http://localhost:3000/parameters?status=active
|
|
58
58
|
```
|
|
59
59
|
```json
|
|
60
|
-
{"
|
|
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 `
|
|
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 :
|
|
80
|
+
preserve :status
|
|
81
81
|
|
|
82
82
|
# ...
|
|
83
83
|
end
|
|
84
84
|
```
|
|
85
85
|
|
|
86
|
-
Sending the same two requests again
|
|
86
|
+
Sending the same two requests again yields a different result.
|
|
87
87
|
|
|
88
88
|
```
|
|
89
|
-
$ curl -c cookies http://localhost:3000/parameters?
|
|
89
|
+
$ curl -c cookies http://localhost:3000/parameters?status=active
|
|
90
90
|
```
|
|
91
91
|
```json
|
|
92
|
-
{"
|
|
92
|
+
{"status":"active"}
|
|
93
93
|
```
|
|
94
94
|
|
|
95
95
|
```
|
|
96
96
|
$ curl -b cookies http://localhost:3000/parameters
|
|
97
97
|
```
|
|
98
98
|
```json
|
|
99
|
-
{"
|
|
99
|
+
{"status":"active"}
|
|
100
100
|
```
|
|
101
101
|
|
|
102
|
-
This time, the `
|
|
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 :
|
|
118
|
+
preserve :status, only: :index
|
|
118
119
|
```
|
|
119
120
|
|
|
120
|
-
|
|
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
|
-
|
|
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
|
-
###
|
|
135
|
+
### Nested parameters
|
|
136
136
|
|
|
137
|
-
|
|
138
|
-
|
|
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
|
-
|
|
153
|
+
preserve [:sort, :column]
|
|
142
154
|
```
|
|
143
155
|
|
|
144
|
-
|
|
156
|
+
### Default parameter value
|
|
145
157
|
|
|
146
|
-
|
|
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
|
-
|
|
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
|
-
|
|
151
|
-
|
|
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
|
-
|
|
155
|
-
preserve :order, prefix: 'preserved'
|
|
156
|
-
end
|
|
174
|
+
preserve :status, allow_blank: true
|
|
157
175
|
```
|
|
158
176
|
|
|
159
|
-
|
|
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
data/lib/preserve.rb
CHANGED
|
@@ -1,35 +1,12 @@
|
|
|
1
1
|
require 'active_support'
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
options = parameter_names.extract_options!
|
|
3
|
+
require 'preserve/version'
|
|
4
|
+
require 'preserve/compatibility'
|
|
6
5
|
|
|
7
|
-
|
|
8
|
-
|
|
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
|
data/lib/preserve/version.rb
CHANGED
|
@@ -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.
|