Skip to content

Instantly share code, notes, and snippets.

@jnunemaker
jnunemaker / database.yml
Created November 12, 2009 14:59
mongo initializer to load config from database.yml, authenticate if needed and ensure indexes are created
development: &global_settings
database: textual_development
host: 127.0.0.1
port: 27017
test:
database: textual_test
<<: *global_settings
production:
@jnunemaker
jnunemaker / push_unique.rb
Created November 18, 2009 05:18
push unique in mongodb
require 'pp'
require 'rubygems'
require 'mongo_mapper'
MongoMapper.database = 'testing'
class Site
include MongoMapper::Document
key :domain, String
key :authorizations, Array
require 'open-uri'
# url dsl -- the ultimate url dsl!
#
# You just can't beat this:
#
# $ irb -r url_dsl
# >> include URLDSL
# => Object
# >> http://github.com/defunkt.json
@jnunemaker
jnunemaker / remove_key.rake
Created December 18, 2009 19:49
Simple example of how to remove a key from a mongo document
namespace :harmony do
desc "Munges the data"
task :munge => :environment do
docs_with_publish = Item.collection.find({'publish' => {'$exists' => true}}).to_a
puts "Item count: #{Item.count}"
puts "Items with publish key: #{docs_with_publish.size}"
docs_with_publish.each do |hash|
hash.delete('publish')
@jnunemaker
jnunemaker / test_helper.rb
Created December 19, 2009 06:46
clearing collections in between tests with mongo
class ActiveSupport::TestCase
def setup
clear_all_collections
end
def clear_all_collections
Dir[Rails.root + 'app/models/**/*.rb'].each do |model_path|
klass = File.basename(model_path, '.rb').classify.constantize
klass.collection.remove if klass.respond_to?(:collection)
end
@jnunemaker
jnunemaker / embedded_uniqueness_validations.rb
Created December 23, 2009 13:52
How to validate uniqueness of embedded objects
require 'pp'
require 'rubygems'
require 'mongo_mapper'
MongoMapper.database = 'testing'
class Rating
include MongoMapper::EmbeddedDocument
key :user_id, ObjectId
@jnunemaker
jnunemaker / example_use_with_comment.rb
Created January 4, 2010 19:39
example of how easy gravatar support is without a plugin/gem
class Comment
include MongoMapper::EmbeddedDocument
include Gravatarable
key :name, String
key :email, String
key :url, String
key :body, String
key :created_at, Time
@jnunemaker
jnunemaker / mongo_mapper_new_relic.rb
Created January 8, 2010 09:04
MongoMapper NewRelic Integration
# 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,
@jnunemaker
jnunemaker / navvy_god_config.rb
Created February 11, 2010 19:50
navvy god config
God.watch do |w|
w.name = "navvy-1"
w.group = 'navvy'
w.interval = 30.seconds
w.start = "rake navvy:work"
w.dir = RAILS_ROOT
w.env = {'RAILS_ENV' => RAILS_ENV}
w.log = "#{RAILS_ROOT}/log/navvy.log"
w.uid = 'rails'
@jnunemaker
jnunemaker / display_failures_instantly.rb
Created February 15, 2010 22:00
shows test unit failures immediately
require 'test/unit/ui/console/testrunner'
class Test::Unit::UI::Console::TestRunner
def add_fault(fault)
hax_output(fault)
@faults << fault
output_single(fault.single_character_display, 1)
@already_outputted = true
end
def hax_output(fault)