aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/secure_token.rb
diff options
context:
space:
mode:
authorKasper Timm Hansen <kaspth@gmail.com>2016-01-09 23:21:18 +0100
committerKasper Timm Hansen <kaspth@gmail.com>2016-01-09 23:21:18 +0100
commita3ab6ad00872d24d4d87637f93fdae798d0edc79 (patch)
tree8892ff3dcd95eb1a1aada503a2e52d4146c6bb94 /activerecord/lib/active_record/secure_token.rb
parent62abae917281f18b882abb6222d64d049e755047 (diff)
downloadrails-a3ab6ad00872d24d4d87637f93fdae798d0edc79.tar.gz
rails-a3ab6ad00872d24d4d87637f93fdae798d0edc79.tar.bz2
rails-a3ab6ad00872d24d4d87637f93fdae798d0edc79.zip
Switch `has_secure_token` to `before_save`.
Adding `if` and `unless` support doesn't bode well if the callback to assign a token only runs on create. Switch to `before_save`, but keep the conditional so that no token already assigned is overriden.
Diffstat (limited to 'activerecord/lib/active_record/secure_token.rb')
-rw-r--r--activerecord/lib/active_record/secure_token.rb7
1 files changed, 4 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/secure_token.rb b/activerecord/lib/active_record/secure_token.rb
index f10f7a1515..80a5018f22 100644
--- a/activerecord/lib/active_record/secure_token.rb
+++ b/activerecord/lib/active_record/secure_token.rb
@@ -41,13 +41,14 @@ module ActiveRecord
# Note that it's still possible to generate a race condition in the database in the same way that
# {validates_uniqueness_of}[rdoc-ref:Validations::ClassMethods#validates_uniqueness_of] can.
# You're encouraged to add a unique index in the database to deal with this even more unlikely scenario.
- def has_secure_token(attribute = :token, **before_create_options)
+ def has_secure_token(attribute = :token, **before_save_options)
# Load securerandom only when has_secure_token is used.
require 'active_support/core_ext/securerandom'
define_method("regenerate_#{attribute}") { update! attribute => self.class.generate_unique_secure_token }
- before_create(before_create_options) do
- self.send("#{attribute}=", self.class.generate_unique_secure_token) unless self.send("#{attribute}?")
+
+ before_save(before_save_options) do
+ send("#{attribute}=", self.class.generate_unique_secure_token) unless send("#{attribute}?")
end
end