Permalink
Browse files

embedded list returns the parent collection

  • Loading branch information...
1 parent 8d08210 commit 59e82451ce3146d9877d15cce8f2ffbfc0c11f0a @curthasselschwert curthasselschwert committed Jul 28, 2011
View
@@ -4,3 +4,8 @@ source "http://rubygems.org"
gemspec
gem 'bson_ext', '>= 1.0.4'
+
+gem 'guard', :group => :development
+gem 'guard-test', :group => :development
+gem 'rb-fsevent', :group => :development
+gem 'growl', :group => :development
View
@@ -0,0 +1,8 @@
+# A sample Guardfile
+# More info at https://github.com/guard/guard#readme
+
+guard 'test' do
+ watch(%r{^lib/(.+)\.rb$}) { |m| "test/#{m[1]}_test.rb" }
+ watch(%r{^test/.+_test\.rb$})
+ watch('test/test_helper.rb') { "test" }
+end
View
@@ -1 +1,10 @@
require 'bundler/gem_tasks'
+require 'rake/testtask'
+
+task :default => 'test'
+
+Rake::TestTask.new(:test) do |t|
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = true
+ t.libs << 'test'
+end
@@ -2,6 +2,10 @@ module Mongoid
module EmbeddedList
+ def parent_collection
+ _parent.send self.class.to_s.pluralize.underscore
+ end
+
end
end
@@ -18,4 +18,5 @@ Gem::Specification.new do |s|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]
s.add_runtime_dependency 'mongoid', '>= 2.0.0'
+ s.add_runtime_dependency 'active_support', '>= 3.0.0'
end
@@ -0,0 +1,23 @@
+require 'test_helper'
+
+class List
+ include Mongoid::Document
+ embeds_many :items
+end
+
+class Item
+ include Mongoid::Document
+ include Mongoid::EmbeddedList
+ embedded_in :list
+end
+
+class MongoidEmbeddedListTest < Test::Unit::TestCase
+
+ test "it returns the parent collection" do
+ list = List.create
+ 3.times { list.items.create }
+ item = list.items.first
+ assert_equal list.items, item.parent_collection
+ end
+
+end
View
@@ -0,0 +1,11 @@
+require 'rubygems'
+require 'test/unit'
+require 'bundler/setup'
+
+require 'mongoid'
+require 'mongoid_embedded_list'
+
+Mongoid.configure do |config|
+ config.master = Mongo::Connection.new.db('mongoid_embedded_list_test')
+ config.allow_dynamic_fields = false
+end

0 comments on commit 59e8245

Please sign in to comment.