mongoid 2.3.5 → 2.4.0
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/CHANGELOG.md +34 -176
- data/LICENSE +1 -1
- data/lib/config/locales/bg.yml +6 -0
- data/lib/config/locales/de.yml +6 -0
- data/lib/config/locales/en-GB.yml +8 -0
- data/lib/config/locales/en.yml +8 -0
- data/lib/config/locales/es.yml +9 -3
- data/lib/config/locales/fr.yml +6 -0
- data/lib/config/locales/hi.yml +6 -0
- data/lib/config/locales/hu.yml +6 -0
- data/lib/config/locales/id.yml +6 -0
- data/lib/config/locales/it.yml +6 -0
- data/lib/config/locales/ja.yml +6 -0
- data/lib/config/locales/kr.yml +6 -0
- data/lib/config/locales/nl.yml +8 -0
- data/lib/config/locales/pl.yml +6 -0
- data/lib/config/locales/pt-BR.yml +6 -0
- data/lib/config/locales/pt.yml +8 -2
- data/lib/config/locales/ro.yml +6 -0
- data/lib/config/locales/ru.yml +6 -0
- data/lib/config/locales/sv.yml +6 -0
- data/lib/config/locales/vi.yml +14 -8
- data/lib/config/locales/zh-CN.yml +6 -0
- data/lib/mongoid/atomic.rb +62 -13
- data/lib/mongoid/atomic/modifiers.rb +33 -1
- data/lib/mongoid/attributes.rb +5 -19
- data/lib/mongoid/callbacks.rb +2 -1
- data/lib/mongoid/collection.rb +2 -2
- data/lib/mongoid/collections/retry.rb +18 -6
- data/lib/mongoid/components.rb +2 -0
- data/lib/mongoid/config.rb +8 -63
- data/lib/mongoid/config/environment.rb +41 -0
- data/lib/mongoid/config/options.rb +74 -0
- data/lib/mongoid/contexts/enumerable.rb +0 -24
- data/lib/mongoid/contexts/mongo.rb +33 -3
- data/lib/mongoid/copyable.rb +1 -1
- data/lib/mongoid/criteria.rb +4 -2
- data/lib/mongoid/criterion/inclusion.rb +1 -16
- data/lib/mongoid/criterion/optional.rb +37 -10
- data/lib/mongoid/criterion/scoping.rb +83 -0
- data/lib/mongoid/criterion/selector.rb +9 -6
- data/lib/mongoid/default_scope.rb +1 -1
- data/lib/mongoid/dirty.rb +163 -29
- data/lib/mongoid/document.rb +58 -7
- data/lib/mongoid/errors.rb +2 -0
- data/lib/mongoid/errors/no_environment.rb +19 -0
- data/lib/mongoid/errors/scope_overwrite.rb +21 -0
- data/lib/mongoid/extensions.rb +6 -0
- data/lib/mongoid/extensions/array/deep_copy.rb +25 -0
- data/lib/mongoid/extensions/hash/deep_copy.rb +25 -0
- data/lib/mongoid/extensions/hash/scoping.rb +1 -1
- data/lib/mongoid/extensions/object/deep_copy.rb +21 -0
- data/lib/mongoid/extensions/proc/scoping.rb +2 -2
- data/lib/mongoid/extensions/symbol/inflections.rb +1 -0
- data/lib/mongoid/fields.rb +171 -104
- data/lib/mongoid/fields/{serializable → internal}/array.rb +33 -1
- data/lib/mongoid/fields/{serializable → internal}/big_decimal.rb +16 -1
- data/lib/mongoid/fields/{serializable → internal}/bignum.rb +1 -1
- data/lib/mongoid/fields/{serializable → internal}/binary.rb +1 -1
- data/lib/mongoid/fields/{serializable → internal}/boolean.rb +16 -1
- data/lib/mongoid/fields/{serializable → internal}/date.rb +1 -1
- data/lib/mongoid/fields/{serializable → internal}/date_time.rb +1 -1
- data/lib/mongoid/fields/{serializable → internal}/fixnum.rb +1 -1
- data/lib/mongoid/fields/{serializable → internal}/float.rb +16 -1
- data/lib/mongoid/fields/internal/foreign_keys/array.rb +74 -0
- data/lib/mongoid/fields/{serializable → internal}/foreign_keys/object.rb +11 -2
- data/lib/mongoid/fields/{serializable → internal}/hash.rb +1 -1
- data/lib/mongoid/fields/{serializable → internal}/integer.rb +16 -1
- data/lib/mongoid/fields/{serializable → internal}/localized.rb +23 -2
- data/lib/mongoid/fields/{serializable → internal}/nil_class.rb +16 -1
- data/lib/mongoid/fields/{serializable → internal}/object.rb +1 -1
- data/lib/mongoid/fields/{serializable → internal}/object_id.rb +16 -1
- data/lib/mongoid/fields/{serializable → internal}/range.rb +21 -2
- data/lib/mongoid/fields/{serializable → internal}/set.rb +16 -1
- data/lib/mongoid/fields/{serializable → internal}/string.rb +16 -1
- data/lib/mongoid/fields/{serializable → internal}/symbol.rb +17 -1
- data/lib/mongoid/fields/{serializable → internal}/time.rb +1 -1
- data/lib/mongoid/fields/{serializable → internal}/time_with_zone.rb +1 -1
- data/lib/mongoid/fields/{serializable → internal}/timekeeping.rb +16 -1
- data/lib/mongoid/fields/mappings.rb +8 -3
- data/lib/mongoid/fields/serializable.rb +34 -3
- data/lib/mongoid/hierarchy.rb +14 -14
- data/lib/mongoid/identity_map.rb +3 -2
- data/lib/mongoid/logger.rb +1 -7
- data/lib/mongoid/named_scope.rb +16 -12
- data/lib/mongoid/observer.rb +5 -1
- data/lib/mongoid/paranoia.rb +1 -0
- data/lib/mongoid/persistence.rb +11 -4
- data/lib/mongoid/persistence/atomic.rb +4 -1
- data/lib/mongoid/persistence/atomic/add_to_set.rb +17 -1
- data/lib/mongoid/persistence/atomic/sets.rb +1 -1
- data/lib/mongoid/railties/database.rake +1 -1
- data/lib/mongoid/relations.rb +1 -3
- data/lib/mongoid/relations/auto_save.rb +1 -1
- data/lib/mongoid/relations/builders.rb +1 -1
- data/lib/mongoid/relations/builders/embedded/many.rb +2 -6
- data/lib/mongoid/relations/builders/nested_attributes/many.rb +1 -1
- data/lib/mongoid/relations/builders/nested_attributes/one.rb +1 -1
- data/lib/mongoid/relations/builders/referenced/many_to_many.rb +1 -1
- data/lib/mongoid/relations/cascading/delete.rb +1 -1
- data/lib/mongoid/relations/cyclic.rb +10 -6
- data/lib/mongoid/relations/embedded/atomic.rb +3 -3
- data/lib/mongoid/relations/embedded/many.rb +98 -20
- data/lib/mongoid/relations/macros.rb +2 -0
- data/lib/mongoid/relations/many.rb +13 -0
- data/lib/mongoid/relations/metadata.rb +3 -3
- data/lib/mongoid/relations/nested_builder.rb +4 -3
- data/lib/mongoid/relations/proxy.rb +0 -1
- data/lib/mongoid/relations/referenced/batch.rb +3 -2
- data/lib/mongoid/relations/referenced/in.rb +3 -3
- data/lib/mongoid/relations/referenced/many.rb +89 -10
- data/lib/mongoid/relations/referenced/many_to_many.rb +34 -43
- data/lib/mongoid/relations/referenced/one.rb +8 -4
- data/lib/mongoid/relations/synchronization.rb +22 -5
- data/lib/mongoid/threaded.rb +38 -276
- data/lib/mongoid/threaded/lifecycle.rb +18 -18
- data/lib/mongoid/timestamps/updated.rb +13 -3
- data/lib/mongoid/validations.rb +22 -1
- data/lib/mongoid/validations/presence.rb +40 -0
- data/lib/mongoid/validations/uniqueness.rb +14 -3
- data/lib/mongoid/version.rb +1 -1
- data/lib/mongoid/versioning.rb +6 -2
- data/lib/rails/mongoid.rb +7 -1
- metadata +64 -45
- data/lib/mongoid/fields/serializable/foreign_keys/array.rb +0 -42
- data/lib/mongoid/relations/embedded/sort.rb +0 -31
|
@@ -6,7 +6,7 @@ module Mongoid #:nodoc:
|
|
|
6
6
|
# mongoid criteria selectors.
|
|
7
7
|
class Selector < Hash
|
|
8
8
|
|
|
9
|
-
attr_reader :fields, :klass
|
|
9
|
+
attr_reader :aliased_fields, :fields, :klass
|
|
10
10
|
|
|
11
11
|
# Create the new selector.
|
|
12
12
|
#
|
|
@@ -17,7 +17,8 @@ module Mongoid #:nodoc:
|
|
|
17
17
|
#
|
|
18
18
|
# @since 1.0.0
|
|
19
19
|
def initialize(klass)
|
|
20
|
-
@fields, @klass =
|
|
20
|
+
@aliased_fields, @fields, @klass =
|
|
21
|
+
klass.aliased_fields, klass.fields.except("_id", "_type"), klass
|
|
21
22
|
end
|
|
22
23
|
|
|
23
24
|
# Set the value for the supplied key, attempting to typecast the value.
|
|
@@ -84,8 +85,10 @@ module Mongoid #:nodoc:
|
|
|
84
85
|
# @since 1.0.0
|
|
85
86
|
def try_to_typecast(key, value)
|
|
86
87
|
access = key.to_s
|
|
87
|
-
|
|
88
|
-
|
|
88
|
+
if !fields.has_key?(access) && !aliased_fields.has_key?(access)
|
|
89
|
+
return value
|
|
90
|
+
end
|
|
91
|
+
field = fields[access] || fields[aliased_fields[access]]
|
|
89
92
|
typecast_value_for(field, value)
|
|
90
93
|
end
|
|
91
94
|
|
|
@@ -101,7 +104,7 @@ module Mongoid #:nodoc:
|
|
|
101
104
|
#
|
|
102
105
|
# @since 1.0.0
|
|
103
106
|
def typecast_value_for(field, value)
|
|
104
|
-
return field.
|
|
107
|
+
return field.selection(value) if field.type === value
|
|
105
108
|
case value
|
|
106
109
|
when Hash
|
|
107
110
|
value = value.dup
|
|
@@ -121,7 +124,7 @@ module Mongoid #:nodoc:
|
|
|
121
124
|
if field.type == Array
|
|
122
125
|
Serialization.mongoize(value, value.class)
|
|
123
126
|
else
|
|
124
|
-
field.
|
|
127
|
+
field.selection(value)
|
|
125
128
|
end
|
|
126
129
|
end
|
|
127
130
|
end
|
data/lib/mongoid/dirty.rb
CHANGED
|
@@ -2,7 +2,58 @@
|
|
|
2
2
|
module Mongoid #:nodoc:
|
|
3
3
|
module Dirty #:nodoc:
|
|
4
4
|
extend ActiveSupport::Concern
|
|
5
|
-
|
|
5
|
+
|
|
6
|
+
# Get the changed attributes for the document.
|
|
7
|
+
#
|
|
8
|
+
# @example Get the changed attributes.
|
|
9
|
+
# model.changed
|
|
10
|
+
#
|
|
11
|
+
# @return [ Array<String> ] The changed attributes.
|
|
12
|
+
#
|
|
13
|
+
# @since 2.4.0
|
|
14
|
+
def changed
|
|
15
|
+
changed_attributes.keys
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# Has the document changed?
|
|
19
|
+
#
|
|
20
|
+
# @example Has the document changed?
|
|
21
|
+
# model.changed?
|
|
22
|
+
#
|
|
23
|
+
# @return [ true, false ] If the document is changed.
|
|
24
|
+
#
|
|
25
|
+
# @since 2.4.0
|
|
26
|
+
def changed?
|
|
27
|
+
changed_attributes.any?
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# Get the attribute changes.
|
|
31
|
+
#
|
|
32
|
+
# @example Get the attribute changes.
|
|
33
|
+
# model.changed_attributes
|
|
34
|
+
#
|
|
35
|
+
# @return [ Hash<String, Object> ] The attribute changes.
|
|
36
|
+
#
|
|
37
|
+
# @since 2.4.0
|
|
38
|
+
def changed_attributes
|
|
39
|
+
@changed_attributes ||= {}
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# Get all the changes for the document.
|
|
43
|
+
#
|
|
44
|
+
# @example Get all the changes.
|
|
45
|
+
# model.changes
|
|
46
|
+
#
|
|
47
|
+
# @return [ Hash<String, Array<Object, Object> ] The changes.
|
|
48
|
+
#
|
|
49
|
+
# @since 2.4.0
|
|
50
|
+
def changes
|
|
51
|
+
changed.inject({}.with_indifferent_access) do |changes, attr|
|
|
52
|
+
changes.tap do |hash|
|
|
53
|
+
hash[attr] = attribute_change(attr)
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
6
57
|
|
|
7
58
|
# Call this method after save, so the changes can be properly switched.
|
|
8
59
|
#
|
|
@@ -15,14 +66,25 @@ module Mongoid #:nodoc:
|
|
|
15
66
|
# @since 2.1.0
|
|
16
67
|
def move_changes
|
|
17
68
|
@_children = nil
|
|
18
|
-
@
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
delayed_atomic_pulls.clear
|
|
69
|
+
@previous_changes = changes
|
|
70
|
+
Atomic::UPDATES.each do |update|
|
|
71
|
+
send(update).clear
|
|
72
|
+
end
|
|
23
73
|
changed_attributes.clear
|
|
24
74
|
end
|
|
25
75
|
|
|
76
|
+
# Get the previous changes on the document.
|
|
77
|
+
#
|
|
78
|
+
# @example Get the previous changes.
|
|
79
|
+
# model.previous_changes
|
|
80
|
+
#
|
|
81
|
+
# @return [ Hash<String, Array<Object, Object> ] The previous changes.
|
|
82
|
+
#
|
|
83
|
+
# @since 2.4.0
|
|
84
|
+
def previous_changes
|
|
85
|
+
@previous_changes
|
|
86
|
+
end
|
|
87
|
+
|
|
26
88
|
# Remove a change from the dirty attributes hash. Used by the single field
|
|
27
89
|
# atomic updators.
|
|
28
90
|
#
|
|
@@ -39,6 +101,8 @@ module Mongoid #:nodoc:
|
|
|
39
101
|
# Gets all the new values for each of the changed fields, to be passed to
|
|
40
102
|
# a MongoDB $set modifier.
|
|
41
103
|
#
|
|
104
|
+
# @todo: Durran: Refactor 3.0
|
|
105
|
+
#
|
|
42
106
|
# @example Get the setters for the atomic updates.
|
|
43
107
|
# person = Person.new(:title => "Sir")
|
|
44
108
|
# person.title = "Madam"
|
|
@@ -47,10 +111,17 @@ module Mongoid #:nodoc:
|
|
|
47
111
|
# @return [ Hash ] A +Hash+ of atomic setters.
|
|
48
112
|
def setters
|
|
49
113
|
{}.tap do |modifications|
|
|
50
|
-
changes.each_pair do |
|
|
114
|
+
changes.each_pair do |name, changes|
|
|
51
115
|
if changes
|
|
52
|
-
|
|
53
|
-
|
|
116
|
+
old, new = changes
|
|
117
|
+
field = fields[name]
|
|
118
|
+
key = embedded? ? "#{atomic_position}.#{name}" : name
|
|
119
|
+
if field && field.resizable?
|
|
120
|
+
pushes, pulls = new - (old || []), (old || []) - new
|
|
121
|
+
field.add_atomic_changes(self, key, modifications, pushes, pulls)
|
|
122
|
+
else
|
|
123
|
+
modifications[key] = new
|
|
124
|
+
end
|
|
54
125
|
end
|
|
55
126
|
end
|
|
56
127
|
end
|
|
@@ -58,15 +129,14 @@ module Mongoid #:nodoc:
|
|
|
58
129
|
|
|
59
130
|
private
|
|
60
131
|
|
|
61
|
-
# Get the
|
|
132
|
+
# Get the old and new value for the provided attribute.
|
|
62
133
|
#
|
|
63
|
-
# @
|
|
64
|
-
#
|
|
65
|
-
#
|
|
66
|
-
#
|
|
67
|
-
# it must be persisted as a UTC Time.
|
|
134
|
+
# @example Get the attribute change.
|
|
135
|
+
# model.attribute_change("name")
|
|
136
|
+
#
|
|
137
|
+
# @param [ String ] attr The name of the attribute.
|
|
68
138
|
#
|
|
69
|
-
# @return [ Object ] The
|
|
139
|
+
# @return [ Array<Object> ] The old and new values.
|
|
70
140
|
#
|
|
71
141
|
# @since 2.1.0
|
|
72
142
|
def attribute_change(attr)
|
|
@@ -75,8 +145,8 @@ module Mongoid #:nodoc:
|
|
|
75
145
|
|
|
76
146
|
# Determine if a specific attribute has changed.
|
|
77
147
|
#
|
|
78
|
-
# @
|
|
79
|
-
#
|
|
148
|
+
# @example Has the attribute changed?
|
|
149
|
+
# model.attribute_changed?("name")
|
|
80
150
|
#
|
|
81
151
|
# @param [ String ] attr The name of the attribute.
|
|
82
152
|
#
|
|
@@ -84,26 +154,90 @@ module Mongoid #:nodoc:
|
|
|
84
154
|
#
|
|
85
155
|
# @since 2.1.6
|
|
86
156
|
def attribute_changed?(attr)
|
|
87
|
-
return false unless changed_attributes.
|
|
157
|
+
return false unless changed_attributes.has_key?(attr)
|
|
88
158
|
changed_attributes[attr] != attributes[attr]
|
|
89
159
|
end
|
|
90
160
|
|
|
91
|
-
#
|
|
92
|
-
#
|
|
161
|
+
# Get the previous value for the attribute.
|
|
162
|
+
#
|
|
163
|
+
# @example Get the previous value.
|
|
164
|
+
# model.attribute_was("name")
|
|
165
|
+
#
|
|
166
|
+
# @param [ String ] attr The attribute name.
|
|
167
|
+
#
|
|
168
|
+
# @since 2.4.0
|
|
169
|
+
def attribute_was(attr)
|
|
170
|
+
attribute_changed?(attr) ? changed_attributes[attr] : attributes[attr]
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
# Flag an attribute as going to change.
|
|
93
174
|
#
|
|
94
|
-
# @example Flag
|
|
95
|
-
#
|
|
175
|
+
# @example Flag the attribute.
|
|
176
|
+
# model.attribute_will_change!("name")
|
|
96
177
|
#
|
|
97
|
-
# @param [
|
|
178
|
+
# @param [ String ] attr The name of the attribute.
|
|
98
179
|
#
|
|
99
|
-
# @return [ Object ] The value
|
|
180
|
+
# @return [ Object ] The old value.
|
|
100
181
|
#
|
|
101
182
|
# @since 2.3.0
|
|
102
183
|
def attribute_will_change!(attr)
|
|
103
|
-
unless changed_attributes.
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
184
|
+
unless changed_attributes.has_key?(attr)
|
|
185
|
+
changed_attributes[attr] = read_attribute(attr)._deep_copy
|
|
186
|
+
end
|
|
187
|
+
end
|
|
188
|
+
|
|
189
|
+
# Set the attribute back to it's old value.
|
|
190
|
+
#
|
|
191
|
+
# @example Reset the attribute.
|
|
192
|
+
# model.reset_attribute!("name")
|
|
193
|
+
#
|
|
194
|
+
# @param [ String ] attr The name of the attribute.
|
|
195
|
+
#
|
|
196
|
+
# @return [ Object ] The old value.
|
|
197
|
+
#
|
|
198
|
+
# @since 2.4.0
|
|
199
|
+
def reset_attribute!(attr)
|
|
200
|
+
attributes[attr] = changed_attributes[attr] if attribute_changed?(attr)
|
|
201
|
+
end
|
|
202
|
+
|
|
203
|
+
module ClassMethods #:nodoc:
|
|
204
|
+
|
|
205
|
+
private
|
|
206
|
+
|
|
207
|
+
# Generate all the dirty methods needed for the attribute.
|
|
208
|
+
#
|
|
209
|
+
# @example Generate the dirty methods.
|
|
210
|
+
# Model.create_dirty_methods("name")
|
|
211
|
+
#
|
|
212
|
+
# @param [ String ] name The name of the attribute.
|
|
213
|
+
#
|
|
214
|
+
# @return [ Module ] The fields module.
|
|
215
|
+
#
|
|
216
|
+
# @since 2.4.0
|
|
217
|
+
def create_dirty_methods(name)
|
|
218
|
+
generated_methods.module_eval do
|
|
219
|
+
class_eval <<-EOM
|
|
220
|
+
def #{name}_change
|
|
221
|
+
attribute_change(#{name.inspect})
|
|
222
|
+
end
|
|
223
|
+
|
|
224
|
+
def #{name}_changed?
|
|
225
|
+
attribute_changed?(#{name.inspect})
|
|
226
|
+
end
|
|
227
|
+
|
|
228
|
+
def #{name}_was
|
|
229
|
+
attribute_was(#{name.inspect})
|
|
230
|
+
end
|
|
231
|
+
|
|
232
|
+
def #{name}_will_change!
|
|
233
|
+
attribute_will_change!(#{name.inspect})
|
|
234
|
+
end
|
|
235
|
+
|
|
236
|
+
def reset_#{name}!
|
|
237
|
+
reset_attribute!(#{name.inspect})
|
|
238
|
+
end
|
|
239
|
+
EOM
|
|
240
|
+
end
|
|
107
241
|
end
|
|
108
242
|
end
|
|
109
243
|
end
|
data/lib/mongoid/document.rb
CHANGED
|
@@ -68,8 +68,7 @@ module Mongoid #:nodoc:
|
|
|
68
68
|
#
|
|
69
69
|
# @since 2.0.0
|
|
70
70
|
def freeze
|
|
71
|
-
|
|
72
|
-
self
|
|
71
|
+
tap { |doc| doc.as_document.freeze }
|
|
73
72
|
end
|
|
74
73
|
|
|
75
74
|
# Checks if the document is frozen
|
|
@@ -81,7 +80,7 @@ module Mongoid #:nodoc:
|
|
|
81
80
|
#
|
|
82
81
|
# @since 2.0.0
|
|
83
82
|
def frozen?
|
|
84
|
-
|
|
83
|
+
attributes.frozen?
|
|
85
84
|
end
|
|
86
85
|
|
|
87
86
|
# Delegates to id in order to allow two records of the same type and id to
|
|
@@ -95,7 +94,7 @@ module Mongoid #:nodoc:
|
|
|
95
94
|
#
|
|
96
95
|
# @return [ Integer ] The hash of the document's id.
|
|
97
96
|
def hash
|
|
98
|
-
|
|
97
|
+
attributes["_id"].hash
|
|
99
98
|
end
|
|
100
99
|
|
|
101
100
|
# Generate an id for this +Document+.
|
|
@@ -128,15 +127,33 @@ module Mongoid #:nodoc:
|
|
|
128
127
|
@new_record = true
|
|
129
128
|
@attributes ||= {}
|
|
130
129
|
options ||= {}
|
|
130
|
+
apply_non_proc_defaults
|
|
131
|
+
identify if using_object_ids?
|
|
131
132
|
process(attrs, options[:as] || :default, !options[:without_protection]) do
|
|
132
|
-
identify
|
|
133
|
-
apply_defaults
|
|
133
|
+
identify unless using_object_ids?
|
|
134
134
|
yield(self) if block_given?
|
|
135
135
|
end
|
|
136
|
+
apply_proc_defaults
|
|
136
137
|
run_callbacks(:initialize) { self }
|
|
137
138
|
end
|
|
138
139
|
end
|
|
139
140
|
|
|
141
|
+
# Return the key value for the document.
|
|
142
|
+
#
|
|
143
|
+
# @example Return the key.
|
|
144
|
+
# document.to_key
|
|
145
|
+
#
|
|
146
|
+
# @return [ Object ] The id of the document or nil if new.
|
|
147
|
+
#
|
|
148
|
+
# @since 2.4.0
|
|
149
|
+
def to_key
|
|
150
|
+
if destroyed?
|
|
151
|
+
[ id ]
|
|
152
|
+
else
|
|
153
|
+
persisted? ? [ id ] : nil
|
|
154
|
+
end
|
|
155
|
+
end
|
|
156
|
+
|
|
140
157
|
# Return an array with this +Document+ only in it.
|
|
141
158
|
#
|
|
142
159
|
# @example Return the document in an array.
|
|
@@ -157,6 +174,7 @@ module Mongoid #:nodoc:
|
|
|
157
174
|
# @return [ Hash ] A hash of all attributes in the hierarchy.
|
|
158
175
|
def as_document
|
|
159
176
|
attributes.tap do |attrs|
|
|
177
|
+
return attrs if frozen?
|
|
160
178
|
relations.each_pair do |name, meta|
|
|
161
179
|
if meta.embedded?
|
|
162
180
|
relation = send(name)
|
|
@@ -189,6 +207,27 @@ module Mongoid #:nodoc:
|
|
|
189
207
|
end
|
|
190
208
|
end
|
|
191
209
|
|
|
210
|
+
# Print out the cache key. This will append different values on the
|
|
211
|
+
# plural model name.
|
|
212
|
+
#
|
|
213
|
+
# If new_record? - will append /new
|
|
214
|
+
# If not - will append /id-updated_at.to_s(:number)
|
|
215
|
+
# Without updated_at - will append /id
|
|
216
|
+
#
|
|
217
|
+
# This is usually called insode a cache() block
|
|
218
|
+
#
|
|
219
|
+
# @example Returns the cache key
|
|
220
|
+
# document.cache_key
|
|
221
|
+
#
|
|
222
|
+
# @return [ String ] the string with or without updated_at
|
|
223
|
+
#
|
|
224
|
+
# @since 2.4.0
|
|
225
|
+
def cache_key
|
|
226
|
+
return "#{model_key}/new" if new_record?
|
|
227
|
+
return "#{model_key}/#{id}-#{updated_at.utc.to_s(:number)}" if updated_at
|
|
228
|
+
"#{model_key}/#{id}"
|
|
229
|
+
end
|
|
230
|
+
|
|
192
231
|
private
|
|
193
232
|
|
|
194
233
|
# Returns the logger
|
|
@@ -200,6 +239,18 @@ module Mongoid #:nodoc:
|
|
|
200
239
|
Mongoid.logger
|
|
201
240
|
end
|
|
202
241
|
|
|
242
|
+
# Get the name of the model used in caching.
|
|
243
|
+
#
|
|
244
|
+
# @example Get the model key.
|
|
245
|
+
# model.model_key
|
|
246
|
+
#
|
|
247
|
+
# @return [ String ] The model key.
|
|
248
|
+
#
|
|
249
|
+
# @since 2.4.0
|
|
250
|
+
def model_key
|
|
251
|
+
@model_cache_key ||= "#{self.class.model_name.cache_key}"
|
|
252
|
+
end
|
|
253
|
+
|
|
203
254
|
# Implement this for calls to flatten on array.
|
|
204
255
|
#
|
|
205
256
|
# @example Get the document as an array.
|
|
@@ -241,7 +292,7 @@ module Mongoid #:nodoc:
|
|
|
241
292
|
attributes = attrs || {}
|
|
242
293
|
allocate.tap do |doc|
|
|
243
294
|
doc.instance_variable_set(:@attributes, attributes)
|
|
244
|
-
doc.
|
|
295
|
+
doc.apply_defaults
|
|
245
296
|
IdentityMap.set(doc) unless _loading_revision?
|
|
246
297
|
doc.run_callbacks(:initialize) { doc }
|
|
247
298
|
end
|
data/lib/mongoid/errors.rb
CHANGED
|
@@ -11,6 +11,8 @@ require "mongoid/errors/invalid_options"
|
|
|
11
11
|
require "mongoid/errors/invalid_time"
|
|
12
12
|
require "mongoid/errors/invalid_type"
|
|
13
13
|
require "mongoid/errors/mixed_relations"
|
|
14
|
+
require "mongoid/errors/no_environment"
|
|
15
|
+
require "mongoid/errors/scope_overwrite"
|
|
14
16
|
require "mongoid/errors/too_many_nested_attribute_records"
|
|
15
17
|
require "mongoid/errors/unsaved_document"
|
|
16
18
|
require "mongoid/errors/unsupported_version"
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
module Mongoid #:nodoc
|
|
3
|
+
module Errors #:nodoc
|
|
4
|
+
|
|
5
|
+
# Raised when trying to load configuration with no RACK_ENV set
|
|
6
|
+
class NoEnvironment < MongoidError
|
|
7
|
+
|
|
8
|
+
# Create the new no environment error.
|
|
9
|
+
#
|
|
10
|
+
# @example Create the new no environment error.
|
|
11
|
+
# NoEnvironment.new
|
|
12
|
+
#
|
|
13
|
+
# @since 2.4.0
|
|
14
|
+
def initialize
|
|
15
|
+
super(translate("no_environment", {}))
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|