Created
June 1, 2017 11:41
-
-
Save vasilakisfil/5679dc66cc622a322881fc98a3fff7a2 to your computer and use it in GitHub Desktop.
Rails flash improvements
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
module ActionDispatch | |
class Flash | |
class FlashHash | |
# Returns a hash that includes everything but the given keys. | |
# hash = { a: true, b: false, c: nil} | |
# hash.except(:c) # => { a: true, b: false} | |
# hash # => { a: true, b: false, c: nil} | |
# | |
# This is useful for limiting a set of parameters to everything but a few known toggles: | |
# @person.update(params[:person].except(:admin)) | |
def except(*keys) | |
HashWithIndifferentAccess.new(@flashes).dup.except!(*keys) | |
end | |
# Replaces the hash without the given keys. | |
# hash = { a: true, b: false, c: nil} | |
# hash.except!(:c) # => { a: true, b: false} | |
# hash # => { a: true, b: false } | |
def except!(*keys) | |
keys.each { |key| delete(key) } | |
self | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment