diff options
author | Lin Jen-Shin <godfat@godfat.org> | 2019-03-08 18:28:59 +0300 |
---|---|---|
committer | Lin Jen-Shin <godfat@godfat.org> | 2019-03-19 08:01:37 +0300 |
commit | 364791c9f02c5e1d44776fa8e48453dd447d4630 (patch) | |
tree | fbea2b3b45fa59ac4b3419f8f2346b786236e88b /app/models/concerns/token_authenticatable.rb | |
parent | 6b0d493350ed9288c4a68a35dbbb4a4b91bd9637 (diff) |
Update TokenAuthenticatable so methods can be overridden
Diffstat (limited to 'app/models/concerns/token_authenticatable.rb')
-rw-r--r-- | app/models/concerns/token_authenticatable.rb | 19 |
1 files changed, 13 insertions, 6 deletions
diff --git a/app/models/concerns/token_authenticatable.rb b/app/models/concerns/token_authenticatable.rb index f5bb559ceda..8c769be0489 100644 --- a/app/models/concerns/token_authenticatable.rb +++ b/app/models/concerns/token_authenticatable.rb @@ -26,34 +26,41 @@ module TokenAuthenticatable end end - define_method(token_field) do + mod = token_authenticatable_module + + mod.define_method(token_field) do strategy.get_token(self) end - define_method("set_#{token_field}") do |token| + mod.define_method("set_#{token_field}") do |token| strategy.set_token(self, token) end - define_method("ensure_#{token_field}") do + mod.define_method("ensure_#{token_field}") do strategy.ensure_token(self) end # Returns a token, but only saves when the database is in read & write mode - define_method("ensure_#{token_field}!") do + mod.define_method("ensure_#{token_field}!") do strategy.ensure_token!(self) end # Resets the token, but only saves when the database is in read & write mode - define_method("reset_#{token_field}!") do + mod.define_method("reset_#{token_field}!") do strategy.reset_token!(self) end - define_method("#{token_field}_matches?") do |other_token| + mod.define_method("#{token_field}_matches?") do |other_token| token = read_attribute(token_field) token.present? && ActiveSupport::SecurityUtils.variable_size_secure_compare(other_token, token) end end + def token_authenticatable_module + @token_authenticatable_module ||= + const_set(:TokenAuthenticatable, Module.new).tap(&method(:include)) + end + def token_authenticatable_fields @token_authenticatable_fields ||= [] end |