mongoid 1.9.5 → 2.0.0.alpha
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +6 -0
- data/.watchr +29 -0
- data/Rakefile +52 -0
- data/VERSION +1 -0
- data/caliper.yml +4 -0
- data/lib/mongoid.rb +20 -9
- data/lib/mongoid/associations.rb +100 -123
- data/lib/mongoid/associations/belongs_to_related.rb +3 -2
- data/lib/mongoid/associations/{embeds_many.rb → embed_many.rb} +29 -90
- data/lib/mongoid/associations/{embeds_one.rb → embed_one.rb} +7 -8
- data/lib/mongoid/associations/embedded_in.rb +4 -12
- data/lib/mongoid/associations/has_many_related.rb +4 -52
- data/lib/mongoid/associations/has_one_related.rb +4 -8
- data/lib/mongoid/associations/meta_data.rb +1 -2
- data/lib/mongoid/associations/options.rb +1 -6
- data/lib/mongoid/associations/proxy.rb +21 -14
- data/lib/mongoid/attributes.rb +13 -33
- data/lib/mongoid/callbacks.rb +11 -16
- data/lib/mongoid/collection.rb +3 -4
- data/lib/mongoid/collections/master.rb +2 -3
- data/lib/mongoid/collections/mimic.rb +46 -0
- data/lib/mongoid/collections/slaves.rb +2 -3
- data/lib/mongoid/commands.rb +161 -0
- data/lib/mongoid/commands/create.rb +19 -0
- data/lib/mongoid/commands/delete.rb +16 -0
- data/lib/mongoid/commands/delete_all.rb +23 -0
- data/lib/mongoid/commands/deletion.rb +18 -0
- data/lib/mongoid/commands/destroy.rb +17 -0
- data/lib/mongoid/commands/destroy_all.rb +23 -0
- data/lib/mongoid/commands/save.rb +29 -0
- data/lib/mongoid/components.rb +6 -4
- data/lib/mongoid/config.rb +15 -134
- data/lib/mongoid/contexts.rb +1 -1
- data/lib/mongoid/contexts/enumerable.rb +1 -1
- data/lib/mongoid/contexts/mongo.rb +1 -1
- data/lib/mongoid/contexts/paging.rb +2 -10
- data/lib/mongoid/criterion/inclusion.rb +0 -17
- data/lib/mongoid/criterion/optional.rb +1 -1
- data/lib/mongoid/cursor.rb +1 -0
- data/lib/mongoid/document.rb +45 -49
- data/lib/mongoid/errors.rb +1 -32
- data/lib/mongoid/extensions.rb +10 -12
- data/lib/mongoid/extensions/array/conversions.rb +6 -8
- data/lib/mongoid/extensions/big_decimal/conversions.rb +2 -2
- data/lib/mongoid/extensions/boolean/conversions.rb +2 -8
- data/lib/mongoid/extensions/date/conversions.rb +4 -13
- data/lib/mongoid/extensions/datetime/conversions.rb +6 -1
- data/lib/mongoid/extensions/float/conversions.rb +1 -5
- data/lib/mongoid/extensions/hash/assimilation.rb +3 -12
- data/lib/mongoid/extensions/hash/conversions.rb +4 -34
- data/lib/mongoid/extensions/integer/conversions.rb +1 -5
- data/lib/mongoid/extensions/nil/assimilation.rb +0 -4
- data/lib/mongoid/extensions/object/conversions.rb +2 -8
- data/lib/mongoid/extensions/objectid/conversions.rb +1 -1
- data/lib/mongoid/extensions/string/conversions.rb +1 -1
- data/lib/mongoid/extensions/symbol/inflections.rb +1 -1
- data/lib/mongoid/extensions/time/conversions.rb +18 -0
- data/lib/mongoid/factory.rb +1 -2
- data/lib/mongoid/field.rb +2 -9
- data/lib/mongoid/fields.rb +7 -11
- data/lib/mongoid/finders.rb +2 -2
- data/lib/mongoid/identity.rb +4 -4
- data/lib/mongoid/indexes.rb +7 -10
- data/lib/mongoid/memoization.rb +2 -8
- data/lib/mongoid/named_scope.rb +5 -0
- data/lib/mongoid/observable.rb +1 -1
- data/lib/mongoid/paths.rb +22 -30
- data/lib/mongoid/state.rb +21 -28
- data/lib/mongoid/timestamps.rb +1 -1
- data/lib/mongoid/validations.rb +51 -0
- data/lib/mongoid/validations/associated.rb +32 -0
- data/lib/mongoid/validations/locale/en.yml +4 -0
- data/lib/mongoid/validations/uniqueness.rb +22 -0
- data/lib/mongoid/versioning.rb +1 -2
- data/mongoid.gemspec +408 -0
- data/perf/benchmark.rb +77 -0
- data/spec/integration/mongoid/associations_spec.rb +340 -0
- data/spec/integration/mongoid/attributes_spec.rb +22 -0
- data/spec/integration/mongoid/commands_spec.rb +227 -0
- data/spec/integration/mongoid/contexts/enumerable_spec.rb +33 -0
- data/spec/integration/mongoid/criteria_spec.rb +272 -0
- data/spec/integration/mongoid/document_spec.rb +650 -0
- data/spec/integration/mongoid/extensions_spec.rb +22 -0
- data/spec/integration/mongoid/finders_spec.rb +119 -0
- data/spec/integration/mongoid/inheritance_spec.rb +137 -0
- data/spec/integration/mongoid/named_scope_spec.rb +46 -0
- data/spec/models/address.rb +39 -0
- data/spec/models/animal.rb +6 -0
- data/spec/models/callbacks.rb +18 -0
- data/spec/models/comment.rb +8 -0
- data/spec/models/country_code.rb +6 -0
- data/spec/models/employer.rb +5 -0
- data/spec/models/game.rb +7 -0
- data/spec/models/inheritance.rb +56 -0
- data/spec/models/location.rb +5 -0
- data/spec/models/mixed_drink.rb +4 -0
- data/spec/models/name.rb +13 -0
- data/spec/models/namespacing.rb +11 -0
- data/spec/models/patient.rb +4 -0
- data/spec/models/person.rb +99 -0
- data/spec/models/pet.rb +7 -0
- data/spec/models/pet_owner.rb +6 -0
- data/spec/models/phone.rb +7 -0
- data/spec/models/post.rb +15 -0
- data/spec/models/translation.rb +5 -0
- data/spec/models/vet_visit.rb +5 -0
- data/spec/spec.opts +3 -0
- data/spec/spec_helper.rb +31 -0
- data/spec/unit/mongoid/associations/belongs_to_related_spec.rb +145 -0
- data/spec/unit/mongoid/associations/embed_many_spec.rb +516 -0
- data/spec/unit/mongoid/associations/embed_one_spec.rb +282 -0
- data/spec/unit/mongoid/associations/embedded_in_spec.rb +193 -0
- data/spec/unit/mongoid/associations/has_many_related_spec.rb +418 -0
- data/spec/unit/mongoid/associations/has_one_related_spec.rb +179 -0
- data/spec/unit/mongoid/associations/meta_data_spec.rb +88 -0
- data/spec/unit/mongoid/associations/options_spec.rb +192 -0
- data/spec/unit/mongoid/associations_spec.rb +595 -0
- data/spec/unit/mongoid/attributes_spec.rb +507 -0
- data/spec/unit/mongoid/callbacks_spec.rb +55 -0
- data/spec/unit/mongoid/collection_spec.rb +187 -0
- data/spec/unit/mongoid/collections/cyclic_iterator_spec.rb +75 -0
- data/spec/unit/mongoid/collections/master_spec.rb +41 -0
- data/spec/unit/mongoid/collections/mimic_spec.rb +43 -0
- data/spec/unit/mongoid/collections/slaves_spec.rb +81 -0
- data/spec/unit/mongoid/commands/create_spec.rb +31 -0
- data/spec/unit/mongoid/commands/delete_all_spec.rb +58 -0
- data/spec/unit/mongoid/commands/delete_spec.rb +38 -0
- data/spec/unit/mongoid/commands/destroy_all_spec.rb +21 -0
- data/spec/unit/mongoid/commands/destroy_spec.rb +51 -0
- data/spec/unit/mongoid/commands/save_spec.rb +107 -0
- data/spec/unit/mongoid/commands_spec.rb +270 -0
- data/spec/unit/mongoid/config_spec.rb +172 -0
- data/spec/unit/mongoid/contexts/enumerable_spec.rb +421 -0
- data/spec/unit/mongoid/contexts/mongo_spec.rb +682 -0
- data/spec/unit/mongoid/contexts_spec.rb +25 -0
- data/spec/unit/mongoid/criteria_spec.rb +824 -0
- data/spec/unit/mongoid/criterion/complex_spec.rb +19 -0
- data/spec/unit/mongoid/criterion/exclusion_spec.rb +91 -0
- data/spec/unit/mongoid/criterion/inclusion_spec.rb +219 -0
- data/spec/unit/mongoid/criterion/optional_spec.rb +319 -0
- data/spec/unit/mongoid/cursor_spec.rb +74 -0
- data/spec/unit/mongoid/deprecation_spec.rb +24 -0
- data/spec/unit/mongoid/document_spec.rb +818 -0
- data/spec/unit/mongoid/errors_spec.rb +103 -0
- data/spec/unit/mongoid/extensions/array/accessors_spec.rb +50 -0
- data/spec/unit/mongoid/extensions/array/assimilation_spec.rb +24 -0
- data/spec/unit/mongoid/extensions/array/conversions_spec.rb +35 -0
- data/spec/unit/mongoid/extensions/array/parentization_spec.rb +20 -0
- data/spec/unit/mongoid/extensions/big_decimal/conversions_spec.rb +22 -0
- data/spec/unit/mongoid/extensions/binary/conversions_spec.rb +22 -0
- data/spec/unit/mongoid/extensions/boolean/conversions_spec.rb +49 -0
- data/spec/unit/mongoid/extensions/date/conversions_spec.rb +102 -0
- data/spec/unit/mongoid/extensions/datetime/conversions_spec.rb +67 -0
- data/spec/unit/mongoid/extensions/float/conversions_spec.rb +61 -0
- data/spec/unit/mongoid/extensions/hash/accessors_spec.rb +184 -0
- data/spec/unit/mongoid/extensions/hash/assimilation_spec.rb +46 -0
- data/spec/unit/mongoid/extensions/hash/conversions_spec.rb +21 -0
- data/spec/unit/mongoid/extensions/hash/criteria_helpers_spec.rb +17 -0
- data/spec/unit/mongoid/extensions/hash/scoping_spec.rb +14 -0
- data/spec/unit/mongoid/extensions/integer/conversions_spec.rb +61 -0
- data/spec/unit/mongoid/extensions/nil/assimilation_spec.rb +24 -0
- data/spec/unit/mongoid/extensions/object/conversions_spec.rb +57 -0
- data/spec/unit/mongoid/extensions/proc/scoping_spec.rb +34 -0
- data/spec/unit/mongoid/extensions/string/conversions_spec.rb +17 -0
- data/spec/unit/mongoid/extensions/string/inflections_spec.rb +208 -0
- data/spec/unit/mongoid/extensions/symbol/inflections_spec.rb +91 -0
- data/spec/unit/mongoid/extensions/time/conversions_spec.rb +70 -0
- data/spec/unit/mongoid/extras_spec.rb +102 -0
- data/spec/unit/mongoid/factory_spec.rb +31 -0
- data/spec/unit/mongoid/field_spec.rb +143 -0
- data/spec/unit/mongoid/fields_spec.rb +181 -0
- data/spec/unit/mongoid/finders_spec.rb +404 -0
- data/spec/unit/mongoid/identity_spec.rb +109 -0
- data/spec/unit/mongoid/indexes_spec.rb +93 -0
- data/spec/unit/mongoid/javascript_spec.rb +48 -0
- data/spec/unit/mongoid/matchers/all_spec.rb +27 -0
- data/spec/unit/mongoid/matchers/default_spec.rb +27 -0
- data/spec/unit/mongoid/matchers/exists_spec.rb +56 -0
- data/spec/unit/mongoid/matchers/gt_spec.rb +39 -0
- data/spec/unit/mongoid/matchers/gte_spec.rb +49 -0
- data/spec/unit/mongoid/matchers/in_spec.rb +27 -0
- data/spec/unit/mongoid/matchers/lt_spec.rb +39 -0
- data/spec/unit/mongoid/matchers/lte_spec.rb +49 -0
- data/spec/unit/mongoid/matchers/ne_spec.rb +27 -0
- data/spec/unit/mongoid/matchers/nin_spec.rb +27 -0
- data/spec/unit/mongoid/matchers/size_spec.rb +27 -0
- data/spec/unit/mongoid/matchers_spec.rb +329 -0
- data/spec/unit/mongoid/memoization_spec.rb +75 -0
- data/spec/unit/mongoid/named_scope_spec.rb +123 -0
- data/spec/unit/mongoid/observable_spec.rb +46 -0
- data/spec/unit/mongoid/paths_spec.rb +124 -0
- data/spec/unit/mongoid/scope_spec.rb +240 -0
- data/spec/unit/mongoid/state_spec.rb +83 -0
- data/spec/unit/mongoid/timestamps_spec.rb +25 -0
- data/spec/unit/mongoid/validations/associated_spec.rb +103 -0
- data/spec/unit/mongoid/validations/uniqueness_spec.rb +47 -0
- data/spec/unit/mongoid/validations_spec.rb +190 -0
- data/spec/unit/mongoid/versioning_spec.rb +41 -0
- data/spec/unit/mongoid_spec.rb +46 -0
- metadata +316 -110
- data/lib/mongoid/collections.rb +0 -41
- data/lib/mongoid/concern.rb +0 -31
- data/lib/mongoid/dirty.rb +0 -253
- data/lib/mongoid/extensions/time_conversions.rb +0 -35
- data/lib/mongoid/persistence.rb +0 -222
- data/lib/mongoid/persistence/command.rb +0 -39
- data/lib/mongoid/persistence/insert.rb +0 -50
- data/lib/mongoid/persistence/insert_embedded.rb +0 -38
- data/lib/mongoid/persistence/remove.rb +0 -39
- data/lib/mongoid/persistence/remove_all.rb +0 -37
- data/lib/mongoid/persistence/remove_embedded.rb +0 -50
- data/lib/mongoid/persistence/update.rb +0 -63
- data/lib/mongoid/version.rb +0 -4
data/.gitignore
ADDED
data/.watchr
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# Watchr is the preferred method to run specs automatically over rspactor for
|
2
|
+
# Mongoid. If you are using vim, you can add the file:
|
3
|
+
#
|
4
|
+
# ~/.vim/ftdetect/watchr.vim
|
5
|
+
#
|
6
|
+
# This should have only the following line in it:
|
7
|
+
#
|
8
|
+
# autocmd BufNewFile,BufRead *.watchr setf ruby
|
9
|
+
#
|
10
|
+
# This will enable vim to recognize this file as ruby code should you wish to
|
11
|
+
# edit it.
|
12
|
+
def run(cmd)
|
13
|
+
puts cmd
|
14
|
+
system cmd
|
15
|
+
end
|
16
|
+
|
17
|
+
def spec(file)
|
18
|
+
run "spec -O spec/spec.opts #{file}"
|
19
|
+
end
|
20
|
+
|
21
|
+
watch("spec/.*/*_spec\.rb") do |match|
|
22
|
+
p match[0]
|
23
|
+
spec(match[0])
|
24
|
+
end
|
25
|
+
|
26
|
+
watch("lib/(.*/.*)\.rb") do |match|
|
27
|
+
p match[1]
|
28
|
+
spec("spec/unit/#{match[1]}_spec.rb")
|
29
|
+
end
|
data/Rakefile
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
require "rubygems"
|
2
|
+
require "rake"
|
3
|
+
require "rake/rdoctask"
|
4
|
+
require "spec/rake/spectask"
|
5
|
+
|
6
|
+
begin
|
7
|
+
require "jeweler"
|
8
|
+
Jeweler::Tasks.new do |gem|
|
9
|
+
gem.name = "mongoid"
|
10
|
+
gem.summary = "ODM framework for MongoDB"
|
11
|
+
gem.email = "durran@gmail.com"
|
12
|
+
gem.homepage = "http://mongoid.org"
|
13
|
+
gem.authors = ["Durran Jordan"]
|
14
|
+
|
15
|
+
gem.add_dependency("activemodel", ">= 3.0.pre")
|
16
|
+
gem.add_dependency("will_paginate", ">= 3.0.pre")
|
17
|
+
gem.add_dependency("mongo", ">= 0.19.1")
|
18
|
+
|
19
|
+
gem.add_development_dependency("rspec", ">= 1.3.0")
|
20
|
+
gem.add_development_dependency("mocha", ">= 0.9.8")
|
21
|
+
end
|
22
|
+
Jeweler::GemcutterTasks.new
|
23
|
+
rescue LoadError
|
24
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
25
|
+
end
|
26
|
+
|
27
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
28
|
+
spec.pattern = "spec/**/*_spec.rb"
|
29
|
+
spec.spec_opts = ["--options", "spec/spec.opts"]
|
30
|
+
end
|
31
|
+
|
32
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
33
|
+
spec.libs << "lib" << "spec"
|
34
|
+
spec.pattern = "spec/**/*_spec.rb"
|
35
|
+
spec.spec_opts = ["--options", "spec/spec.opts"]
|
36
|
+
spec.rcov = true
|
37
|
+
end
|
38
|
+
|
39
|
+
Rake::RDocTask.new do |rdoc|
|
40
|
+
if File.exist?("VERSION.yml")
|
41
|
+
config = File.read("VERSION")
|
42
|
+
version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
|
43
|
+
else
|
44
|
+
version = ""
|
45
|
+
end
|
46
|
+
rdoc.rdoc_dir = "rdoc"
|
47
|
+
rdoc.title = "mongoid #{version}"
|
48
|
+
rdoc.rdoc_files.include("README*")
|
49
|
+
rdoc.rdoc_files.include("lib/**/*.rb")
|
50
|
+
end
|
51
|
+
|
52
|
+
task :default => ["spec"]
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.0.0.alpha
|
data/caliper.yml
ADDED
data/lib/mongoid.rb
CHANGED
@@ -19,29 +19,41 @@
|
|
19
19
|
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
20
20
|
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
21
21
|
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
22
|
+
require "rubygems"
|
23
|
+
|
24
|
+
gem "activemodel", ">= 3.0.pre"
|
25
|
+
gem "will_paginate", ">= 3.0.pre"
|
26
|
+
gem "mongo", ">= 0.19.1"
|
27
|
+
|
22
28
|
require "delegate"
|
23
29
|
require "singleton"
|
24
30
|
require "time"
|
25
|
-
require "validatable"
|
26
|
-
require "active_support/callbacks"
|
27
31
|
require "active_support/core_ext"
|
32
|
+
require 'active_support/json'
|
28
33
|
require "active_support/inflector"
|
29
34
|
require "active_support/time_with_zone"
|
35
|
+
require "active_model"
|
36
|
+
require "active_model/callbacks"
|
37
|
+
require "active_model/conversion"
|
38
|
+
require "active_model/deprecated_error_methods"
|
39
|
+
require "active_model/errors"
|
40
|
+
require "active_model/naming"
|
41
|
+
require "active_model/serialization"
|
42
|
+
require "active_model/translation"
|
43
|
+
require "active_model/validator"
|
44
|
+
require "active_model/validations"
|
30
45
|
require "will_paginate/collection"
|
31
46
|
require "mongo"
|
32
|
-
require "mongoid/concern"
|
33
|
-
require "mongoid/observable"
|
34
47
|
require "mongoid/associations"
|
35
48
|
require "mongoid/attributes"
|
36
49
|
require "mongoid/callbacks"
|
37
50
|
require "mongoid/collection"
|
38
|
-
require "mongoid/
|
51
|
+
require "mongoid/commands"
|
39
52
|
require "mongoid/config"
|
40
53
|
require "mongoid/contexts"
|
41
54
|
require "mongoid/criteria"
|
42
55
|
require "mongoid/cursor"
|
43
56
|
require "mongoid/deprecation"
|
44
|
-
require "mongoid/dirty"
|
45
57
|
require "mongoid/extensions"
|
46
58
|
require "mongoid/extras"
|
47
59
|
require "mongoid/errors"
|
@@ -55,19 +67,18 @@ require "mongoid/javascript"
|
|
55
67
|
require "mongoid/matchers"
|
56
68
|
require "mongoid/memoization"
|
57
69
|
require "mongoid/named_scope"
|
70
|
+
require "mongoid/observable"
|
58
71
|
require "mongoid/paths"
|
59
|
-
require "mongoid/persistence"
|
60
72
|
require "mongoid/scope"
|
61
73
|
require "mongoid/state"
|
62
74
|
require "mongoid/timestamps"
|
75
|
+
require "mongoid/validations"
|
63
76
|
require "mongoid/versioning"
|
64
77
|
require "mongoid/components"
|
65
78
|
require "mongoid/document"
|
66
79
|
|
67
80
|
module Mongoid #:nodoc
|
68
81
|
|
69
|
-
MONGODB_VERSION = "1.4.0"
|
70
|
-
|
71
82
|
class << self
|
72
83
|
|
73
84
|
# Sets the Mongoid configuration options. Best used by passing a block.
|
data/lib/mongoid/associations.rb
CHANGED
@@ -2,8 +2,8 @@
|
|
2
2
|
require "mongoid/associations/proxy"
|
3
3
|
require "mongoid/associations/belongs_to_related"
|
4
4
|
require "mongoid/associations/embedded_in"
|
5
|
-
require "mongoid/associations/
|
6
|
-
require "mongoid/associations/
|
5
|
+
require "mongoid/associations/embed_many"
|
6
|
+
require "mongoid/associations/embed_one"
|
7
7
|
require "mongoid/associations/has_many_related"
|
8
8
|
require "mongoid/associations/has_one_related"
|
9
9
|
require "mongoid/associations/options"
|
@@ -13,13 +13,9 @@ module Mongoid # :nodoc:
|
|
13
13
|
module Associations #:nodoc:
|
14
14
|
extend ActiveSupport::Concern
|
15
15
|
included do
|
16
|
-
|
17
|
-
self.embedded = false
|
18
|
-
|
16
|
+
# Associations need to inherit down the chain.
|
19
17
|
class_inheritable_accessor :associations
|
20
18
|
self.associations = {}
|
21
|
-
|
22
|
-
delegate :embedded, :embedded?, :to => "self.class"
|
23
19
|
end
|
24
20
|
|
25
21
|
module InstanceMethods
|
@@ -28,15 +24,9 @@ module Mongoid # :nodoc:
|
|
28
24
|
self.class.associations
|
29
25
|
end
|
30
26
|
|
31
|
-
#
|
32
|
-
def
|
33
|
-
|
34
|
-
end
|
35
|
-
|
36
|
-
# Update all the dirty child documents after an update.
|
37
|
-
def update_embedded(name)
|
38
|
-
association = send(name)
|
39
|
-
association.to_a.each { |doc| doc.save if doc.changed? || doc.new_record? } unless association.blank?
|
27
|
+
# Updates all the one-to-many relational associations for the name.
|
28
|
+
def update_associations(name)
|
29
|
+
send(name).each { |doc| doc.save } if new_record?
|
40
30
|
end
|
41
31
|
|
42
32
|
# Update the one-to-one relational association for the name.
|
@@ -44,46 +34,14 @@ module Mongoid # :nodoc:
|
|
44
34
|
association = send(name)
|
45
35
|
association.save if new_record? && !association.nil?
|
46
36
|
end
|
47
|
-
|
48
|
-
# Updates all the one-to-many relational associations for the name.
|
49
|
-
def update_associations(name)
|
50
|
-
send(name).each { |doc| doc.save } if new_record?
|
51
|
-
end
|
52
37
|
end
|
53
38
|
|
54
39
|
module ClassMethods
|
55
|
-
# Adds a relational association from the child Document to a Document in
|
56
|
-
# another database or collection.
|
57
|
-
#
|
58
|
-
# Options:
|
59
|
-
#
|
60
|
-
# name: A +Symbol+ that is the related class name.
|
61
|
-
#
|
62
|
-
# Example:
|
63
|
-
#
|
64
|
-
# class Game
|
65
|
-
# include Mongoid::Document
|
66
|
-
# belongs_to_related :person
|
67
|
-
# end
|
68
|
-
#
|
69
|
-
def belongs_to_related(name, options = {}, &block)
|
70
|
-
opts = optionize(name, options, fk(name, options), &block)
|
71
|
-
associate(Associations::BelongsToRelated, opts)
|
72
|
-
field(opts.foreign_key, :type => Mongoid.use_object_ids ? BSON::ObjectId : String)
|
73
|
-
index(opts.foreign_key) unless embedded?
|
74
|
-
end
|
75
40
|
|
76
|
-
#
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
# <tt>Person.embedded?</tt>
|
81
|
-
#
|
82
|
-
# Returns:
|
83
|
-
#
|
84
|
-
# <tt>true</tt> if embedded, <tt>false</tt> if not.
|
85
|
-
def embedded?
|
86
|
-
!!self.embedded
|
41
|
+
# Deprecated: Document.belongs_to will become Document.embedded_in
|
42
|
+
def belongs_to(name, options = {}, &block)
|
43
|
+
Mongoid.deprecate("Document.belongs_to will become Document.embedded_in")
|
44
|
+
embedded_in(name, options, &block)
|
87
45
|
end
|
88
46
|
|
89
47
|
# Adds the association back to the parent document. This macro is
|
@@ -99,7 +57,7 @@ module Mongoid # :nodoc:
|
|
99
57
|
#
|
100
58
|
# class Person
|
101
59
|
# include Mongoid::Document
|
102
|
-
#
|
60
|
+
# embed_many :addresses
|
103
61
|
# end
|
104
62
|
#
|
105
63
|
# class Address
|
@@ -108,47 +66,48 @@ module Mongoid # :nodoc:
|
|
108
66
|
# end
|
109
67
|
def embedded_in(name, options = {}, &block)
|
110
68
|
unless options.has_key?(:inverse_of)
|
111
|
-
raise Errors::InvalidOptions.new("Options for
|
69
|
+
raise Errors::InvalidOptions.new("Options for belongs_to association must include :inverse_of")
|
112
70
|
end
|
113
71
|
self.embedded = true
|
114
|
-
|
72
|
+
add_association(
|
73
|
+
Associations::EmbeddedIn,
|
74
|
+
Associations::Options.new(
|
75
|
+
options.merge(:name => name, :extend => block)
|
76
|
+
)
|
77
|
+
)
|
115
78
|
end
|
116
79
|
|
117
|
-
|
118
|
-
|
119
|
-
# Adds the association from a parent document to its children. The name
|
120
|
-
# of the association needs to be a pluralized form of the child class
|
121
|
-
# name.
|
80
|
+
# Adds a relational association from the child Document to a Document in
|
81
|
+
# another database or collection.
|
122
82
|
#
|
123
83
|
# Options:
|
124
84
|
#
|
125
|
-
# name: A +Symbol+ that is the
|
85
|
+
# name: A +Symbol+ that is the related class name.
|
126
86
|
#
|
127
87
|
# Example:
|
128
88
|
#
|
129
|
-
# class
|
89
|
+
# class Game
|
130
90
|
# include Mongoid::Document
|
131
|
-
#
|
91
|
+
# belongs_to_related :person
|
132
92
|
# end
|
133
93
|
#
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
unless
|
141
|
-
after_update do |document|
|
142
|
-
document.update_embedded(name)
|
143
|
-
end
|
144
|
-
end
|
94
|
+
def belongs_to_related(name, options = {}, &block)
|
95
|
+
opts = Associations::Options.new(
|
96
|
+
options.merge(:name => name, :extend => block, :foreign_key => foreign_key(name, options))
|
97
|
+
)
|
98
|
+
add_association(Associations::BelongsToRelated, opts)
|
99
|
+
field(opts.foreign_key, :type => Mongoid.use_object_ids ? Mongo::ObjectID : String)
|
100
|
+
index(opts.foreign_key) unless self.embedded
|
145
101
|
end
|
146
102
|
|
147
|
-
|
148
|
-
|
103
|
+
# Deprecated: Document.has_many will become Document.embed_many
|
104
|
+
def has_many(name, options = {}, &block)
|
105
|
+
Mongoid.deprecate("Document.has_many will become Document.embed_many")
|
106
|
+
embed_many(name, options, &block)
|
107
|
+
end
|
149
108
|
|
150
|
-
# Adds the association from a parent document to its
|
151
|
-
# of the association needs to be a
|
109
|
+
# Adds the association from a parent document to its children. The name
|
110
|
+
# of the association needs to be a pluralized form of the child class
|
152
111
|
# name.
|
153
112
|
#
|
154
113
|
# Options:
|
@@ -159,27 +118,22 @@ module Mongoid # :nodoc:
|
|
159
118
|
#
|
160
119
|
# class Person
|
161
120
|
# include Mongoid::Document
|
162
|
-
#
|
121
|
+
# embed_many :addresses
|
163
122
|
# end
|
164
123
|
#
|
165
|
-
# class
|
124
|
+
# class Address
|
166
125
|
# include Mongoid::Document
|
167
|
-
# embedded_in :person
|
126
|
+
# embedded_in :person, :inverse_of => :addresses
|
168
127
|
# end
|
169
|
-
def
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
document.update_embedded(name)
|
177
|
-
end
|
128
|
+
def embed_many(name, options = {}, &block)
|
129
|
+
add_association(
|
130
|
+
Associations::EmbedMany,
|
131
|
+
Associations::Options.new(
|
132
|
+
options.merge(:name => name, :extend => block)
|
133
|
+
)
|
134
|
+
)
|
178
135
|
end
|
179
136
|
|
180
|
-
alias :embed_one :embeds_one
|
181
|
-
alias :has_one :embeds_one
|
182
|
-
|
183
137
|
# Adds a relational association from the Document to many Documents in
|
184
138
|
# another database or collection.
|
185
139
|
#
|
@@ -195,12 +149,51 @@ module Mongoid # :nodoc:
|
|
195
149
|
# end
|
196
150
|
#
|
197
151
|
def has_many_related(name, options = {}, &block)
|
198
|
-
|
152
|
+
add_association(Associations::HasManyRelated,
|
153
|
+
Associations::Options.new(
|
154
|
+
options.merge(:name => name, :foreign_key => foreign_key(self.name, options), :extend => block)
|
155
|
+
)
|
156
|
+
)
|
199
157
|
before_save do |document|
|
200
158
|
document.update_associations(name)
|
201
159
|
end
|
202
160
|
end
|
203
161
|
|
162
|
+
# Deprecated: Document.has_one will become Document.embed_one
|
163
|
+
def has_one(name, options = {}, &block)
|
164
|
+
Mongoid.deprecate("Document.has_one will become Document.embed_one")
|
165
|
+
embed_one(name, options, &block)
|
166
|
+
end
|
167
|
+
|
168
|
+
# Adds the association from a parent document to its child. The name
|
169
|
+
# of the association needs to be a singular form of the child class
|
170
|
+
# name.
|
171
|
+
#
|
172
|
+
# Options:
|
173
|
+
#
|
174
|
+
# name: A +Symbol+ that is the plural child class name.
|
175
|
+
#
|
176
|
+
# Example:
|
177
|
+
#
|
178
|
+
# class Person
|
179
|
+
# include Mongoid::Document
|
180
|
+
# embed_one :name
|
181
|
+
# end
|
182
|
+
#
|
183
|
+
# class Name
|
184
|
+
# include Mongoid::Document
|
185
|
+
# embedded_in :person
|
186
|
+
# end
|
187
|
+
def embed_one(name, options = {}, &block)
|
188
|
+
opts = Associations::Options.new(
|
189
|
+
options.merge(:name => name, :extend => block)
|
190
|
+
)
|
191
|
+
type = Associations::EmbedOne
|
192
|
+
add_association(type, opts)
|
193
|
+
add_builder(type, opts)
|
194
|
+
add_creator(type, opts)
|
195
|
+
end
|
196
|
+
|
204
197
|
# Adds a relational association from the Document to one Document in
|
205
198
|
# another database or collection.
|
206
199
|
#
|
@@ -215,7 +208,12 @@ module Mongoid # :nodoc:
|
|
215
208
|
# has_one_related :game
|
216
209
|
# end
|
217
210
|
def has_one_related(name, options = {}, &block)
|
218
|
-
|
211
|
+
add_association(
|
212
|
+
Associations::HasOneRelated,
|
213
|
+
Associations::Options.new(
|
214
|
+
options.merge(:name => name, :foreign_key => foreign_key(name, options), :extend => block)
|
215
|
+
)
|
216
|
+
)
|
219
217
|
before_save do |document|
|
220
218
|
document.update_association(name)
|
221
219
|
end
|
@@ -240,17 +238,14 @@ module Mongoid # :nodoc:
|
|
240
238
|
# Adds the association to the associations hash with the type as the key,
|
241
239
|
# then adds the accessors for the association. The defined setters and
|
242
240
|
# getters for the associations will perform the necessary memoization.
|
243
|
-
|
244
|
-
# Example:
|
245
|
-
#
|
246
|
-
# <tt>Person.associate(EmbedsMany, { :name => :addresses })</tt>
|
247
|
-
def associate(type, options)
|
241
|
+
def add_association(type, options)
|
248
242
|
name = options.name.to_s
|
249
243
|
associations[name] = MetaData.new(type, options)
|
250
|
-
define_method(name)
|
244
|
+
define_method(name) do
|
245
|
+
memoized(name) { type.instantiate(self, options) }
|
246
|
+
end
|
251
247
|
define_method("#{name}=") do |object|
|
252
|
-
|
253
|
-
memoized(name) { type.update(object, self, options) }
|
248
|
+
reset(name) { type.update(object, self, options) }
|
254
249
|
end
|
255
250
|
end
|
256
251
|
|
@@ -269,32 +264,14 @@ module Mongoid # :nodoc:
|
|
269
264
|
name = options.name.to_s
|
270
265
|
define_method("create_#{name}") do |attrs|
|
271
266
|
document = send("build_#{name}", attrs)
|
272
|
-
document.save; document
|
267
|
+
document.run_callbacks(:create) { document.save }; document
|
273
268
|
end
|
274
269
|
end
|
275
270
|
|
276
|
-
# build the options given the params.
|
277
|
-
def optionize(name, options, foreign_key, &block)
|
278
|
-
Associations::Options.new(
|
279
|
-
options.merge(:name => name, :foreign_key => foreign_key, :extend => block)
|
280
|
-
)
|
281
|
-
end
|
282
|
-
|
283
271
|
# Find the foreign key.
|
284
|
-
def
|
272
|
+
def foreign_key(name, options)
|
285
273
|
options[:foreign_key] || name.to_s.foreign_key
|
286
274
|
end
|
287
|
-
|
288
|
-
# Build the association options.
|
289
|
-
def build_options(name, options, &block)
|
290
|
-
Associations::Options.new(
|
291
|
-
options.merge(
|
292
|
-
:name => name,
|
293
|
-
:foreign_key => foreign_key(name, options),
|
294
|
-
:extend => block
|
295
|
-
)
|
296
|
-
)
|
297
|
-
end
|
298
275
|
end
|
299
276
|
end
|
300
277
|
end
|