djsun-mongo_mapper 0.5.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +8 -0
- data/LICENSE +20 -0
- data/README.rdoc +39 -0
- data/Rakefile +87 -0
- data/VERSION +1 -0
- data/bin/mmconsole +55 -0
- data/lib/mongo_mapper/associations/base.rb +83 -0
- data/lib/mongo_mapper/associations/belongs_to_polymorphic_proxy.rb +34 -0
- data/lib/mongo_mapper/associations/belongs_to_proxy.rb +22 -0
- data/lib/mongo_mapper/associations/many_documents_as_proxy.rb +27 -0
- data/lib/mongo_mapper/associations/many_documents_proxy.rb +116 -0
- data/lib/mongo_mapper/associations/many_embedded_polymorphic_proxy.rb +33 -0
- data/lib/mongo_mapper/associations/many_embedded_proxy.rb +67 -0
- data/lib/mongo_mapper/associations/many_polymorphic_proxy.rb +11 -0
- data/lib/mongo_mapper/associations/many_proxy.rb +6 -0
- data/lib/mongo_mapper/associations/proxy.rb +74 -0
- data/lib/mongo_mapper/associations.rb +86 -0
- data/lib/mongo_mapper/callbacks.rb +106 -0
- data/lib/mongo_mapper/document.rb +308 -0
- data/lib/mongo_mapper/dynamic_finder.rb +35 -0
- data/lib/mongo_mapper/embedded_document.rb +354 -0
- data/lib/mongo_mapper/finder_options.rb +94 -0
- data/lib/mongo_mapper/key.rb +32 -0
- data/lib/mongo_mapper/observing.rb +50 -0
- data/lib/mongo_mapper/pagination.rb +51 -0
- data/lib/mongo_mapper/rails_compatibility/document.rb +15 -0
- data/lib/mongo_mapper/rails_compatibility/embedded_document.rb +27 -0
- data/lib/mongo_mapper/save_with_validation.rb +19 -0
- data/lib/mongo_mapper/serialization.rb +55 -0
- data/lib/mongo_mapper/serializers/json_serializer.rb +92 -0
- data/lib/mongo_mapper/support.rb +171 -0
- data/lib/mongo_mapper/validations.rb +69 -0
- data/lib/mongo_mapper.rb +95 -0
- data/mongo_mapper.gemspec +156 -0
- data/specs.watchr +32 -0
- data/test/NOTE_ON_TESTING +1 -0
- data/test/custom_matchers.rb +48 -0
- data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +55 -0
- data/test/functional/associations/test_belongs_to_proxy.rb +49 -0
- data/test/functional/associations/test_many_documents_as_proxy.rb +244 -0
- data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +132 -0
- data/test/functional/associations/test_many_embedded_proxy.rb +174 -0
- data/test/functional/associations/test_many_polymorphic_proxy.rb +297 -0
- data/test/functional/associations/test_many_proxy.rb +331 -0
- data/test/functional/test_associations.rb +44 -0
- data/test/functional/test_binary.rb +18 -0
- data/test/functional/test_callbacks.rb +85 -0
- data/test/functional/test_document.rb +964 -0
- data/test/functional/test_embedded_document.rb +97 -0
- data/test/functional/test_logger.rb +20 -0
- data/test/functional/test_pagination.rb +87 -0
- data/test/functional/test_rails_compatibility.rb +30 -0
- data/test/functional/test_validations.rb +279 -0
- data/test/models.rb +169 -0
- data/test/test_helper.rb +30 -0
- data/test/unit/serializers/test_json_serializer.rb +193 -0
- data/test/unit/test_association_base.rb +144 -0
- data/test/unit/test_document.rb +165 -0
- data/test/unit/test_dynamic_finder.rb +125 -0
- data/test/unit/test_embedded_document.rb +643 -0
- data/test/unit/test_finder_options.rb +193 -0
- data/test/unit/test_key.rb +175 -0
- data/test/unit/test_mongomapper.rb +28 -0
- data/test/unit/test_observing.rb +101 -0
- data/test/unit/test_pagination.rb +109 -0
- data/test/unit/test_rails_compatibility.rb +39 -0
- data/test/unit/test_serializations.rb +52 -0
- data/test/unit/test_support.rb +272 -0
- data/test/unit/test_time_zones.rb +40 -0
- data/test/unit/test_validations.rb +503 -0
- metadata +207 -0
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 John Nunemaker
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
= MongoMapper
|
2
|
+
|
3
|
+
Awesome gem for modeling your domain and storing it in mongo.
|
4
|
+
|
5
|
+
Releases are tagged on github and also released as gems on github and rubyforge. Master is pushed to whenever I add a patch or a new feature. To build from master, you can clone the code, generate the updated gemspec, build the gem and install.
|
6
|
+
|
7
|
+
* rake gemspec
|
8
|
+
* gem build mongo_mapper.gemspec
|
9
|
+
* gem install the gem that was built
|
10
|
+
|
11
|
+
== Note on Patches/Pull Requests
|
12
|
+
|
13
|
+
* Fork the project.
|
14
|
+
* Make your feature addition or bug fix.
|
15
|
+
* Add tests for it. This is important so I don't break it in a future version unintentionally.
|
16
|
+
* Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself in another branch so I can ignore when I pull)
|
17
|
+
* Send me a pull request. Bonus points for topic branches.
|
18
|
+
|
19
|
+
== Dependencies
|
20
|
+
|
21
|
+
* ActiveSupport (activesupport)
|
22
|
+
* Mongo Ruby Driver (mongodb-mongo)
|
23
|
+
* My fork of the validatable gem (jnunemaker-validatable)
|
24
|
+
|
25
|
+
== Documentation
|
26
|
+
|
27
|
+
http://rdoc.info/projects/jnunemaker/mongomapper
|
28
|
+
|
29
|
+
== More Info
|
30
|
+
|
31
|
+
You can learn more about mongo here:
|
32
|
+
http://www.mongodb.org/
|
33
|
+
|
34
|
+
You can learn more about the mongo ruby driver here:
|
35
|
+
http://github.com/mongodb/mongo-ruby-driver/tree/master
|
36
|
+
|
37
|
+
== Copyright
|
38
|
+
|
39
|
+
Copyright (c) 2009 John Nunemaker. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "djsun-mongo_mapper"
|
8
|
+
gem.summary = %Q{Awesome gem for modeling your domain and storing it in mongo}
|
9
|
+
gem.email = "nunemaker@gmail.com"
|
10
|
+
gem.homepage = "http://github.com/jnunemaker/mongomapper"
|
11
|
+
gem.authors = ["John Nunemaker"]
|
12
|
+
gem.rubyforge_project = "mongomapper"
|
13
|
+
|
14
|
+
gem.add_dependency('activesupport')
|
15
|
+
gem.add_dependency('mongo', '0.15')
|
16
|
+
gem.add_dependency('jnunemaker-validatable', '1.7.3')
|
17
|
+
|
18
|
+
gem.add_development_dependency('mocha', '0.9.4')
|
19
|
+
gem.add_development_dependency('jnunemaker-matchy', '0.4.0')
|
20
|
+
end
|
21
|
+
|
22
|
+
Jeweler::RubyforgeTasks.new do |rubyforge|
|
23
|
+
rubyforge.doc_task = "rdoc"
|
24
|
+
end
|
25
|
+
rescue LoadError
|
26
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
27
|
+
end
|
28
|
+
|
29
|
+
require 'rake/testtask'
|
30
|
+
Rake::TestTask.new(:test) do |test|
|
31
|
+
test.libs << 'lib' << 'test'
|
32
|
+
test.pattern = 'test/**/test_*.rb'
|
33
|
+
test.verbose = true
|
34
|
+
end
|
35
|
+
|
36
|
+
namespace :test do
|
37
|
+
Rake::TestTask.new(:units) do |test|
|
38
|
+
test.libs << 'lib' << 'test'
|
39
|
+
test.pattern = 'test/unit/**/test_*.rb'
|
40
|
+
test.verbose = true
|
41
|
+
end
|
42
|
+
|
43
|
+
Rake::TestTask.new(:functionals) do |test|
|
44
|
+
test.libs << 'lib' << 'test'
|
45
|
+
test.pattern = 'test/functional/**/test_*.rb'
|
46
|
+
test.verbose = true
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
begin
|
51
|
+
require 'rcov/rcovtask'
|
52
|
+
Rcov::RcovTask.new do |test|
|
53
|
+
test.libs << 'test'
|
54
|
+
test.pattern = 'test/**/test_*.rb'
|
55
|
+
test.verbose = true
|
56
|
+
end
|
57
|
+
rescue LoadError
|
58
|
+
task :rcov do
|
59
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
begin
|
64
|
+
require 'cucumber/rake/task'
|
65
|
+
Cucumber::Rake::Task.new(:features)
|
66
|
+
rescue LoadError
|
67
|
+
task :features do
|
68
|
+
abort "Cucumber is not available. In order to run features, you must: sudo gem install cucumber"
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
task :default => :test
|
73
|
+
|
74
|
+
require 'rake/rdoctask'
|
75
|
+
Rake::RDocTask.new do |rdoc|
|
76
|
+
if File.exist?('VERSION.yml')
|
77
|
+
config = YAML.load(File.read('VERSION.yml'))
|
78
|
+
version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
|
79
|
+
else
|
80
|
+
version = ""
|
81
|
+
end
|
82
|
+
|
83
|
+
rdoc.rdoc_dir = 'rdoc'
|
84
|
+
rdoc.title = "MongoMapper #{version}"
|
85
|
+
rdoc.rdoc_files.include('README*')
|
86
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
87
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.5.0.1
|
data/bin/mmconsole
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
$:.unshift File.dirname(__FILE__)+"/../lib"
|
3
|
+
|
4
|
+
require 'mongo_mapper'
|
5
|
+
require 'irb'
|
6
|
+
|
7
|
+
IRB.setup(nil)
|
8
|
+
irb = IRB::Irb.new
|
9
|
+
|
10
|
+
IRB.conf[:MAIN_CONTEXT] = irb.context
|
11
|
+
|
12
|
+
irb.context.evaluate("require 'irb/completion'", 0)
|
13
|
+
irb.context.evaluate(%@
|
14
|
+
include MongoMapper
|
15
|
+
|
16
|
+
MongoMapper.database = "mmtest"
|
17
|
+
$db = MongoMapper.database
|
18
|
+
|
19
|
+
@, 0)
|
20
|
+
|
21
|
+
puts %@
|
22
|
+
Welcome to the MongoMapper Console!
|
23
|
+
|
24
|
+
Example 1:
|
25
|
+
things = $db.collection("things")
|
26
|
+
things.insert("name" => "Raw Thing")
|
27
|
+
things.insert("name" => "Another Thing", "date" => Time.now)
|
28
|
+
|
29
|
+
cursor = things.find("name" => "Raw Thing")
|
30
|
+
puts cursor.next_object.inspect
|
31
|
+
|
32
|
+
Example 2:
|
33
|
+
class Thing
|
34
|
+
include MongoMapper::Document
|
35
|
+
key :name, String, :required => true
|
36
|
+
key :date, Time
|
37
|
+
end
|
38
|
+
|
39
|
+
thing = Thing.new
|
40
|
+
thing.name = "My thing"
|
41
|
+
thing.date = Time.now
|
42
|
+
thing.save
|
43
|
+
|
44
|
+
all_things = Thing.all
|
45
|
+
puts all_things.map { |object| object.name }.inspect
|
46
|
+
|
47
|
+
@
|
48
|
+
|
49
|
+
trap("SIGINT") do
|
50
|
+
irb.signal_handle
|
51
|
+
end
|
52
|
+
catch(:IRB_EXIT) do
|
53
|
+
irb.eval_input
|
54
|
+
end
|
55
|
+
|
@@ -0,0 +1,83 @@
|
|
1
|
+
module MongoMapper
|
2
|
+
module Associations
|
3
|
+
class Base
|
4
|
+
attr_reader :type, :name, :options
|
5
|
+
|
6
|
+
def initialize(type, name, options = {})
|
7
|
+
@type, @name, @options = type, name, options
|
8
|
+
end
|
9
|
+
|
10
|
+
def class_name
|
11
|
+
@class_name ||= begin
|
12
|
+
if cn = options[:class_name]
|
13
|
+
cn
|
14
|
+
elsif many?
|
15
|
+
name.to_s.singularize.camelize
|
16
|
+
else
|
17
|
+
name.to_s.camelize
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def klass
|
23
|
+
@klass ||= class_name.constantize
|
24
|
+
end
|
25
|
+
|
26
|
+
def many?
|
27
|
+
@many_type ||= @type == :many
|
28
|
+
end
|
29
|
+
|
30
|
+
def belongs_to?
|
31
|
+
@belongs_to_type ||= @type == :belongs_to
|
32
|
+
end
|
33
|
+
|
34
|
+
def polymorphic?
|
35
|
+
!!@options[:polymorphic]
|
36
|
+
end
|
37
|
+
|
38
|
+
def as?
|
39
|
+
!!@options[:as]
|
40
|
+
end
|
41
|
+
|
42
|
+
def type_key_name
|
43
|
+
@type_key_name ||= many? ? '_type' : "#{as}_type"
|
44
|
+
end
|
45
|
+
|
46
|
+
def as
|
47
|
+
@options[:as] || self.name
|
48
|
+
end
|
49
|
+
|
50
|
+
def foreign_key
|
51
|
+
@options[:foreign_key] || "#{name}_id"
|
52
|
+
end
|
53
|
+
|
54
|
+
def ivar
|
55
|
+
@ivar ||= "@_#{name}"
|
56
|
+
end
|
57
|
+
|
58
|
+
def embeddable?
|
59
|
+
many? && klass.embeddable?
|
60
|
+
end
|
61
|
+
|
62
|
+
def proxy_class
|
63
|
+
@proxy_class ||= begin
|
64
|
+
if many?
|
65
|
+
if self.klass.embeddable?
|
66
|
+
polymorphic? ? ManyEmbeddedPolymorphicProxy : ManyEmbeddedProxy
|
67
|
+
else
|
68
|
+
if polymorphic?
|
69
|
+
ManyPolymorphicProxy
|
70
|
+
elsif as?
|
71
|
+
ManyDocumentsAsProxy
|
72
|
+
else
|
73
|
+
ManyProxy
|
74
|
+
end
|
75
|
+
end
|
76
|
+
else
|
77
|
+
polymorphic? ? BelongsToPolymorphicProxy : BelongsToProxy
|
78
|
+
end
|
79
|
+
end # end begin
|
80
|
+
end # end proxy_class
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module MongoMapper
|
2
|
+
module Associations
|
3
|
+
class BelongsToPolymorphicProxy < Proxy
|
4
|
+
def replace(doc)
|
5
|
+
if doc
|
6
|
+
doc.save if doc.new?
|
7
|
+
id, type = doc.id, doc.class.name
|
8
|
+
end
|
9
|
+
|
10
|
+
@owner.send("#{@association.foreign_key}=", id)
|
11
|
+
@owner.send("#{@association.type_key_name}=", type)
|
12
|
+
reset
|
13
|
+
end
|
14
|
+
|
15
|
+
protected
|
16
|
+
def find_target
|
17
|
+
if proxy_id && proxy_class
|
18
|
+
proxy_class.find_by_id(proxy_id)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def proxy_id
|
23
|
+
@proxy_id ||= @owner.send(@association.foreign_key)
|
24
|
+
end
|
25
|
+
|
26
|
+
def proxy_class
|
27
|
+
@proxy_class ||= begin
|
28
|
+
klass = @owner.send(@association.type_key_name)
|
29
|
+
klass && klass.constantize
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module MongoMapper
|
2
|
+
module Associations
|
3
|
+
class BelongsToProxy < Proxy
|
4
|
+
def replace(doc)
|
5
|
+
if doc
|
6
|
+
doc.save if doc.new?
|
7
|
+
id = doc.id
|
8
|
+
end
|
9
|
+
|
10
|
+
@owner.send("#{@association.foreign_key}=", id)
|
11
|
+
reset
|
12
|
+
end
|
13
|
+
|
14
|
+
protected
|
15
|
+
def find_target
|
16
|
+
if association_id = @owner.send(@association.foreign_key)
|
17
|
+
@association.klass.find_by_id(association_id)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module MongoMapper
|
2
|
+
module Associations
|
3
|
+
class ManyDocumentsAsProxy < ManyDocumentsProxy
|
4
|
+
protected
|
5
|
+
def scoped_conditions
|
6
|
+
{as_type_name => @owner.class.name, as_id_name => @owner.id}
|
7
|
+
end
|
8
|
+
|
9
|
+
def apply_scope(doc)
|
10
|
+
ensure_owner_saved
|
11
|
+
|
12
|
+
doc.send("#{as_type_name}=", @owner.class.name)
|
13
|
+
doc.send("#{as_id_name}=", @owner.id)
|
14
|
+
|
15
|
+
doc
|
16
|
+
end
|
17
|
+
|
18
|
+
def as_type_name
|
19
|
+
@as_type_name ||= @association.options[:as].to_s + "_type"
|
20
|
+
end
|
21
|
+
|
22
|
+
def as_id_name
|
23
|
+
@as_id_name ||= @association.options[:as].to_s + "_id"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,116 @@
|
|
1
|
+
module MongoMapper
|
2
|
+
module Associations
|
3
|
+
class ManyDocumentsProxy < Proxy
|
4
|
+
delegate :klass, :to => :@association
|
5
|
+
delegate :collection, :to => :klass
|
6
|
+
|
7
|
+
include MongoMapper::Finders
|
8
|
+
|
9
|
+
def find(*args)
|
10
|
+
options = args.extract_options!
|
11
|
+
klass.find(*args << scoped_options(options))
|
12
|
+
end
|
13
|
+
|
14
|
+
def paginate(options)
|
15
|
+
klass.paginate(scoped_options(options))
|
16
|
+
end
|
17
|
+
|
18
|
+
def all(options={})
|
19
|
+
find(:all, scoped_options(options))
|
20
|
+
end
|
21
|
+
|
22
|
+
def first(options={})
|
23
|
+
find(:first, scoped_options(options))
|
24
|
+
end
|
25
|
+
|
26
|
+
def last(options={})
|
27
|
+
find(:last, scoped_options(options))
|
28
|
+
end
|
29
|
+
|
30
|
+
def count(conditions={})
|
31
|
+
klass.count(conditions.deep_merge(scoped_conditions))
|
32
|
+
end
|
33
|
+
|
34
|
+
def replace(docs)
|
35
|
+
@target.map(&:destroy) if load_target
|
36
|
+
docs.each { |doc| apply_scope(doc).save }
|
37
|
+
reset
|
38
|
+
end
|
39
|
+
|
40
|
+
def <<(*docs)
|
41
|
+
ensure_owner_saved
|
42
|
+
flatten_deeper(docs).each { |doc| apply_scope(doc).save }
|
43
|
+
reset
|
44
|
+
end
|
45
|
+
alias_method :push, :<<
|
46
|
+
alias_method :concat, :<<
|
47
|
+
|
48
|
+
def build(attrs={})
|
49
|
+
doc = klass.new(attrs)
|
50
|
+
apply_scope(doc)
|
51
|
+
doc
|
52
|
+
end
|
53
|
+
|
54
|
+
def create(attrs={})
|
55
|
+
doc = klass.new(attrs)
|
56
|
+
apply_scope(doc).save
|
57
|
+
doc
|
58
|
+
end
|
59
|
+
|
60
|
+
def destroy_all(conditions={})
|
61
|
+
all(:conditions => conditions).map(&:destroy)
|
62
|
+
reset
|
63
|
+
end
|
64
|
+
|
65
|
+
def delete_all(conditions={})
|
66
|
+
klass.delete_all(conditions.deep_merge(scoped_conditions))
|
67
|
+
reset
|
68
|
+
end
|
69
|
+
|
70
|
+
def nullify
|
71
|
+
criteria = FinderOptions.to_mongo_criteria(scoped_conditions)
|
72
|
+
all(criteria).each do |doc|
|
73
|
+
doc.update_attributes self.foreign_key => nil
|
74
|
+
end
|
75
|
+
reset
|
76
|
+
end
|
77
|
+
|
78
|
+
def method_missing(method, *args)
|
79
|
+
finder = DynamicFinder.new(method)
|
80
|
+
|
81
|
+
if finder.found?
|
82
|
+
dynamic_find(finder, args)
|
83
|
+
else
|
84
|
+
super
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
protected
|
89
|
+
def scoped_conditions
|
90
|
+
{self.foreign_key => @owner.id}
|
91
|
+
end
|
92
|
+
|
93
|
+
def scoped_options(options)
|
94
|
+
options.deep_merge({:conditions => scoped_conditions})
|
95
|
+
end
|
96
|
+
|
97
|
+
def find_target
|
98
|
+
find(:all)
|
99
|
+
end
|
100
|
+
|
101
|
+
def ensure_owner_saved
|
102
|
+
@owner.save if @owner.new?
|
103
|
+
end
|
104
|
+
|
105
|
+
def apply_scope(doc)
|
106
|
+
ensure_owner_saved
|
107
|
+
doc.send("#{self.foreign_key}=", @owner.id)
|
108
|
+
doc
|
109
|
+
end
|
110
|
+
|
111
|
+
def foreign_key
|
112
|
+
@association.options[:foreign_key] || @owner.class.name.underscore.gsub("/", "_") + "_id"
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module MongoMapper
|
2
|
+
module Associations
|
3
|
+
class ManyEmbeddedPolymorphicProxy < Proxy
|
4
|
+
def replace(v)
|
5
|
+
@_values = v.map do |doc_or_hash|
|
6
|
+
if doc_or_hash.kind_of?(EmbeddedDocument)
|
7
|
+
doc = doc_or_hash
|
8
|
+
{@association.type_key_name => doc.class.name}.merge(doc.attributes)
|
9
|
+
else
|
10
|
+
doc_or_hash
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
reset
|
15
|
+
end
|
16
|
+
|
17
|
+
protected
|
18
|
+
def find_target
|
19
|
+
(@_values || []).map do |hash|
|
20
|
+
polymorphic_class(hash).new(hash)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def polymorphic_class(doc)
|
25
|
+
if class_name = doc[@association.type_key_name]
|
26
|
+
class_name.constantize
|
27
|
+
else
|
28
|
+
@association.klass
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
module MongoMapper
|
2
|
+
module Associations
|
3
|
+
class ManyEmbeddedProxy < Proxy
|
4
|
+
def replace(v)
|
5
|
+
@_values = v.map { |e| e.kind_of?(EmbeddedDocument) ? e.attributes : e }
|
6
|
+
reset
|
7
|
+
end
|
8
|
+
|
9
|
+
def build(opts={})
|
10
|
+
owner = @owner
|
11
|
+
child = @association.klass.new(opts)
|
12
|
+
assign_parent_reference(child)
|
13
|
+
child._root_document = owner
|
14
|
+
self << child
|
15
|
+
child
|
16
|
+
end
|
17
|
+
|
18
|
+
def find(opts)
|
19
|
+
case opts
|
20
|
+
when :all
|
21
|
+
self
|
22
|
+
when String
|
23
|
+
if load_target
|
24
|
+
child = @target.detect {|item| item.id == opts}
|
25
|
+
assign_parent_reference(child)
|
26
|
+
child
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def <<(*docs)
|
32
|
+
if load_target
|
33
|
+
root = @owner._root_document || @owner
|
34
|
+
docs.each do |doc|
|
35
|
+
doc._root_document = root
|
36
|
+
@target << doc
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
alias_method :push, :<<
|
41
|
+
alias_method :concat, :<<
|
42
|
+
|
43
|
+
protected
|
44
|
+
def find_target
|
45
|
+
(@_values || []).map do |e|
|
46
|
+
child = @association.klass.new(e)
|
47
|
+
assign_parent_reference(child)
|
48
|
+
child
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
private
|
53
|
+
|
54
|
+
def assign_parent_reference(child)
|
55
|
+
return unless child && @owner
|
56
|
+
return if @owner.class.name.blank?
|
57
|
+
owner = @owner
|
58
|
+
child.class_eval do
|
59
|
+
define_method(owner.class.name.underscore) do
|
60
|
+
owner
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
module MongoMapper
|
2
|
+
module Associations
|
3
|
+
class Proxy < BasicObject
|
4
|
+
attr_reader :owner, :association
|
5
|
+
|
6
|
+
def initialize(owner, association)
|
7
|
+
@owner = owner
|
8
|
+
@association = association
|
9
|
+
reset
|
10
|
+
end
|
11
|
+
|
12
|
+
def respond_to?(*methods)
|
13
|
+
(load_target && @target.respond_to?(*methods))
|
14
|
+
end
|
15
|
+
|
16
|
+
def reset
|
17
|
+
@target = nil
|
18
|
+
end
|
19
|
+
|
20
|
+
def reload_target
|
21
|
+
reset
|
22
|
+
load_target
|
23
|
+
self
|
24
|
+
end
|
25
|
+
|
26
|
+
def send(method, *args)
|
27
|
+
return super if methods.include?(method.to_s)
|
28
|
+
load_target
|
29
|
+
@target.send(method, *args)
|
30
|
+
end
|
31
|
+
|
32
|
+
def replace(v)
|
33
|
+
raise NotImplementedError
|
34
|
+
end
|
35
|
+
|
36
|
+
def inspect
|
37
|
+
load_target
|
38
|
+
@target.inspect
|
39
|
+
end
|
40
|
+
|
41
|
+
def nil?
|
42
|
+
load_target
|
43
|
+
@target.nil?
|
44
|
+
end
|
45
|
+
|
46
|
+
protected
|
47
|
+
def method_missing(method, *args)
|
48
|
+
if load_target
|
49
|
+
if block_given?
|
50
|
+
@target.send(method, *args) { |*block_args| yield(*block_args) }
|
51
|
+
else
|
52
|
+
@target.send(method, *args)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def load_target
|
58
|
+
@target ||= find_target
|
59
|
+
end
|
60
|
+
|
61
|
+
def find_target
|
62
|
+
raise NotImplementedError
|
63
|
+
end
|
64
|
+
|
65
|
+
# Array#flatten has problems with recursive arrays. Going one level
|
66
|
+
# deeper solves the majority of the problems.
|
67
|
+
def flatten_deeper(array)
|
68
|
+
array.collect do |element|
|
69
|
+
(element.respond_to?(:flatten) && !element.is_a?(Hash)) ? element.flatten : element
|
70
|
+
end.flatten
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|