signed_form 0.5.0 → 0.6.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: e8d65b2f35d3f09c9d3a8f19fa9f09fb5ba4861c97953b4d5fc47fd2e046f180
4
- data.tar.gz: a5e96ac88b81ad28bf1a3f047181b4e9cf6ae35f75d8e9780f8f5adec42961c0
3
+ metadata.gz: 9f57ada0901cf1f4c7cf694b33404fa6210141ea66adaa456bbb9b61d2a5c538
4
+ data.tar.gz: 16b3a5b16edd3e94a340ca4f6f5321920c34bdbb87b527233c8d699ed4121e18
5
5
  SHA512:
6
- metadata.gz: 0f1f672f5aa3f8a269129b4d37fba225d75fd7bcc47a3bfbf63a45bd19417cdc7b0978419ca008a95f61b8b0cf3e6b69b5ada2c9098aeb5f2dc34a33b839daeb
7
- data.tar.gz: e2242bf0dd88d0e8ebfd3a9bb74072b0412de296f4c12e617a335df64ed1b3f1984ad6f211cbdae038153374822165230aa73e162cdc51f1e727180cc3c61c78
6
+ metadata.gz: 4d9066d31bb044c1cc496327a341c898d8e6c38d2af414117aeb6e92ba75e5895e4feefc6f91febb4e20b218cbfa3e2d05bf6583aadcbb1018dbba12a2f86666
7
+ data.tar.gz: 75ae805951f7eebd331006f65d761481e669781478833b241a7f60d3515354e4bf5e25bf0795eb7468d0596feb6ed1dbbc66e57a9d1f17e48810703046170654
@@ -0,0 +1,56 @@
1
+ name: Ruby and Rails
2
+
3
+ on:
4
+ push:
5
+ branches: [master]
6
+ pull_request:
7
+ workflow_dispatch:
8
+
9
+ permissions:
10
+ contents: read
11
+
12
+ jobs:
13
+ test:
14
+ if: >-
15
+ !github.event.repository.fork &&
16
+ contains(fromJSON('["erichmenge","Spaceghost"]'), github.actor) &&
17
+ contains(fromJSON('["erichmenge","Spaceghost"]'), github.triggering_actor) &&
18
+ (github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository)
19
+ runs-on: [self-hosted, Linux, X64, incus, ubuntu-tiny, fedora-std]
20
+ name: Ruby ${{ matrix.ruby }} / Rails ${{ matrix.rails }}
21
+ timeout-minutes: 45
22
+ continue-on-error: ${{ matrix.ruby == 'ruby-head' || matrix.rails == 'main' }}
23
+ strategy:
24
+ fail-fast: false
25
+ max-parallel: 2
26
+ matrix:
27
+ ruby: ['3.3', '3.4', '4.0']
28
+ rails: ['~> 7.2.0', '~> 8.0.0', '~> 8.1.0']
29
+ include:
30
+ - { ruby: '3.3', rails: '~> 6.1.0' }
31
+ - { ruby: '3.3', rails: '~> 7.0.0' }
32
+ - { ruby: '3.3', rails: '~> 7.1.0' }
33
+ # Rolling stable and independent upstream development lanes.
34
+ - { ruby: jruby, rails: latest }
35
+ - { ruby: ruby, rails: latest }
36
+ - { ruby: ruby, rails: main }
37
+ - { ruby: ruby-head, rails: latest }
38
+ - { ruby: ruby-head, rails: main }
39
+ env:
40
+ RAILS_VERSION: ${{ matrix.rails }}
41
+ steps:
42
+ - uses: actions/checkout@v4
43
+ with:
44
+ persist-credentials: false
45
+ - uses: actions/setup-java@v4
46
+ if: matrix.ruby == 'jruby'
47
+ with:
48
+ distribution: temurin
49
+ java-version: '21'
50
+ - uses: ruby/setup-ruby@v1
51
+ with:
52
+ ruby-version: ${{ matrix.ruby }}
53
+ bundler-cache: true
54
+ - run: bundle exec ruby -raction_pack -e 'puts ActionPack::VERSION::STRING'
55
+ - run: bundle exec rake spec
56
+ - run: bundle exec rake build
data/.gitignore CHANGED
@@ -15,4 +15,3 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
- .byebug_history
data/Changes.md CHANGED
@@ -1,66 +1,92 @@
1
+ ## 0.6.0
2
+
3
+ Thank you to [@dxg][] for getting destination signing working on Rails 7
4
+ and for testing SignedForm across Ruby and Rails versions.
5
+
6
+ * Merged [#47][] - Fix destination signing on Rails 7, sign the new Rails 8.0
7
+ form helpers, and test compatibility through Ruby 3.4 and Rails 8.0.\
8
+ Contributed by Arek W <arek01@gmail.com>
9
+
10
+ * Support signed `form_with` forms and extract `SignedForm::SignedData` so
11
+ previously rendered forms can still be submitted after an upgrade.\
12
+ Contributed by Johnneylee Jack Rollins <git@spacegho.st>
13
+
14
+ * Extend Arek’s test matrix to Ruby 4.0 and Rails 8.1, both released after
15
+ he submitted #47. Test JRuby, Ruby head, and Rails main as well. The gem
16
+ now requires Ruby 3.3 or newer.\
17
+ Contributed by Johnneylee Jack Rollins <git@spacegho.st>
18
+
19
+ Released by Johnneylee Jack Rollins <git@spacegho.st>
20
+
21
+ [@dxg]: https://github.com/dxg
22
+ [#47]: https://github.com/erichmenge/signed_form/pull/47
23
+
1
24
  ## 0.5.0
2
- This release is owed entirely to @eric1234 & @schuetzm, thank you both for your
3
- efforts and patience.
25
+ This release is owed entirely to [@eric1234][] & [@schuetzm][], thank you both
26
+ for your efforts and patience.
4
27
 
5
- In short, this release is intended to bring the project back to an active state,
6
- any mistakes having been added in this version should be regarded as
7
- intentional. Let's move forward.
28
+ In short, this release is intended to bring the project back to an active state,
29
+ any mistakes having been added in this version should be regarded as
30
+ intentional. Let's move forward.
8
31
 
9
- Thank you as well to our gracious host @erichmenge for incepting this gem of a
10
- gem.
32
+ Thank you as well to our gracious host [@erichmenge][] for incepting this gem of
33
+ a gem.
11
34
 
12
- * Merged #25 - Fix tests
35
+ * Merged #25 - Fix tests
13
36
  Contributed by Marc Schütz <schuetzm@gmx.net>
14
37
 
15
- * Merged #29 - Use `prepend_before_action` if available
38
+ * Merged #29 - Use `prepend_before_action` if available
16
39
  Contributed by Marc Schütz <schuetzm@gmx.net>
17
40
 
18
- * Merged #31 - Release attempt
41
+ * Merged #31 - Release attempt
19
42
  Contributed by Eric Anderson <eric@pixelwareinc.com>
20
43
 
21
- * Temporarily removed fields helper from being tested in 8517af8
44
+ * Temporarily removed fields helper from being tested in 8517af8
22
45
  Contributed by Eric Anderson <eric@pixelwareinc.com>
23
46
 
24
- * Remove deprecation warnings & fixed bug revealed in 9985314
47
+ * Remove deprecation warnings & fixed bug revealed in 9985314
25
48
  Contributed by Eric Anderson <eric@pixelwareinc.com>
26
49
 
27
- * Enable signed_form to be tested against more versions of rails in b263e5b
50
+ * Enable signed_form to be tested against more versions of rails in b263e5b
28
51
  Contributed by Eric Anderson <eric@pixelwareinc.com>
29
52
 
30
53
  Released by Johnneylee Jack Rollins <Johnneylee.Rollins@gmail.com>
31
54
 
55
+ [@eric1234]: https://github.com/eric1234
56
+ [@schuetzm]: https://github.com/schuetzm
57
+ [@erichmenge]: https://github.com/erichmenge
58
+
32
59
  ## 0.4.0
33
- * Designate fields that submit multiple values correctly
60
+ * Designate fields that submit multiple values correctly
34
61
  Contributed by Marc Schütz <schuetzm@gmx.net>
35
62
 
36
- * Allow to provide blocks to form helper methods
37
- Previously, the block was swallowed rather than passed to the form helper
63
+ * Allow to provide blocks to form helper methods
64
+ Previously, the block was swallowed rather than passed to the form helper.
38
65
  Christopher Schramm <cschramm@shakaweb.org>
39
66
 
40
67
  ## 0.3.0
41
68
 
42
- * Disabled fields are no longer signed by default.
43
- To include a disabled field, explicitly sign it with
44
- `f.add_signed_fields field_name`
69
+ * Disabled fields are no longer signed by default.
70
+ To include a disabled field, explicitly sign it with
71
+ `f.add_signed_fields field_name`
45
72
  Contributed by James Moriarty <jamespaulmoriarty@gmail.com>
46
73
 
47
- * Fix multiple fields_for calls
48
- Prior to this fix, only the last of the calls would be passed.
74
+ * Fix multiple fields_for calls
75
+ Prior to this fix, only the last of the calls would be passed.
49
76
  Contributed by Marc Schütz <schuetzm@gmx.net>
50
77
 
51
- * ActiveAdmin integration
52
- CSchramm has created a plugin that integrates both activeadmin and
53
- signed_form
78
+ * ActiveAdmin integration
79
+ CSchramm has created a plugin that integrates both activeadmin and signed_form
54
80
  Contributed by Christopher Schramm <cschramm@shakaweb.org>
55
81
 
56
- * Tests pass under Rails 4.1
82
+ * Tests pass under Rails 4.1
57
83
  Contributed by Christopher Schramm <cschramm@shakaweb.org>
58
84
 
59
85
  ## 0.2.0
60
86
 
61
- * Instead of using `signed_form_for` add an option for form signing to `form_for`
62
- so that signing third party builders like SimpleForm doesn't require an
63
- adapter.
87
+ * Instead of using `signed_form_for` add an option for form signing to
88
+ `form_for` so that signing third party builders like SimpleForm doesn't
89
+ require an adapter.
64
90
 
65
91
  * Move configuration options to main module name-space.
66
92
 
@@ -70,22 +96,22 @@ Released by Johnneylee Jack Rollins <Johnneylee.Rollins@gmail.com>
70
96
 
71
97
  * Add a test helper to make testing controllers easy.
72
98
 
73
- * Only permit parameters but don't require them.
74
- Requiring them raises an exception if they're missing from the form
75
- submission. But in cases where other parameters are sent as well and the form
99
+ * Only permit parameters but don't require them.
100
+ Requiring them raises an exception if they're missing from the form
101
+ submission. But in cases where other parameters are sent as well and the form
76
102
  object may be optional this would raise an exception that would be undesired.
77
103
 
78
104
  * Allow all forms to be signed by default.
79
105
 
80
106
  ## 0.1.2
81
107
 
82
- * Fix issues where request method was not being compared properly and request
83
- url would not handle some potential cases leading to an erroneous rejection
108
+ * Fix issues where request method was not being compared properly and request
109
+ url would not handle some potential cases leading to an erroneous rejection
84
110
  of the form. [Marc Schütz, #6]
85
111
 
86
112
  ## 0.1.1
87
113
 
88
- * Add some select and date/time field helpers that were not getting added to
114
+ * Add some select and date/time field helpers that were not getting added to
89
115
  the signature [#5].
90
116
 
91
117
  ## 0.1.0
data/Gemfile CHANGED
@@ -3,14 +3,19 @@ source 'https://rubygems.org'
3
3
  # Specify your gem's dependencies in signed_form.gemspec
4
4
  gemspec
5
5
 
6
- rails_version = ENV['RAILS_VERSION'] || 'master'
6
+ rails_version = ENV['RAILS_VERSION'] || 'latest'
7
7
 
8
8
  case rails_version
9
- when /master/
10
- gem 'rails', github: 'rails/rails'
11
- gem 'arel', github: 'rails/arel'
9
+ when 'latest'
10
+ gem 'rails'
11
+ when 'main', 'master'
12
+ git 'https://github.com/rails/rails.git', branch: 'main' do
13
+ gem 'rails'
14
+ gem 'actionpack'
15
+ gem 'activemodel'
16
+ end
12
17
  when /-stable$/
13
18
  gem 'rails', github: 'rails/rails', branch: rails_version
14
19
  else
15
- gem 'rails', ENV['RAILS_VERSION']
20
+ gem 'rails', rails_version
16
21
  end
data/README.md CHANGED
@@ -1,38 +1,36 @@
1
1
  # SignedForm
2
2
 
3
- [![Gem Version](https://badge.fury.io/rb/signed_form.png)](http://badge.fury.io/rb/signed_form)
4
- [![Build Status](https://travis-ci.org/erichmenge/signed_form.png?branch=master)](https://travis-ci.org/erichmenge/signed_form)
5
- [![Code Climate](https://codeclimate.com/github/erichmenge/signed_form.png)](https://codeclimate.com/github/erichmenge/signed_form)
6
- [![Coverage Status](https://coveralls.io/repos/erichmenge/signed_form/badge.png?branch=master)](https://coveralls.io/r/erichmenge/signed_form)
7
- [![Inline docs](http://inch-ci.org/github/erichmenge/signed_form.svg?branch=master&style=flat)](https://inch-ci.org/github/erichmenge/signed_form)
3
+ [![Gem Version](https://badge.fury.io/rb/signed_form.png)](https://badge.fury.io/rb/signed_form)
4
+ [![Tests](https://github.com/erichmenge/signed_form/actions/workflows/tests.yml/badge.svg)](https://github.com/erichmenge/signed_form/actions/workflows/tests.yml)
8
5
 
9
- SignedForm brings new convenience and security to your Rails 4 or Rails 3 application.
6
+ SignedForm brings signed-field convenience and security to Rails applications.
10
7
 
11
- SignedForm is under active development. Please make sure you're reading the README associated with the version of
12
- SignedForm you're using. Click the tag link on GitHub to switch to the version you've installed to get the correct
8
+ SignedForm is under active development. Please make sure you're reading the
9
+ README associated with the version of SignedForm you're using. Click the tag
10
+ link on GitHub to switch to the version you've installed to get the correct
13
11
  README.
14
12
 
15
13
  Or be brave and bundle the gem straight from GitHub master.
16
14
 
17
- A nicely displayed version of this README complete with table of contents is available
18
- [here](http://erichmenge.com/signed_form/).
19
-
20
15
  ## How It Works
21
16
 
22
- Traditionally, when you create a form with Rails you enter your fields using something like `f.text_field :name` and so
23
- on. Once you're done making your form you need to make sure that you've either set those parameters as accessible in
24
- the model (Rails 3) or use `permit` (Rails 4). This is redundant. Why would you make a form for a user to fill out and
25
- then not accept their input? You need to always maintain this synchronization.
17
+ Traditionally, when you create a form with Rails you enter your fields using
18
+ something like `f.text_field :name` and so on. Once you're done making your
19
+ form you need to make sure that you've set those parameters as accessible in the
20
+ model (Rails 3) or use `permit` (Rails 4). This is redundant. Why would you make
21
+ a form for a user to fill out and then not accept their input? You need to
22
+ always maintain this synchronization.
26
23
 
27
- SignedForm generates a list of attributes that you have in your form and attaches them to be submitted with the form
28
- along with a HMAC-SHA1 signature of those attributes to protect them from tampering. That means no more `permit` and
24
+ SignedForm generates a list of attributes that you have in your form and
25
+ attaches them to be submitted with the form along with a HMAC-SHA1 signature of
26
+ those attributes to protect them from tampering. That means no more `permit` and
29
27
  no more `attr_accessible`. It just works.
30
28
 
31
29
  What this looks like:
32
30
 
33
31
  ```erb
34
- <%= form_for @user, signed: true do |f| %>
35
- <% f.add_signed_fields :zipcode, :state # Optionally add additional fields to sign %>
32
+ <%= form_with model: @user, signed: true do |f| %>
33
+ <% f.add_signed_fields :zipcode, :state # add additional fields to sign %>
36
34
 
37
35
  <%= f.text_field :name %>
38
36
  <%= f.text_field :address %>
@@ -41,10 +39,10 @@ What this looks like:
41
39
  ```
42
40
 
43
41
  ```ruby
44
- UsersController < ApplicationController
42
+ class UsersController < ApplicationController
45
43
  def update
46
44
  @user = User.find params[:id]
47
- @user.update_attributes params[:user]
45
+ @user.update params[:user]
48
46
  end
49
47
  end
50
48
  ```
@@ -52,7 +50,7 @@ end
52
50
  Disabled fields need to be explicitly signed:
53
51
 
54
52
  ```erb
55
- <%= form_for @user, signed: true do |f| %>
53
+ <%= form_with model: @user, signed: true do |f| %>
56
54
  <% f.add_signed_fields :name %>
57
55
 
58
56
  <%= f.text_field :name, disabled: true %>
@@ -60,10 +58,10 @@ Disabled fields need to be explicitly signed:
60
58
  <% end %>
61
59
  ```
62
60
 
63
- That's it. You're done. Need to add a field? Pop it in the form. You don't need to then update a list of attributes.
61
+ That's it. You're done. Need to add a field? Pop it in the form. You don't need
62
+ to then update a list of attributes.
64
63
 
65
- Of course, you're free to continue using the standard `form_for`. `SignedForm` is strictly opt-in. It won't change the
66
- way you use standard forms.
64
+ `SignedForm` is strictly opt-in. Rails 8's `form_with` is the primary API, and legacy `form_for` remains supported. Unsigned forms keep Rails' normal behavior.
67
65
 
68
66
  ## Is it any good?
69
67
 
@@ -73,20 +71,24 @@ Yes.
73
71
 
74
72
  SignedForm protects you in 3 ways:
75
73
 
76
- * Form fields are signed, so no alteration of the fields are allowed.
77
- * Form actions are signed. That means a form with an action of `/admin/users/3` will not work when submitted to `/users/3`.
78
- * Form views are digested (see below). So if you remove a field from your form, old forms will not be accepted despite
79
- a valid signature.
74
+ * Form fields are signed, so no alteration of the fields are allowed.
75
+ * Form actions are signed. That means a form with an action of `/admin/users/3`
76
+ will not work when submitted to `/users/3`.
77
+ * Form views are digested (see below). So if you remove a field from your form,
78
+ old forms will not be accepted despite a valid signature.
80
79
 
81
- The second two methods of security are optional and can be turned off globally or on a form by form basis.
80
+ The second two methods of security are optional and can be turned off globally
81
+ or on a form by form basis.
82
82
 
83
83
  ## Requirements
84
84
 
85
- SignedForm requires:
85
+ The gem requires Ruby 3.3 or newer and Action Pack 4.2 or newer.
86
+ The [test matrix](.github/workflows/tests.yml) lists individual Ruby and Rails combinations, including the latest stable releases, Ruby head, and Rails main.
87
+
88
+ `SignedForm::SignedData` signs and verifies the existing form token format.
89
+ Applications can upgrade while previously rendered forms are still in use.
86
90
 
87
- * Ruby 1.9 or later
88
- * Rails 4 or Rails 3.1+ ([strong_parameters](https://github.com/rails/strong_parameters) gem
89
- required for Rails 3)
91
+ [strong_parameters]: https://github.com/rails/strong_parameters
90
92
 
91
93
  ## Installation
92
94
 
@@ -100,13 +102,9 @@ And then execute:
100
102
 
101
103
  $ bundle
102
104
 
103
- If you're using Rails 3, you'll also need to install the [strong_parameters](https://github.com/rails/strong_parameters)
104
- gem. Please set it up as instructed on the linked GitHub repo.
105
-
106
- If you're using Rails 4, it works out of the box.
107
-
108
- You'll need to include `SignedForm::ActionController::PermitSignedParams` in the controller(s) you want to use
109
- SignedForm with. This can be done application wide by adding the `include` to your ApplicationController.
105
+ You'll need to include `SignedForm::ActionController::PermitSignedParams` in the
106
+ controller(s) you want to use SignedForm with. This can be done application wide
107
+ by adding the `include` to your ApplicationController.
110
108
 
111
109
  ```ruby
112
110
  ApplicationController < ActionController::Base
@@ -116,19 +114,9 @@ ApplicationController < ActionController::Base
116
114
  end
117
115
  ```
118
116
 
119
- On Rails versions older than 4.1, you'll also need to create an initializer:
120
-
121
- ```shell
122
- $ echo "SignedForm.secret_key = '$(rake secret)'" > config/initializers/signed_form.rb
123
- ```
124
-
125
- You'll probably want to keep this out of version control. Treat this key like you would your session secret, keep it
126
- private.
127
-
128
117
  ## Support for other Builders
129
118
 
130
- Any form that wraps `form_for` and the default field helpers will work with SignedForm. For example, a signed SimpleForm
131
- might look like this:
119
+ Builders that wrap Rails `form_with` or `form_for` and the default field helpers work with SignedForm. For example, a signed SimpleForm might look like this:
132
120
 
133
121
  ```erb
134
122
  <%= simple_form_for @user, signed: true do |f| %>
@@ -138,7 +126,8 @@ might look like this:
138
126
 
139
127
  This will create a signed form as expected.
140
128
 
141
- For builders that don't use the standard field helpers under the hood, you can create an adapter like this:
129
+ For builders that don't use the standard field helpers under the hood, you can
130
+ create an adapter like this:
142
131
 
143
132
  ```ruby
144
133
  class MyAdapter < SomeOtherBuilder
@@ -161,31 +150,38 @@ Then in your view:
161
150
 
162
151
  ## ActiveAdmin support
163
152
 
164
- Gem [`signed_form-activeadmin`](https://github.com/cschramm/signed_form-activeadmin) integrates SignedForm with Active Admin.
153
+ Gem
154
+ [`signed_form-activeadmin`][] integrates SignedForm with Active Admin.
165
155
 
156
+ [`signed_form-activeadmin`]: https://github.com/cschramm/signed_form-activeadmin
166
157
  ## Form Digests
167
158
 
168
- SignedForm will create a digest of all the views/partials involved with rendering your form. If the form is modifed old
169
- forms will be expired. This is done to eliminate the possibility of old forms coming back to bite you.
159
+ SignedForm will create a digest of all the views/partials involved with
160
+ rendering your form. If the form is modifed old forms will be expired. This is
161
+ done to eliminate the possibility of old forms coming back to bite you.
170
162
 
171
- By default, there is a 5 minute grace period before old forms will be rejected. This is done so that if you make a
172
- trivial change to a form you won't prevent a form a user is currently filling out from being accepted when you
173
- restart your server.
163
+ By default, there is a 5 minute grace period before old forms will be rejected.
164
+ This is done so that if you make a trivial change to a form you won't prevent a
165
+ form a user is currently filling out from being accepted when you restart your
166
+ server.
174
167
 
175
- Of course if a critical mistake is made (such as allowing an admin field to be set in the form) you could change the
176
- secret key to prevent any old form from getting through.
168
+ Of course if a critical mistake is made (such as allowing an admin field to be
169
+ set in the form) you could change the secret key to prevent any old form from
170
+ getting through.
177
171
 
178
- By default, these digests are not cached. That means that each form that is submitted will have the views be digested
179
- again. Most views and partials are relatively small so the cost of computing the MD5 hash of the files is not very
180
- expensive. However, if this is something you care about SignedForm also provides a memory store
181
- (`SignedForm::DigestStores::MemoryStore`) that will cache the digests in memory. Other stores could be used as well, as
182
- long as the object responds to `#fetch` taking the cache key as an argument as well as the block that will return the
183
- digest.
172
+ By default, these digests are not cached. That means that each form that is
173
+ submitted will have the views be digested again. Most views and partials are
174
+ relatively small so the cost of computing the MD5 hash of the files is not very
175
+ expensive. However, if this is something you care about SignedForm also provides
176
+ a memory store (`SignedForm::DigestStores::MemoryStore`) that will cache the
177
+ digests in memory. Other stores could be used as well, as long as the object
178
+ responds to `#fetch` taking the cache key as an argument as well as the block
179
+ that will return the digest.
184
180
 
185
181
  ## Example Configuration
186
182
 
187
- An example config/initializers/signed_form.rb might look something like this (these are the defaults, with the exception
188
- of the key of course):
183
+ An example config/initializers/signed_form.rb might look something like this
184
+ (these are the defaults, with the exception of the key of course):
189
185
 
190
186
  ```ruby
191
187
  SignedForm.config do |c|
@@ -199,19 +195,22 @@ SignedForm.config do |c|
199
195
  end
200
196
  ```
201
197
 
202
- Those options that are in the options hash are the default per-form options. They can be overridden by passing the same
203
- option to the `form_for` method.
198
+ Those options that are in the options hash are the default per-form options.
199
+ They can be overridden by passing the same option to the `form_for` method.
204
200
 
205
201
  ## Testing Your Controllers
206
202
 
207
- Because your tests won't include a signature you will get a `ForbiddenAttributes` exception in your tests that do mass
208
- assignment. SignedForm includes a test helper method, `permit_all_parameters` that works with both TestUnit and RSpec.
203
+ Because your tests won't include a signature you will get a
204
+ `ForbiddenAttributes` exception in your tests that do mass assignment.
205
+ SignedForm includes a test helper method, `permit_all_parameters` that works
206
+ with both TestUnit and RSpec.
209
207
 
210
- In your `spec_helper` file or `test_helper` file `require 'signed_form/test_helper'`. Then `include
211
- SignedForm::TestHelper` in tests where you need it. An example is below.
208
+ Add `require 'signed_form/test_helper'` and `include SignedForm::TestHelper`
209
+ wherever appropriate for your tests. An example is below.
212
210
 
213
- **Caution**: `permit_all_parameters` without a block modifies the singleton class of the controller under test which
214
- lasts for the duration of the test. If you want `permit_all_parameters` to be limited to a specific part of the test,
211
+ **Caution**: `permit_all_parameters` without a block modifies the singleton
212
+ class of the controller under test which lasts for the duration of the test. If
213
+ you want `permit_all_parameters` to be limited to a specific part of the test,
215
214
  pass it a block and only that block will be affected. Example:
216
215
 
217
216
  ```ruby
@@ -259,8 +258,11 @@ end
259
258
 
260
259
  ## I want to hear from you
261
260
 
262
- If you're using SignedForm, I'd love to hear from you. What do you like? What could be better? I'd love to hear your
263
- ideas. Join the mailing list on librelist to join the discussion at [signedform@librelist.com](mailto:signedform@librelist.com).
261
+ If you're using SignedForm, I'd love to hear from you. What do you like? What
262
+ could be better? I'd love to hear your ideas. Join the mailing list on librelist
263
+ to join the discussion at [signedform@librelist.com][].
264
+
265
+ [signedform@librelist.com]: mailto:signedform@librelist.com
264
266
 
265
267
  ## Contributing
266
268
 
data/Rakefile CHANGED
@@ -4,3 +4,7 @@ desc 'Generate documentation.'
4
4
  task :rdoc do
5
5
  system 'yardoc'
6
6
  end
7
+
8
+ require "rspec/core/rake_task"
9
+ RSpec::Core::RakeTask.new(:spec)
10
+ task default: :spec
@@ -1,31 +1,58 @@
1
1
  module SignedForm
2
2
  module ActionView
3
3
  module FormHelper
4
-
5
- # @option options :sign_destination [Boolean] Only the URL given/created will be allowed to receive the form.
6
- # @option options :digest [Boolean] Digest and verify the views have not been modified
7
- # @option options :digest_grace_period [Integer] Time in seconds to allow old forms
8
- # @option options :wrap_form [Symbol] Method of a form builder to wrap. Default is form_for
9
4
  def form_for(record, options = {}, &block)
10
- if options[:signed].nil? && !SignedForm.options[:signed] || !options[:signed].nil? && !options[:signed]
11
- return super
5
+ # Starting with Rails 7, form_for delegates to form_with.
6
+ return super if ::ActionView::VERSION::MAJOR >= 7 || !signed_form_enabled?(options)
7
+
8
+ super(record, signed_builder_options(options)) do |form|
9
+ signed_form_body(form, &block)
12
10
  end
11
+ end
13
12
 
14
- options = SignedForm.options.merge options
15
- options[:builder] ||= ::ActionView::Helpers::FormBuilder
13
+ if ::ActionView::Helpers::FormHelper.method_defined?(:form_with)
14
+ def form_with(**options, &block)
15
+ return super unless signed_form_enabled?(options)
16
16
 
17
- ancestors = options[:builder].ancestors
17
+ options = signed_builder_options(options)
18
+ if options[:sign_destination]
19
+ options[:signed_form_url] = signed_form_url(options)
20
+ end
18
21
 
19
- if !ancestors.include?(::ActionView::Helpers::FormBuilder) && !ancestors.include?(SignedForm::FormBuilder)
20
- raise "Form signing not supported on builders that don't subclass ActionView::Helpers::FormBuilder or include SignedForm::FormBuilder"
21
- elsif !ancestors.include?(SignedForm::FormBuilder)
22
- options[:builder] = SignedForm::FormBuilder::BUILDERS[options[:builder]]
22
+ super(**options) do |form|
23
+ signed_form_body(form, &block)
24
+ end
23
25
  end
26
+ end
27
+
28
+ private
29
+
30
+ def signed_form_enabled?(options)
31
+ options[:signed].nil? ? SignedForm.options[:signed] : options[:signed]
32
+ end
33
+
34
+ def signed_builder_options(options)
35
+ merged = SignedForm.options.merge(options)
36
+ builder = merged[:builder] || ::ActionView::Helpers::FormBuilder
37
+ merged[:builder] = SignedForm::FormBuilder.signed_builder(builder)
38
+ merged
39
+ end
24
40
 
25
- super record, options do |f|
26
- output = capture(f, &block)
27
- f.form_signature_tag + output
41
+ def signed_form_url(options)
42
+ url = options[:url]
43
+ html = options[:html] || {}
44
+ # With no action attribute the browser submits to the current document.
45
+ return request.fullpath if url == false || html[:action] == false || html['action'] == false
46
+
47
+ if url.nil? && options[:model]
48
+ url = polymorphic_path(options[:model], options.slice(:format).compact)
28
49
  end
50
+ url_for(url || {})
51
+ end
52
+
53
+ def signed_form_body(form, &block)
54
+ body = capture(form, &block)
55
+ form.form_signature_tag + body
29
56
  end
30
57
  end
31
58
  end
@@ -1,3 +1,4 @@
1
+ require 'digest/md5'
1
2
  require 'set'
2
3
 
3
4
  module SignedForm
@@ -28,7 +29,6 @@ module SignedForm
28
29
  def marshal_load(input)
29
30
  @views, @digest = input
30
31
  @view_paths = []
31
- @digest.taint
32
32
  end
33
33
 
34
34
  def to_s