Created
August 15, 2024 12:28
-
-
Save searls/d3f5c90bf6310f5eafa5d59a2c61eff0 to your computer and use it in GitHub Desktop.
Was genuinely surprised Rails `reset_session` doesn't accept a list of exceptions
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
# If you have any session variables that should survive a logout, add them to | |
# the except_for array. Usage from a controller: | |
# | |
# ResetsSessionButItsStillThursdayINeedThese.new.reset(self, | |
# except_for: [:has_logged_in_before]) | |
class ResetsSessionButItsStillThursdayINeedThese | |
def reset(receiver, except_for: []) | |
backup = except_for.map { |key| [key, receiver.session[key]] } | |
receiver.reset_session | |
backup.each { |key, value| receiver.session[key] = value } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment