Skip to content

Instantly share code, notes, and snippets.

@hakanensari
Created May 29, 2010 11:39
Show Gist options
  • Select an option

  • Save hakanensari/418236 to your computer and use it in GitHub Desktop.

Select an option

Save hakanensari/418236 to your computer and use it in GitHub Desktop.

Rails 3 with Mongoid

Notes to self

echo rvm ree@rails3 > .rvmrc

To apply individual files while in Rails.root:

rails application ./ -OTJs -m "<raw_path>"

Start mongo at load on Mac OS X:

launchctl unload -w ~/Library/LaunchAgents/org.mongodb.mongod.plist
cp /usr/local/Cellar/mongodb/1.4.3-x86_64/org.mongodb.mongod.plist ~/Library/LaunchAgents
launchctl load -w ~/Library/LaunchAgents/org.mongodb.mongod.plist
rails paper_cavalier --skip-activerecord --skip-testunit --skip-prototype
git :init
git :add => "."
git :commit => "-am 'Initial commit of Rails 3 app without ActiveRecord, TestUnit, and Prototype'"
gem 'mongoid', '2.0.0.beta6'
gem 'bson_ext', '1.0.1'
run "bundle install"
generate "mongoid:config"
git :add => "."
git :commit => "-am 'Set up Mongoid'"
gem 'cucumber', '0.7.3', :group => :test
gem 'cucumber-rails', '0.3.1', :group => :test
gem 'faker', '0.3.1', :group => :test
gem 'launchy', '0.3.5', :group => :test
gem 'machinist_mongo', '1.1.0', :group => :test
gem 'rspec', '2.0.0.beta.9', :group => :test
gem 'rspec-rails', '2.0.0.beta.9.1', :group => :test
run 'bundle install'
file 'config/database.yml', <<-EOF
# Using MongoDB
EOF
generate 'rspec:install'
generate 'cucumber:skeleton'
run 'mkdir spec/support'
file 'spec/support/blueprints.rb', <<-EOF
require "machinist/mongoid"
require "sham"
require "faker"
EOF
file 'features/support/machinist.rb', <<-EOF
require File.dirname(__FILE__) + '/../../spec/support/blueprints'
Before { Sham.reset }
EOF
git :add => '.'
git :commit => "-am 'Set up Cucumber, RSpec, and Machinist'"
file 'spec/support/mongodb_cleaner.rb', <<-EOF
Rspec.configure do |config|
config.use_transactional_examples = false
config.before(:each) do
Mongoid.master.collections.each(&:drop)
end
end
EOF
file 'features/support/mongodb_cleaner.rb', <<-EOF
Before do
Mongoid.master.collections.each(&:drop)
end
EOF
run "perl -pi -e 's/^Cucumber::Rails::World.use_tr/# Cucumber::Rails::World.use_tr/g' features/support/env.rb"
lib 'tasks/mongo.rake', <<-EOF
namespace :db do
namespace :test do
task :prepare do
# Stub out for MongoDB
end
end
end
EOF
git :add => '.'
git :commit => "-am 'Mongoified Cucumber and RSpec'"
# http://github.com/nicolaracco/mongo_session_store
gem 'mongo_session_store', :git => 'git://github.com/nicolaracco/mongo_session_store.git'
run 'bundle install'
file 'config/initializers/session_store.rb', <<-EOF
require "mongo_session_store/mongoid"
ActionController::Base.session_store = :mongoid_store
EOF
git :commit => "-am 'Switched to Mongo session store'"
file = "config/application.rb"
text = File.read(file)
File.open(file, 'w') do |f|
f << text.gsub(/# config.generators[^\n]+\n[^\n]+\n[^\n]+\n[^\n]+\n[^#]+# end\n/, <<-EOF
config.generators do |g|
g.orm :mongoid
g.template_engine :erb
g.test_framework :rspec, :fixture => false
end
EOF
)
end
git :commit => "-am 'Configured generators to use Mongoid and RSpec'"
run "rm public/index.html public/favicon.ico public/images/rails.png"
run "echo TODO > README"
run "mv README README.textile"
git :commit => "-am 'Removed index, favicon, rails.png, and edited, moved README'"
gem 'pickle', '0.2.11'
run 'bundle install'
file 'features/support/pickle.rb', <<-EOF
# Make sure that you are loading your factory of choice in your cucumber environment
#
# For machinist add: features/support/machinist.rb
#
# require 'machinist/active_record' # or your chosen adaptor
# require File.dirname(__FILE__) + '/../../spec/blueprints' # or wherever your blueprints are
# Before { Sham.reset } # to reset Sham's seed between scenarios so each run has same random sequences
#
# For FactoryGirl add: features/support/factory_girl.rb
#
# require 'factory_girl'
# require File.dirname(__FILE__) + '/../../spec/factories' # or wherever your factories are
#
# You may also need to add gem dependencies on your factory of choice in <tt>config/environments/cucumber.rb</tt>
require 'pickle/world'
# Example of configuring pickle:
#
# Pickle.configure do |config|
# config.adapters = [:machinist]
# config.map 'I', 'myself', 'me', 'my', :to => 'user: "me"'
# end
Pickle.configure do |config|
config.adapters = [:machinist]
end
EOF
file 'features/step_definitions/pickle_steps.rb', <<-EOF
# create a model
Given(/^\#{capture_model} exists?(?: with \#{capture_fields})?$/) do |name, fields|
create_model(name, fields)
end
# create n models
Given(/^(\d+) \#{capture_plural_factory} exist(?: with \#{capture_fields})?$/) do |count, plural_factory, fields|
count.to_i.times { create_model(plural_factory.singularize, fields) }
end
# create models from a table
Given(/^the following \#{capture_plural_factory} exists?:?$/) do |plural_factory, table|
create_models_from_table(plural_factory, table)
end
# find a model
Then(/^\#{capture_model} should exist(?: with \#{capture_fields})?$/) do |name, fields|
find_model!(name, fields)
end
# not find a model
Then(/^\#{capture_model} should not exist(?: with \#{capture_fields})?$/) do |name, fields|
find_model(name, fields).should be_nil
end
# find models with a table
Then(/^the following \#{capture_plural_factory} should exists?:?$/) do |plural_factory, table|
find_models_from_table(plural_factory, table).should_not be_any(&:nil?)
end
# find exactly n models
Then(/^(\d+) \#{capture_plural_factory} should exist(?: with \#{capture_fields})?$/) do |count, plural_factory, fields|
find_models(plural_factory.singularize, fields).size.should == count.to_i
end
# assert equality of models
Then(/^\#{capture_model} should be \#{capture_model}$/) do |a, b|
model!(a).should == model!(b)
end
# assert model is in another model's has_many assoc
Then(/^\#{capture_model} should be (?:in|one of|amongst) \#{capture_model}(?:'s)? (\w+)$/) do |target, owner, association|
model!(owner).send(association).should include(model!(target))
end
# assert model is not in another model's has_many assoc
Then(/^\#{capture_model} should not be (?:in|one of|amongst) \#{capture_model}(?:'s)? (\w+)$/) do |target, owner, association|
model!(owner).send(association).should_not include(model!(target))
end
# assert model is another model's has_one/belongs_to assoc
Then(/^\#{capture_model} should be \#{capture_model}(?:'s)? (\w+)$/) do |target, owner, association|
model!(owner).send(association).should == model!(target)
end
# assert model is not another model's has_one/belongs_to assoc
Then(/^\#{capture_model} should not be \#{capture_model}(?:'s)? (\w+)$/) do |target, owner, association|
model!(owner).send(association).should_not == model!(target)
end
# assert model.predicate?
Then(/^\#{capture_model} should (?:be|have) (?:an? )?\#{capture_predicate}$/) do |name, predicate|
if model!(name).respond_to?("has_\#{predicate.gsub(' ', '_')}")
model!(name).should send("have_\#{predicate.gsub(' ', '_')}")
else
model!(name).should send("be_\#{predicate.gsub(' ', '_')}")
end
end
# assert not model.predicate?
Then(/^\#{capture_model} should not (?:be|have) (?:an? )?\#{capture_predicate}$/) do |name, predicate|
if model!(name).respond_to?("has_\#{predicate.gsub(' ', '_')}")
model!(name).should_not send("have_\#{predicate.gsub(' ', '_')}")
else
model!(name).should_not send("be_\#{predicate.gsub(' ', '_')}")
end
end
# model.attribute.should eql(value)
# model.attribute.should_not eql(value)
Then(/^\#{capture_model}'s (\w+) (should(?: not)?) be \#{capture_value}$/) do |name, attribute, expectation, expected|
actual_value = model(name).send(attribute)
expectation = expectation.gsub(' ', '_')
case expected
when 'nil', 'true', 'false'
actual_value.send(expectation, send("be_\#{expected}"))
when /^[+-]?[0-9_]+(\.\d+)?$/
actual_value.send(expectation, eql(expected.to_f))
else
actual_value.to_s.send(expectation, eql(eval(expected)))
end
end
# assert size of association
Then /^\#{capture_model} should have (\d+) (\w+)$/ do |name, size, association|
model!(name).send(association).size.should == size.to_i
end
EOF
git :add => '.'
git :commit => "-am 'Added Pickle for Cucumber'"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment