Skip to content

Instantly share code, notes, and snippets.

View hopsoft's full-sized avatar

Nate Hopkins hopsoft

View GitHub Profile
@hopsoft
hopsoft / user.rb
Created March 15, 2012 04:19
User - cancan abstract resource authorization
class User < ActiveRecord::Base
has_and_belongs_to_many :associates,
:class_name => "User",
:join_table => "connections",
:association_foreign_key => "connection_user_id"
end
@hopsoft
hopsoft / routes.rb
Created March 15, 2012 04:37
Routes - cancan abstract resource authorization
MyApp::Application.routes.draw do
resources :users
resources :associates
end
@hopsoft
hopsoft / ability.rb
Created March 15, 2012 04:41
Ability - cancan abstract resource authorization
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new
resourceful_actions = [
:index,
:show,
:new,
@hopsoft
hopsoft / ability.rb
Created March 15, 2012 04:49
Ability 2 - cancan abstract resource authorization
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new
resourceful_actions = [
:index,
:show,
:new,
@hopsoft
hopsoft / json_hashifier.rb
Created March 15, 2012 20:43
Hashify helper for ActiveRecord models
module JsonHashifier
# Converts an ActiveRecord model object to a JSON appropriate Hash.
# @param [Hash] options
# @option options [String] :timezone The timezone to apply to the created_at & updated_at columns
# @option options [Integer] :timezone_offset The timezone offset that should be used to determine the timezone
# @option options [String] :date_format The strftime format to apply to the created_at & updated_at columns
# @option options [Boolean] :indifferent_access Indicates whether or not to return a HashWithIndifferentAccess
# @options options [Array<String,Symbol>] :whitelist A white list of attributes to keep in the Hash. Defaults to all attributes.
# @options options [Array<String,Symbol>] :methods A list of methods to invoke for inclusion in the Hash.
@hopsoft
hopsoft / tricks.sh
Created March 20, 2012 15:11
A list of tips and tricks for the shell
# search all installed gems
$ rvm gemdir | xargs ack "def fail" {}
# search within a specific gem
$ bundle show cancan | xargs ack "def can?" {}
# open a gem in Sublime
$ bundle show warden | xargs subl {}
@hopsoft
hopsoft / gist:2368535
Created April 12, 2012 15:52
Get the currently executing method's name
def current_method_name
caller[0][/`([^']*)'/, 1].to_sym
end
@hopsoft
hopsoft / readme2ghpage.rb
Created June 21, 2012 17:09
Convert your README.md on master to index.md on gh-pages
#!/usr/bin/env ruby
# checkout the readme from the master branch
`git checkout gh-pages; git checkout master README.md`
path = `pwd`.gsub(/\n/, "")
readme_path = File.join(path, "README.md")
index_path = File.join(path, "index.md")
# write the index readme file
@hopsoft
hopsoft / gist:3147080
Created July 19, 2012 21:48
Divide an Array equally
class Array
# Divides the Array into equally sized chunks.
#
# @example
# list = ["a", "b", "c", "d", "e", "f"]
# list.divide(3) # => [["a", "b"], ["c", "d"], ["e", "f"]]
#
# @param [Integer] count A recommended number of chunks (sub lists) to create.
# The calculated chunk size is rounded up,
@hopsoft
hopsoft / grumpy_old_man.rb
Created July 27, 2012 05:56
Asserts for RSpec
# An RSpec shim that provides old school assert methods.
# My contention is that its better to write tests in the same fashion that you write the app.
# This approach is faster since it does away with the context switching between typical everyday logic and a wonky DSL.
# The barrier to entry is also lower for the unitiated RSpec user.
#
# Consider the following example from the RSpec docs.
# expect(order.total).to eq(Money.new(5.55, :USD))
#
# Rewritten with GrumpyOldMan.
# assert_equal order.total, Money.new(5.55, :USD)