Skip to content

Instantly share code, notes, and snippets.

@nmk
Created November 7, 2009 05:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nmk/228556 to your computer and use it in GitHub Desktop.
Save nmk/228556 to your computer and use it in GitHub Desktop.
# recent github checkout
$:.unshift(File.dirname(__FILE__) + '/lib/mongomapper/lib/')
require 'mongo_mapper'
require 'spec'
MongoMapper.database = 'tmp'
class Party
include MongoMapper::Document
many :guests
def invite(name)
if guests.any? { |g| g.name == name }
errors.add(:guests, 'duplicate name') # after this the errors stay empty
else
guests << Guest.new(:name => name)
end
end
end
class Guest
include MongoMapper::EmbeddedDocument
key :name, :required => true
end
context 'Party' do
before(:each){ Party.collection.remove }
it 'is boring without guests' do
party = Party.create
party.invite('George')
party.should be_valid
party.guests.length.should == 1
end
it 'leads to confusion if guests are named the same' do
party = Party.create
party.invite('George')
party.invite('Melinda')
party.invite('George')
party.guests.length.should == 2 # this is ok
party.should_not be_valid # this fails
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment