Skip to content

Instantly share code, notes, and snippets.

@yaronw
yaronw / can_can_rspec_macros.rb
Created January 17, 2012 16:47
An RSpec Macro for Testing That Controller Actions Perform CanCan Authorization
=begin
# This RSpec Macro is for use with the authorization plugin CanCan. It lets you test that a controller checks for a
# specific authorization, and if it fails, that the controller performs a certain action, such as redirecting to a
# different page.
# Example Usages in specs:
it_should_authorize_access_for(:new, Company)
it_should_authorize_access_for(:edit, Company.new) { get :edit, id: @company }
# For more on using it_should_authorize_access_for, see comment block above the definition of it_should_authorize_access_for
# in the code below.
@yaronw
yaronw / hash_merge.rb
Created December 9, 2011 00:20
Ruby Hash Merge Function for Multiple Hashes
# A little function to easily merge multiple hashes.
# Example: Given h1, ..., h4 are hashes,
# hash_merge(h1, h2, h3, h4)
# or
# hash_merge *[h1, h2, h3, h4]
#
# More on this function and the Ruby syntax presented here at
# http://yaronwalfish.com/ruby-hash-merge-function-for-multiple-hashes-inject-and-asterisk-notation/
def hash_merge *hashes
hashes.inject :merge