Skip to content

Instantly share code, notes, and snippets.

@vasilakisfil
Created June 1, 2017 11:41
Show Gist options
  • Save vasilakisfil/5679dc66cc622a322881fc98a3fff7a2 to your computer and use it in GitHub Desktop.
Save vasilakisfil/5679dc66cc622a322881fc98a3fff7a2 to your computer and use it in GitHub Desktop.
Rails flash improvements
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