Skip to content

Instantly share code, notes, and snippets.

View batasrki's full-sized avatar

Srdjan Pejic batasrki

  • S-Squared Design & Development
  • Toronto, ON
View GitHub Profile
require "stateful"
class Folder < ActiveRecord::Base
include Stateful
# ...
stateful do
state :active
state :inactive
if Rails.env.production?
Braintree::Configuration.environment = Rails.env.staging? ? :sandbox : :production
Braintree::Configuration.merchant_id = ENV["braintree_merchant_id"]
Braintree::Configuration.public_key = ENV["braintree_public_key"]
Braintree::Configuration.private_key = ENV["braintree_private_key"]
else
Braintree::Configuration.environment = :sandbox
Braintree::Configuration.merchant_id = "<super secret>"
Braintree::Configuration.public_key = "<super secret>"
Braintree::Configuration.private_key = "<super secret>"
# Monkey patching required for compatibility between MongoMapper (v. 0.8.6) and
# the master branch of Devise heading towards 1.2. It seems some methods have been moved
# around to different modules/classes within Devise since the 1.1 branch
module Devise
module Orm
module MongoMapper
module Hook
def devise_modules_hook!
extend Schema
#! /usr/bin/env ruby
## this shows how a pipe consumer needs to consume all the stdout and stderr
# being created or the child process will hang. once the child has hung the
# parent too will hang.
#
require 'open3'
require 'timeout'
require 'uri'
require 'psych'
require 'net/http'
require 'meme' # Install meme_generator
module Campfire
class API
attr_reader :uri, :token, :pass
@batasrki
batasrki / gist:821766
Created February 11, 2011 01:40 — forked from foca/gist:821116
o1 = Object.new
o2 = Object.new
def (1 > 2 ? o1 : o2).blah
puts "blah"
end
o2.blah #=> puts "blah"
o1.blah #=> NoMethodError
class GlobalStats
include MongoMapper::Document
key :attempts, Attempts
end
class Attempts
attr_accessor :total, :hours
def self.to_mongo(attempts)
{
require 'digest/sha2'
class User < ActiveRecord::Base
class << self
def authenticate(username, password)
user = find_by_username(username)
if user && user.authenticate(password)
user
else
nil
# Have to use ActiveRecord so that New Relic shows it on all graphs.
# The push scope false stuff makes it so that you can track usage by model and overall.
if defined?(NewRelic)
module MongoMapperNewRelic
def self.included(model)
mm_class_methods = [
:find,
:find!,
:paginate,
:first,

Instrument Anything in Rails 3

With Rails 3.0 released a few weeks ago I've migrated a few apps and I'm constantly finding useful new improvements. One such improvement is the ability to log anything in the same way that Rails internally logs ActiveRecord and ActionView. By default Rails 3 logs look slightly spiffier than those produced by Rails 2.3: (notice the second line has been cleaned up)

Started GET "/" for 127.0.0.1 at Mon Sep 06 01:07:11 -0400 2010
  Processing by HomeController#index as HTML
  User Load (0.2ms)  SELECT `users`.* FROM `users` WHERE (`users`.`id` = 3) LIMIT 1
  CACHE (0.0ms)  SELECT `users`.* FROM `users` WHERE (`users`.`id` = 3) LIMIT 1

Rendered layouts/_nav.html.erb (363.4ms)