Skip to content

Instantly share code, notes, and snippets.

@bowsersenior
bowsersenior / cancan_mongoid.rb
Created September 2, 2010 00:42 — forked from blackgold9/mongoid_addtions.rb
How to get CanCan to work with Mongoid 2
module CanCan
class Query
def sanitize_sql(conditions)
conditions
end
end
# customize to handle Mongoid queries in ability definitions conditions
class CanDefinition
def matches_conditions_hash?(subject, conditions = @conditions)
module Workflow
module MongoidInstanceMethods
def load_workflow_state
self.workflow_state
end
def persist_workflow_state(new_value)
self.workflow_state = new_value
save!
end
%w(rubygems mongoid rspec).each do |gem|
require gem
end
Mongoid.configure do |config|
name = "dirty_track_test"
host = "localhost"
config.master = Mongo::Connection.new.db(name)
config.persist_in_safe_mode = false
end
@bowsersenior
bowsersenior / gist:768344
Created January 6, 2011 18:57
Improved .rvmrc for cancan
#!/usr/bin/env bash
# adapted from: http://rvm.beginrescueend.com/workflow/rvmrc/
ruby_string="1.8.7"
gemset_name="cancan"
if rvm list strings | grep -q "${ruby_string}" ; then
# Load or create the specified environment
namespace :gource do
desc "Build gource file"
task :build => :environment do
system("gource -s .05 -800x600 --disable-progress --stop-at-end --bloom-multiplier .4 --bloom-intensity 1.5 --hide-filenames --output-framerate 30 --user-image-dir #{Rails.root}/.git/avatar --output-ppm-stream #{Rails.root}/gource/pathways.ppm #{Rails.root}")
system("ffmpeg -y -b 3000k -r 30 -f image2pipe -vcodec ppm -i #{Rails.root}/gource/pathways.ppm -vcodec libx264 -vpre hq -vpre fastfirstpass #{Rails.root}/gource/pathways.mp4")
system("rm #{Rails.root}/gource/pathways.ppm")
end
end
class String
def bling
self + "inator"
end
def bling!
replace(self.bling)
end
end
require 'acceptance/acceptance_helper'
feature "Articles", %q{
In order to have an awesome blog
As an author
I want to create and manage articles
} do
background do
Article.create!(:title => 'One')
@bowsersenior
bowsersenior / closures-in-ruby.rb
Created March 23, 2011 23:33
A copy of Paul Cantrell's excellent info on ruby closures : http://innig.net/software/ruby/closures-in-ruby.rb
# CLOSURES IN RUBY Paul Cantrell http://innig.net
# Email: username "cantrell", domain name "pobox.com"
# I recommend executing this file, then reading it alongside its output.
#
# Alteratively, you can give yourself a sort of Ruby test by deleting all the comments,
# then trying to guess the output of the code!
# A closure is a block of code which meets three criteria:
@bowsersenior
bowsersenior / word_frequency_in_swifts_modest_proposal.sh
Created April 27, 2011 00:28
Calculates word frequencies from the text of Jonathan Swift’s, A Modest Proposal from http://tomayko.com/writings/awkward-ruby
# with AWK
curl -s http://www.gutenberg.org/files/1080/1080.txt |
awk '
BEGIN { FS="[^a-zA-Z]+" }
{
for (i=1; i<=NF; i++) {
word = tolower($i)
words[word]++
}
@bowsersenior
bowsersenior / stooge_loader.rb
Created May 18, 2011 23:18
A demo of YAML anchors, references and nested values
require 'rubygems'
require 'yaml'
# A demonstration of YAML anchors, references and handling of nested values
# For more info, see:
# http://atechie.net/2009/07/merging-hashes-in-yaml-conf-files/
stooges = YAML::load( File.read('stooges.yml') )
# => {
# "default" => {