Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save searls/d3f5c90bf6310f5eafa5d59a2c61eff0 to your computer and use it in GitHub Desktop.
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
# 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