Skip to content

Instantly share code, notes, and snippets.

@chischaschos
Created January 24, 2011 20:09
Show Gist options
  • Save chischaschos/793861 to your computer and use it in GitHub Desktop.
Save chischaschos/793861 to your computer and use it in GitHub Desktop.
Mongo mapper examples
rvm 1.9.2@mongomapper-examples --create
require 'rubygems'
require 'bundler'
Bundler.require
MongoMapper.connection = Mongo::Connection.new('127.0.0.1', 27017)
database = "test-#{RUBY_VERSION.gsub('.', '-')}"
MongoMapper.database = database
class Tag
include MongoMapper::Document
key :label, String
belongs_to :person
end
class Person
include MongoMapper::Document
key :name, String
key :age, Integer
many :tags, :dependent => :destroy
end
describe "Dependent destroy for many and one associations" do
after(:all) do
MongoMapper.connection.drop_database database
end
before(:all) do
@person = Person.create(:name => 'paco', :age => 15)
tag1 = Tag.create(:label => 'small')
tag2 = Tag.create(:label => 'crazy')
@person.tags << tag1
@person.tags << tag2
@person.save
end
it 'should be correclty set up' do
Person.all.count.should == 1
Tag.all.count.should == 2
Person.first.tags.count == 2
end
context "on many associations" do
context 'when deleting a person' do
it 'should delete all his tags' do
person = Person.where(:name => 'paco').first
person.destroy
Person.all.count.should == 0
Tag.all.count.should == 0
end
end
end
end
require 'rubygems'
require 'bundler'
Bundler.require
MongoMapper.connection = Mongo::Connection.new('127.0.0.1', 27017)
database = "test-#{RUBY_VERSION.gsub('.', '-')}"
MongoMapper.database = database
class Tag
include MongoMapper::Document
key :label, String
belongs_to :person
end
class Person
include MongoMapper::Document
key :name, String
key :age, Integer
many :tags, :dependent => :destroy_all
end
describe "Dependent destroy for many and one associations" do
after(:all) do
MongoMapper.connection.drop_database database
end
before(:all) do
@person = Person.create(:name => 'paco', :age => 15)
tag1 = Tag.create(:label => 'small')
tag2 = Tag.create(:label => 'crazy')
@person.tags << tag1
@person.tags << tag2
@person.save
end
it 'should be correclty set up' do
Person.all.count.should == 1
Tag.all.count.should == 2
Person.first.tags.count == 2
end
context "on many associations" do
context 'when deleting a person' do
it 'should not delete all his tags' do
person = Person.where(:name => 'paco').first
person.destroy
Person.all.count.should == 0
Tag.all.count.should == 2
end
end
end
end
source :gemcutter
gem 'mongo_mapper'
gem 'bson_ext'
gem 'i18n'
gem 'rspec', '~> 2.0.0'
GEM
remote: http://rubygems.org/
specs:
activesupport (3.0.3)
bson (1.1.5)
bson_ext (1.2.0)
diff-lcs (1.1.2)
i18n (0.5.0)
jnunemaker-validatable (1.8.4)
activesupport (>= 2.3.4)
mongo (1.1.5)
bson (>= 1.1.5)
mongo_mapper (0.8.6)
activesupport (>= 2.3.4)
jnunemaker-validatable (~> 1.8.4)
plucky (~> 0.3.6)
plucky (0.3.6)
mongo (~> 1.1)
rspec (2.0.1)
rspec-core (~> 2.0.1)
rspec-expectations (~> 2.0.1)
rspec-mocks (~> 2.0.1)
rspec-core (2.0.1)
rspec-expectations (2.0.1)
diff-lcs (>= 1.1.2)
rspec-mocks (2.0.1)
rspec-core (~> 2.0.1)
rspec-expectations (~> 2.0.1)
PLATFORMS
ruby
DEPENDENCIES
bson_ext
i18n
mongo_mapper
rspec (~> 2.0.0)
require 'rubygems'
require 'bundler'
Bundler.require
MongoMapper.connection = Mongo::Connection.new('127.0.0.1', 27017)
MongoMapper.database = "test1-#{RUBY_VERSION.gsub('.', '-')}"
MongoMapper.database.collections.each { |c| c.drop_indexes }
class Person
include MongoMapper::Document
key :name, String
key :age, Integer
end
class BadPerson < Person
key :habit, String
end
class God
include MongoMapper::Document
key :strengh, String
end
class ChristianGod < God
end
class ZenGod < God
end
require 'rubygems'
require 'bundler'
Bundler.require
MongoMapper.connection = Mongo::Connection.new('127.0.0.1', 27017)
MongoMapper.database = "test1-#{RUBY_VERSION.gsub('.', '-')}"
MongoMapper.database.collections.each { |c| c.drop_indexes }
class Person
include MongoMapper::Document
key :name, String
key :age, Integer
many :tags
def tags
Tag.where(:person_id => self.id).all
end
end
class BadPerson < Person
key :killed, Integer
end
class GoodPerson < Person
key :saved, Integer
end
class Tag
include MongoMapper::Document
key :label, String
belongs_to :person
end
describe "One to many relations" do
context 'One person with many tags' do
before do
bp1 = BadPerson.create(:name => 'Some guy', :age => 123, :killed => 123)
Tag.create(:label => 'Ugly', :person => bp1)
Tag.create(:label => 'Deadly', :person => bp1)
bp2 = GoodPerson.create(:name => 'Other guy', :age => 45, :killed => 0)
Tag.create(:label => 'Nice', :person => bp2)
Tag.create(:label => 'Benevolent', :person => bp2)
end
after do
MongoMapper.connection.drop_database MongoMapper.database.name
end
it 'bad person should have two tags' do
BadPerson.where(:name => 'Some guy').first.tags.count.should == 2
end
it 'good person should have two tags' do
GoodPerson.where(:name => 'Other guy').first.tags.count.should == 2
end
it 'person should have four tags' do
Person.all.each do |person|
person.tags.count.should == 2
end
end
end
end
require 'rubygems'
require 'bundler'
Bundler.require
MongoMapper.connection = Mongo::Connection.new('127.0.0.1', 27017, :logger => Logger.new('./test.log'))
MongoMapper.database = "test1-#{RUBY_VERSION.gsub('.', '-')}"
MongoMapper.database.collections.each { |c| c.drop_indexes }
class Person
include MongoMapper::Document
key :name, String
key :age, Integer
many :tags
end
class Tag
include MongoMapper::Document
key :label, String
belongs_to :person
end
describe "One to many relations" do
context 'One person with many tags' do
before do
p1 = Person.create(:name => 'Some guy', :age => 123)
Tag.create(:label => 'Ugly', :person => p1)
Tag.create(:label => 'Deadly', :person => p1)
end
after do
MongoMapper.connection.drop_database MongoMapper.database.name
end
it 'should have two tags' do
Person.where(:name => 'Some guy').first.tags.count.should == 2
end
end
end
require 'rubygems'
require 'bundler'
Bundler.require
MongoMapper.connection = Mongo::Connection.new('127.0.0.1', 27017)
MongoMapper.database = "test1-#{RUBY_VERSION.gsub('.', '-')}"
MongoMapper.database.collections.each { |c| c.drop_indexes }
class Person
include MongoMapper::Document
key :name, String
key :age, Integer
many :tags, :as => :person
end
class BadPerson < Person
key :killed, Integer
end
class GoodPerson < Person
key :saved, Integer
end
class Tag
include MongoMapper::Document
key :label, String
belongs_to :person, :polymorphic => true
end
describe "A parent wih many subtypes stored in a single document" do
context 'Person(1)..Tag(*)' do
before do
bp1 = BadPerson.create(:name => 'Some guy', :age => 123, :killed => 123)
bp1.tags.create(:label => 'Ugly')
bp1.tags.create(:label => 'Deadly')
bp1.save
bp2 = GoodPerson.create(:name => 'Other guy', :age => 45, :killed => 0)
Tag.create(:label => 'Nice', :person => bp2)
Tag.create(:label => 'Benevolent', :person => bp2)
end
after do
MongoMapper.connection.drop_database MongoMapper.database.name
end
it 'bad person should have two tags' do
BadPerson.where(:name => 'Some guy').first.tags.count.should == 2
end
it 'good person should have two tags' do
GoodPerson.where(:name => 'Other guy').first.tags.count.should == 2
end
it 'person should have four tags' do
Person.all.each do |person|
person.tags.count.should == 2
end
end
it 'Test retrieving person from tags' do
Tag.where(:label => 'Nice').first.person.should be_a(GoodPerson)
end
end
end
require 'rubygems'
require 'bundler'
Bundler.require
MongoMapper.connection = Mongo::Connection.new('127.0.0.1', 27017)
MongoMapper.database = "test1-#{RUBY_VERSION.gsub('.', '-')}"
MongoMapper.database.collections.each { |c| c.drop_indexes }
class Person
include MongoMapper::Document
set_collection_name :moronga
puts collection_name
key :name, String
end
require 'rubygems'
require 'bundler'
Bundler.require
MongoMapper.connection = Mongo::Connection.new('127.0.0.1', 27017)
MongoMapper.database = "test1-#{RUBY_VERSION.gsub('.', '-')}"
MongoMapper.database.collections.each { |c| c.drop_indexes }
class Person
include MongoMapper::Document
key :name, String
key :age, Integer
many :tags, :polymorphic => true
end
class BadPerson < Person
key :killed, Integer
end
class GoodPerson < Person
key :saved, Integer
end
class Tag
include MongoMapper::Document
key :label, String
belongs_to :person
end
describe "One to many relations" do
context 'One person with many tags' do
before do
bp1 = BadPerson.create(:name => 'Some guy', :age => 123, :killed => 123)
bp1.tags.create(:label => 'Ugly')
bp1.tags.create(:label => 'Deadly')
bp1.save
bp2 = GoodPerson.create(:name => 'Other guy', :age => 45, :killed => 0)
Tag.create(:label => 'Nice', :person => bp2)
Tag.create(:label => 'Benevolent', :person => bp2)
end
after do
MongoMapper.connection.drop_database MongoMapper.database.name
end
it 'bad person should have two tags' do
BadPerson.where(:name => 'Some guy').first.tags.count.should == 2
end
it 'good person should have two tags' do
p Tag.all
p Person.all
GoodPerson.where(:name => 'Other guy').first.tags.count.should == 2
end
it 'person should have four tags' do
Person.all.each do |person|
person.tags.count.should == 2
end
end
it 'Test retrieving person from tags' do
Tag.where(:label => 'Nice').first.person.should be_a(GoodPerson)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment