New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Specify when to generate has_secure_token
#47420
Merged
guilleiguaran
merged 1 commit into
rails:main
from
seanpdoyle:has-secure-token-callback
Jul 18, 2023
Merged
Specify when to generate has_secure_token
#47420
guilleiguaran
merged 1 commit into
rails:main
from
seanpdoyle:has-secure-token-callback
Jul 18, 2023
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
seanpdoyle
commented
Feb 16, 2023
seanpdoyle
force-pushed
the
has-secure-token-callback
branch
from
February 16, 2023 23:04
eae8663
to
2929e30
Compare
p8
changed the title
Sepcify when to generate
Specify when to generate Feb 17, 2023
has_secure_token
has_secure_token
zzak
reviewed
Feb 18, 2023
seanpdoyle
force-pushed
the
has-secure-token-callback
branch
from
February 18, 2023 14:22
2929e30
to
6bb2149
Compare
seanpdoyle
force-pushed
the
has-secure-token-callback
branch
2 times, most recently
from
February 21, 2023 17:47
0d21192
to
3a02994
Compare
The callback when the value is generated. When called with `on: :initialize`, the value is generated in an `after_initialize` callback, otherwise the value will be used in a `before_` callback. It will default to `:create`.
seanpdoyle
force-pushed
the
has-secure-token-callback
branch
from
July 18, 2023 18:48
3a02994
to
7d63864
Compare
4 tasks
@guilleiguaran thank you for reviewing and merging this work! As a follow-up, I've opened #48912 to propose |
rafaelfranca
pushed a commit
to seanpdoyle/rails
that referenced
this pull request
Sep 1, 2023
Follow-up to [rails#47420][] With the changes made in [rails#47420][], `has_secure_token` declarations can be configured to execute in an `after_initialize` callback. This commit proposed a new Rails 7.1 default: generate all `has_secure_token` values when their corresponding models are initialized. To preserve pre-7.1 behavior, applications can set `config.active_record.generate_secure_token_on = :create`. By default, generate the value when the model is initialized: ```ruby class User < ApplicationRecord has_secure_token end record = User.new record.token # => "fwZcXX6SkJBJRogzMdciS7wf" ``` With `config.active_record.generate_secure_token_on = :create`, generate the value when the model is created: ```ruby # config/application.rb config.active_record.generate_secure_token_on = :create # app/models/user.rb class User < ApplicationRecord has_secure_token on: :create end record = User.new record.token # => nil record.save! record.token # => "fwZcXX6SkJBJRogzMdciS7wf" ``` [rails#47420]: rails#47420 Co-authored-by: Hartley McGuire <skipkayhil@gmail.com>
kamipo
added a commit
that referenced
this pull request
Sep 5, 2023
…n: :create` Follow-up to #47420. Whereas the original behavior (`on: :create`) is invoked only once before a record is persisted, the new behavior (`on: :initialize`) is invoked not only new record but also persisted records. It should be invoked only once for new record consistently.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Motivation / Background
When a model defines
has_secure_token
, that value isn't available at initialization-time.Detail
Extend
has_secure_token
to accept anon:
option to specify the callback. Continues to default tobefore_create
.The callback when the value is generated. When called with
on: :initialize
, the value is generated in anafter_initialize
callback, otherwise the value will be used in abefore_
callback. It will default to:create
.Checklist
Before submitting the PR make sure the following are checked:
[Fix #issue-number]