mongoid 2.0.0.beta.20 → 2.0.0.rc.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (134) hide show
  1. data/README.rdoc +8 -0
  2. data/Rakefile +51 -0
  3. data/lib/config/locales/nl.yml +39 -0
  4. data/lib/config/locales/ro.yml +1 -1
  5. data/lib/mongoid.rb +17 -17
  6. data/lib/mongoid/atomicity.rb +54 -22
  7. data/lib/mongoid/attributes.rb +145 -125
  8. data/lib/mongoid/callbacks.rb +7 -2
  9. data/lib/mongoid/collection.rb +49 -32
  10. data/lib/mongoid/collections.rb +0 -1
  11. data/lib/mongoid/components.rb +34 -29
  12. data/lib/mongoid/config.rb +207 -193
  13. data/lib/mongoid/config/database.rb +167 -0
  14. data/lib/mongoid/contexts.rb +2 -5
  15. data/lib/mongoid/contexts/enumerable.rb +30 -4
  16. data/lib/mongoid/contexts/ids.rb +2 -2
  17. data/lib/mongoid/contexts/mongo.rb +30 -5
  18. data/lib/mongoid/copyable.rb +44 -0
  19. data/lib/mongoid/criteria.rb +110 -56
  20. data/lib/mongoid/criterion/creational.rb +34 -0
  21. data/lib/mongoid/criterion/destructive.rb +37 -0
  22. data/lib/mongoid/criterion/exclusion.rb +3 -1
  23. data/lib/mongoid/criterion/inclusion.rb +59 -64
  24. data/lib/mongoid/criterion/inspection.rb +22 -0
  25. data/lib/mongoid/criterion/optional.rb +42 -54
  26. data/lib/mongoid/criterion/selector.rb +9 -0
  27. data/lib/mongoid/default_scope.rb +28 -0
  28. data/lib/mongoid/deprecation.rb +5 -5
  29. data/lib/mongoid/dirty.rb +4 -5
  30. data/lib/mongoid/document.rb +161 -114
  31. data/lib/mongoid/extensions.rb +7 -11
  32. data/lib/mongoid/extensions/array/parentization.rb +2 -2
  33. data/lib/mongoid/extensions/date/conversions.rb +1 -1
  34. data/lib/mongoid/extensions/hash/conversions.rb +0 -23
  35. data/lib/mongoid/extensions/nil/collectionization.rb +12 -0
  36. data/lib/mongoid/extensions/object/reflections.rb +17 -0
  37. data/lib/mongoid/extensions/object/yoda.rb +27 -0
  38. data/lib/mongoid/extensions/string/conversions.rb +23 -4
  39. data/lib/mongoid/extensions/time_conversions.rb +4 -4
  40. data/lib/mongoid/field.rb +30 -19
  41. data/lib/mongoid/fields.rb +15 -5
  42. data/lib/mongoid/finders.rb +19 -11
  43. data/lib/mongoid/hierarchy.rb +34 -28
  44. data/lib/mongoid/identity.rb +62 -20
  45. data/lib/mongoid/inspection.rb +58 -0
  46. data/lib/mongoid/matchers.rb +20 -0
  47. data/lib/mongoid/multi_database.rb +11 -0
  48. data/lib/mongoid/nested_attributes.rb +41 -0
  49. data/lib/mongoid/paranoia.rb +3 -4
  50. data/lib/mongoid/paths.rb +1 -1
  51. data/lib/mongoid/persistence.rb +89 -90
  52. data/lib/mongoid/persistence/command.rb +20 -4
  53. data/lib/mongoid/persistence/insert.rb +13 -11
  54. data/lib/mongoid/persistence/insert_embedded.rb +8 -6
  55. data/lib/mongoid/persistence/remove.rb +6 -4
  56. data/lib/mongoid/persistence/remove_all.rb +6 -4
  57. data/lib/mongoid/persistence/remove_embedded.rb +8 -6
  58. data/lib/mongoid/persistence/update.rb +12 -10
  59. data/lib/mongoid/railtie.rb +2 -2
  60. data/lib/mongoid/railties/database.rake +10 -9
  61. data/lib/mongoid/relations.rb +104 -0
  62. data/lib/mongoid/relations/accessors.rb +154 -0
  63. data/lib/mongoid/relations/auto_save.rb +34 -0
  64. data/lib/mongoid/relations/binding.rb +24 -0
  65. data/lib/mongoid/relations/bindings.rb +9 -0
  66. data/lib/mongoid/relations/bindings/embedded/in.rb +77 -0
  67. data/lib/mongoid/relations/bindings/embedded/many.rb +93 -0
  68. data/lib/mongoid/relations/bindings/embedded/one.rb +65 -0
  69. data/lib/mongoid/relations/bindings/referenced/in.rb +78 -0
  70. data/lib/mongoid/relations/bindings/referenced/many.rb +93 -0
  71. data/lib/mongoid/relations/bindings/referenced/many_to_many.rb +94 -0
  72. data/lib/mongoid/relations/bindings/referenced/one.rb +63 -0
  73. data/lib/mongoid/relations/builder.rb +41 -0
  74. data/lib/mongoid/relations/builders.rb +79 -0
  75. data/lib/mongoid/relations/builders/embedded/in.rb +25 -0
  76. data/lib/mongoid/relations/builders/embedded/many.rb +32 -0
  77. data/lib/mongoid/relations/builders/embedded/one.rb +26 -0
  78. data/lib/mongoid/relations/builders/nested_attributes/many.rb +116 -0
  79. data/lib/mongoid/relations/builders/nested_attributes/one.rb +135 -0
  80. data/lib/mongoid/relations/builders/referenced/in.rb +32 -0
  81. data/lib/mongoid/relations/builders/referenced/many.rb +26 -0
  82. data/lib/mongoid/relations/builders/referenced/many_to_many.rb +29 -0
  83. data/lib/mongoid/relations/builders/referenced/one.rb +30 -0
  84. data/lib/mongoid/relations/cascading.rb +55 -0
  85. data/lib/mongoid/relations/cascading/delete.rb +19 -0
  86. data/lib/mongoid/relations/cascading/destroy.rb +19 -0
  87. data/lib/mongoid/relations/cascading/nullify.rb +18 -0
  88. data/lib/mongoid/relations/cascading/strategy.rb +26 -0
  89. data/lib/mongoid/relations/cyclic.rb +97 -0
  90. data/lib/mongoid/relations/embedded/in.rb +172 -0
  91. data/lib/mongoid/relations/embedded/many.rb +450 -0
  92. data/lib/mongoid/relations/embedded/one.rb +169 -0
  93. data/lib/mongoid/relations/macros.rb +302 -0
  94. data/lib/mongoid/relations/many.rb +185 -0
  95. data/lib/mongoid/relations/metadata.rb +529 -0
  96. data/lib/mongoid/relations/nested_builder.rb +52 -0
  97. data/lib/mongoid/relations/one.rb +29 -0
  98. data/lib/mongoid/relations/polymorphic.rb +54 -0
  99. data/lib/mongoid/relations/proxy.rb +122 -0
  100. data/lib/mongoid/relations/referenced/in.rb +214 -0
  101. data/lib/mongoid/relations/referenced/many.rb +358 -0
  102. data/lib/mongoid/relations/referenced/many_to_many.rb +379 -0
  103. data/lib/mongoid/relations/referenced/one.rb +204 -0
  104. data/lib/mongoid/relations/reflections.rb +45 -0
  105. data/lib/mongoid/safe.rb +11 -1
  106. data/lib/mongoid/safety.rb +122 -97
  107. data/lib/mongoid/scope.rb +14 -9
  108. data/lib/mongoid/state.rb +37 -3
  109. data/lib/mongoid/timestamps.rb +11 -0
  110. data/lib/mongoid/validations.rb +42 -3
  111. data/lib/mongoid/validations/associated.rb +8 -5
  112. data/lib/mongoid/validations/uniqueness.rb +23 -2
  113. data/lib/mongoid/version.rb +1 -1
  114. data/lib/mongoid/versioning.rb +25 -16
  115. data/lib/rails/generators/mongoid/model/templates/model.rb +3 -1
  116. metadata +95 -80
  117. data/lib/mongoid/associations.rb +0 -364
  118. data/lib/mongoid/associations/embedded_in.rb +0 -74
  119. data/lib/mongoid/associations/embeds_many.rb +0 -299
  120. data/lib/mongoid/associations/embeds_one.rb +0 -111
  121. data/lib/mongoid/associations/foreign_key.rb +0 -35
  122. data/lib/mongoid/associations/meta_data.rb +0 -38
  123. data/lib/mongoid/associations/options.rb +0 -78
  124. data/lib/mongoid/associations/proxy.rb +0 -60
  125. data/lib/mongoid/associations/referenced_in.rb +0 -70
  126. data/lib/mongoid/associations/references_many.rb +0 -254
  127. data/lib/mongoid/associations/references_many_as_array.rb +0 -128
  128. data/lib/mongoid/associations/references_one.rb +0 -104
  129. data/lib/mongoid/extensions/array/accessors.rb +0 -17
  130. data/lib/mongoid/extensions/array/assimilation.rb +0 -26
  131. data/lib/mongoid/extensions/hash/accessors.rb +0 -42
  132. data/lib/mongoid/extensions/hash/assimilation.rb +0 -40
  133. data/lib/mongoid/extensions/nil/assimilation.rb +0 -17
  134. data/lib/mongoid/memoization.rb +0 -33
@@ -0,0 +1,25 @@
1
+ # encoding: utf-8
2
+ module Mongoid # :nodoc:
3
+ module Relations #:nodoc:
4
+ module Builders #:nodoc:
5
+ module Embedded #:nodoc:
6
+ class In < Builder #:nodoc:
7
+
8
+ # This builder doesn't actually build anything, just returns the
9
+ # parent since it should already be instantiated.
10
+ #
11
+ # @example Build the document.
12
+ # Builder.new(meta, attrs).build
13
+ #
14
+ # @param [ String ] type Not used in this context.
15
+ #
16
+ # @return [ Document ] A single document.
17
+ def build(type = nil)
18
+ return object unless object.is_a?(Hash)
19
+ Mongoid::Factory.build(metadata.klass, object)
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,32 @@
1
+ # encoding: utf-8
2
+ module Mongoid # :nodoc:
3
+ module Relations #:nodoc:
4
+ module Builders #:nodoc:
5
+ module Embedded #:nodoc:
6
+ class Many < Builder #:nodoc:
7
+
8
+ # Builds the document out of the attributes using the provided
9
+ # metadata on the relation. Instantiates through the factory in order
10
+ # to make sure subclasses and allocation are used if fitting. This
11
+ # case will return many documents.
12
+ #
13
+ # @example Build the documents.
14
+ # Builder.new(meta, attrs).build
15
+ #
16
+ # @param [ String ] type Not used in this context.
17
+ #
18
+ # @return [ Array<Document ] The documents.
19
+ def build(type = nil)
20
+ return [] if object.blank?
21
+ return object if object.first.is_a?(Document)
22
+ object.inject([]) do |documents, attrs|
23
+ documents.tap do |docs|
24
+ docs << Mongoid::Factory.build(metadata.klass, attrs)
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,26 @@
1
+ # encoding: utf-8
2
+ module Mongoid # :nodoc:
3
+ module Relations #:nodoc:
4
+ module Builders #:nodoc:
5
+ module Embedded #:nodoc:
6
+ class One < Builder #:nodoc:
7
+
8
+ # Builds the document out of the attributes using the provided
9
+ # metadata on the relation. Instantiates through the factory in order
10
+ # to make sure subclasses and allocation are used if fitting.
11
+ #
12
+ # @example Build the document.
13
+ # Builder.new(meta, attrs).build
14
+ #
15
+ # @param [ String ] type Not used in this context.
16
+ #
17
+ # @return [ Document ] A single document.
18
+ def build(type = nil)
19
+ return object unless object.is_a?(Hash)
20
+ Mongoid::Factory.build(metadata.klass, object)
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,116 @@
1
+ # encoding: utf-8
2
+ module Mongoid # :nodoc:
3
+ module Relations #:nodoc:
4
+ module Builders #:nodoc:
5
+ module NestedAttributes #:nodoc:
6
+ class Many < NestedBuilder
7
+
8
+ # Builds the relation depending on the attributes and the options
9
+ # passed to the macro.
10
+ #
11
+ # This attempts to perform 3 operations, either one of an update of
12
+ # the existing relation, a replacement of the relation with a new
13
+ # document, or a removal of the relation.
14
+ #
15
+ # Example:
16
+ #
17
+ # <tt>many.build(person)</tt>
18
+ #
19
+ # Options:
20
+ #
21
+ # parent: The parent document of the relation.
22
+ def build(parent)
23
+ @existing = parent.send(metadata.name)
24
+ if over_limit?(attributes)
25
+ raise Errors::TooManyNestedAttributeRecords.new(existing, options[:limit])
26
+ end
27
+ attributes.each { |attrs| process(attrs[1]) }
28
+ end
29
+
30
+ # Create the new builder for nested attributes on one-to-one
31
+ # relations.
32
+ #
33
+ # Example:
34
+ #
35
+ # <tt>One.new(metadata, attributes, options)</tt>
36
+ #
37
+ # Options:
38
+ #
39
+ # metadata: The relation metadata
40
+ # attributes: The attributes hash to attempt to set.
41
+ # options: The options defined.
42
+ #
43
+ # Returns:
44
+ #
45
+ # A new builder.
46
+ def initialize(metadata, attributes, options = {})
47
+ @attributes = attributes.with_indifferent_access.sort do |a, b|
48
+ a[0] <=> b[0]
49
+ end
50
+ @metadata = metadata
51
+ @options = options
52
+ end
53
+
54
+ private
55
+
56
+ # Can the existing relation potentially be deleted?
57
+ #
58
+ # Example:
59
+ #
60
+ # <tt>destroyable?({ :_destroy => "1" })</tt>
61
+ #
62
+ # Options:
63
+ #
64
+ # attributes: The attributes to pull the flag from.
65
+ #
66
+ # Returns:
67
+ #
68
+ # True if the relation can potentially be deleted.
69
+ def destroyable?(attributes)
70
+ destroy = attributes.delete(:_destroy)
71
+ [ 1, "1", true, "true" ].include?(destroy) && allow_destroy?
72
+ end
73
+
74
+ # Are the supplied attributes of greater number than the supplied
75
+ # limit?
76
+ #
77
+ # Example:
78
+ #
79
+ # <tt>builder.over_limit?({ "street" => "Bond" })</tt>
80
+ #
81
+ # Options:
82
+ #
83
+ # attributes: The attributes being set.
84
+ #
85
+ # Returns:
86
+ #
87
+ # True if a limit supplied and the attributes are of greater number.
88
+ def over_limit?(attributes)
89
+ limit = options[:limit]
90
+ limit ? attributes.size > limit : false
91
+ end
92
+
93
+ # Process each set of attributes one at a time for each potential
94
+ # new, existing, or ignored document.
95
+ #
96
+ # Example:
97
+ #
98
+ # <tt>builder.process({ "_id" => 1, "street" => "Bond" })
99
+ #
100
+ # Options:
101
+ #
102
+ # attrs: The single document attributes to process.
103
+ def process(attrs)
104
+ return if reject?(attrs)
105
+ if attrs[:_id]
106
+ document = existing.find(attrs[:_id])
107
+ destroyable?(attrs) ? document.destroy : document.update_attributes(attrs)
108
+ else
109
+ existing.push(metadata.klass.new(attrs)) unless destroyable?(attrs)
110
+ end
111
+ end
112
+ end
113
+ end
114
+ end
115
+ end
116
+ end
@@ -0,0 +1,135 @@
1
+ # encoding: utf-8
2
+ module Mongoid # :nodoc:
3
+ module Relations #:nodoc:
4
+ module Builders #:nodoc:
5
+ module NestedAttributes #:nodoc:
6
+ class One < NestedBuilder
7
+
8
+ attr_accessor :destroy
9
+
10
+ # Builds the relation depending on the attributes and the options
11
+ # passed to the macro.
12
+ #
13
+ # This attempts to perform 3 operations, either one of an update of
14
+ # the existing relation, a replacement of the relation with a new
15
+ # document, or a removal of the relation.
16
+ #
17
+ # Example:
18
+ #
19
+ # <tt>one.build(person)</tt>
20
+ #
21
+ # Options:
22
+ #
23
+ # parent: The parent document of the relation.
24
+ def build(parent)
25
+ return if reject?(attributes)
26
+ @existing = parent.send(metadata.name)
27
+ if update?
28
+ existing.attributes = attributes
29
+ elsif replace?
30
+ parent.send(metadata.setter, metadata.klass.new(attributes))
31
+ elsif delete?
32
+ parent.send(metadata.setter, nil)
33
+ end
34
+ end
35
+
36
+ # Create the new builder for nested attributes on one-to-one
37
+ # relations.
38
+ #
39
+ # Example:
40
+ #
41
+ # <tt>One.new(metadata, attributes, options)</tt>
42
+ #
43
+ # Options:
44
+ #
45
+ # metadata: The relation metadata
46
+ # attributes: The attributes hash to attempt to set.
47
+ # options: The options defined.
48
+ #
49
+ # Returns:
50
+ #
51
+ # A new builder.
52
+ def initialize(metadata, attributes, options)
53
+ @attributes = attributes.with_indifferent_access
54
+ @metadata = metadata
55
+ @options = options
56
+ @destroy = @attributes.delete(:_destroy)
57
+ end
58
+
59
+ private
60
+
61
+ # Is the id in the attribtues acceptable for allowing an update to
62
+ # the existing relation?
63
+ #
64
+ # Example:
65
+ #
66
+ # <tt>acceptable_id?</tt>
67
+ #
68
+ # Returns:
69
+ #
70
+ # True if the id part of the logic will allow an update.
71
+ def acceptable_id?
72
+ id = attributes[:_id]
73
+ existing.id == id || id.nil? || (existing.id != id && update_only?)
74
+ end
75
+
76
+ # Can the existing relation be deleted?
77
+ #
78
+ # Example:
79
+ #
80
+ # <tt>delete?</tt>
81
+ #
82
+ # Returns:
83
+ #
84
+ # True if the relation should be deleted.
85
+ def delete?
86
+ destroyable? && !attributes[:_id].nil?
87
+ end
88
+
89
+ # Can the existing relation potentially be deleted?
90
+ #
91
+ # Example:
92
+ #
93
+ # <tt>destroyable?({ :_destroy => "1" })</tt>
94
+ #
95
+ # Options:
96
+ #
97
+ # attributes: The attributes to pull the flag from.
98
+ #
99
+ # Returns:
100
+ #
101
+ # True if the relation can potentially be deleted.
102
+ def destroyable?
103
+ [ 1, "1", true, "true" ].include?(destroy) && allow_destroy?
104
+ end
105
+
106
+ # Is the document to be replaced?
107
+ #
108
+ # Example:
109
+ #
110
+ # <tt>replace?</tt>
111
+ #
112
+ # Returns:
113
+ #
114
+ # True if the document should be replaced.
115
+ def replace?
116
+ !existing && !destroyable? && !attributes.blank?
117
+ end
118
+
119
+ # Should the document be updated?
120
+ #
121
+ # Example:
122
+ #
123
+ # <tt>update?</tt>
124
+ #
125
+ # Returns:
126
+ #
127
+ # True if the object should have its attributes updated.
128
+ def update?
129
+ existing && !destroyable? && acceptable_id?
130
+ end
131
+ end
132
+ end
133
+ end
134
+ end
135
+ end
@@ -0,0 +1,32 @@
1
+ # encoding: utf-8
2
+ module Mongoid # :nodoc:
3
+ module Relations #:nodoc:
4
+ module Builders #:nodoc:
5
+ module Referenced #:nodoc:
6
+ class In < Builder
7
+
8
+ # This builder either takes a foreign key and queries for the
9
+ # object or a document, where it will just return it.
10
+ #
11
+ # @example Build the document.
12
+ # Builder.new(meta, attrs).build
13
+ #
14
+ # @param [ String ] type The type of document to query for.
15
+ #
16
+ # @return [ Document ] A single document.
17
+ def build(type = nil)
18
+ return object unless query?
19
+ if object.is_a?(Hash)
20
+ return Mongoid::Factory.build(metadata.klass, object)
21
+ end
22
+ begin
23
+ (type ? type.constantize : metadata.klass).find(object)
24
+ rescue Errors::DocumentNotFound
25
+ return nil
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,26 @@
1
+ # encoding: utf-8
2
+ module Mongoid # :nodoc:
3
+ module Relations #:nodoc:
4
+ module Builders #:nodoc:
5
+ module Referenced #:nodoc:
6
+ class Many < Builder
7
+
8
+ # This builder either takes a hash and queries for the
9
+ # object or an array of documents, where it will just return tem.
10
+ #
11
+ # @example Build the documents.
12
+ # Builder.new(meta, attrs).build
13
+ #
14
+ # @param [ String ] type The type of document to query for.
15
+ #
16
+ # @return [ Array<Document> ] The documents.
17
+ def build(type = nil)
18
+ return object unless query?
19
+ key = metadata.foreign_key
20
+ metadata.klass.find(:conditions => { key => object })
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,29 @@
1
+ # encoding: utf-8
2
+ module Mongoid # :nodoc:
3
+ module Relations #:nodoc:
4
+ module Builders #:nodoc:
5
+ module Referenced #:nodoc:
6
+ class ManyToMany < Builder
7
+
8
+ # This builder either takes a hash and queries for the
9
+ # object or an array of documents, where it will just return them.
10
+ #
11
+ # @example Build the documents.
12
+ # Builder.new(meta, attrs).build
13
+ #
14
+ # @param [ String ] type The type of document to query for.
15
+ #
16
+ # @return [ Array<Document> ] The documents.
17
+ def build(type = nil)
18
+ return object.dup unless query?
19
+ begin
20
+ metadata.klass.find(object)
21
+ rescue Errors::DocumentNotFound
22
+ return []
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,30 @@
1
+ # encoding: utf-8
2
+ module Mongoid # :nodoc:
3
+ module Relations #:nodoc:
4
+ module Builders #:nodoc:
5
+ module Referenced #:nodoc:
6
+ class One < Builder
7
+
8
+ # This builder either takes an _id or an object and queries for the
9
+ # inverse side using the id or sets the object.
10
+ #
11
+ # @example Build the document.
12
+ # Builder.new(meta, attrs).build
13
+ #
14
+ # @param [ String ] type The type of document to query for.
15
+ #
16
+ # @return [ Document ] A single document.
17
+ def build(type = nil)
18
+ return object unless query?
19
+ if object.is_a?(Hash)
20
+ return Mongoid::Factory.build(metadata.klass, object)
21
+ end
22
+ metadata.klass.first(
23
+ :conditions => { metadata.foreign_key => object }
24
+ )
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end