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.
Files changed (126) hide show
  1. data/CHANGELOG.md +34 -176
  2. data/LICENSE +1 -1
  3. data/lib/config/locales/bg.yml +6 -0
  4. data/lib/config/locales/de.yml +6 -0
  5. data/lib/config/locales/en-GB.yml +8 -0
  6. data/lib/config/locales/en.yml +8 -0
  7. data/lib/config/locales/es.yml +9 -3
  8. data/lib/config/locales/fr.yml +6 -0
  9. data/lib/config/locales/hi.yml +6 -0
  10. data/lib/config/locales/hu.yml +6 -0
  11. data/lib/config/locales/id.yml +6 -0
  12. data/lib/config/locales/it.yml +6 -0
  13. data/lib/config/locales/ja.yml +6 -0
  14. data/lib/config/locales/kr.yml +6 -0
  15. data/lib/config/locales/nl.yml +8 -0
  16. data/lib/config/locales/pl.yml +6 -0
  17. data/lib/config/locales/pt-BR.yml +6 -0
  18. data/lib/config/locales/pt.yml +8 -2
  19. data/lib/config/locales/ro.yml +6 -0
  20. data/lib/config/locales/ru.yml +6 -0
  21. data/lib/config/locales/sv.yml +6 -0
  22. data/lib/config/locales/vi.yml +14 -8
  23. data/lib/config/locales/zh-CN.yml +6 -0
  24. data/lib/mongoid/atomic.rb +62 -13
  25. data/lib/mongoid/atomic/modifiers.rb +33 -1
  26. data/lib/mongoid/attributes.rb +5 -19
  27. data/lib/mongoid/callbacks.rb +2 -1
  28. data/lib/mongoid/collection.rb +2 -2
  29. data/lib/mongoid/collections/retry.rb +18 -6
  30. data/lib/mongoid/components.rb +2 -0
  31. data/lib/mongoid/config.rb +8 -63
  32. data/lib/mongoid/config/environment.rb +41 -0
  33. data/lib/mongoid/config/options.rb +74 -0
  34. data/lib/mongoid/contexts/enumerable.rb +0 -24
  35. data/lib/mongoid/contexts/mongo.rb +33 -3
  36. data/lib/mongoid/copyable.rb +1 -1
  37. data/lib/mongoid/criteria.rb +4 -2
  38. data/lib/mongoid/criterion/inclusion.rb +1 -16
  39. data/lib/mongoid/criterion/optional.rb +37 -10
  40. data/lib/mongoid/criterion/scoping.rb +83 -0
  41. data/lib/mongoid/criterion/selector.rb +9 -6
  42. data/lib/mongoid/default_scope.rb +1 -1
  43. data/lib/mongoid/dirty.rb +163 -29
  44. data/lib/mongoid/document.rb +58 -7
  45. data/lib/mongoid/errors.rb +2 -0
  46. data/lib/mongoid/errors/no_environment.rb +19 -0
  47. data/lib/mongoid/errors/scope_overwrite.rb +21 -0
  48. data/lib/mongoid/extensions.rb +6 -0
  49. data/lib/mongoid/extensions/array/deep_copy.rb +25 -0
  50. data/lib/mongoid/extensions/hash/deep_copy.rb +25 -0
  51. data/lib/mongoid/extensions/hash/scoping.rb +1 -1
  52. data/lib/mongoid/extensions/object/deep_copy.rb +21 -0
  53. data/lib/mongoid/extensions/proc/scoping.rb +2 -2
  54. data/lib/mongoid/extensions/symbol/inflections.rb +1 -0
  55. data/lib/mongoid/fields.rb +171 -104
  56. data/lib/mongoid/fields/{serializable → internal}/array.rb +33 -1
  57. data/lib/mongoid/fields/{serializable → internal}/big_decimal.rb +16 -1
  58. data/lib/mongoid/fields/{serializable → internal}/bignum.rb +1 -1
  59. data/lib/mongoid/fields/{serializable → internal}/binary.rb +1 -1
  60. data/lib/mongoid/fields/{serializable → internal}/boolean.rb +16 -1
  61. data/lib/mongoid/fields/{serializable → internal}/date.rb +1 -1
  62. data/lib/mongoid/fields/{serializable → internal}/date_time.rb +1 -1
  63. data/lib/mongoid/fields/{serializable → internal}/fixnum.rb +1 -1
  64. data/lib/mongoid/fields/{serializable → internal}/float.rb +16 -1
  65. data/lib/mongoid/fields/internal/foreign_keys/array.rb +74 -0
  66. data/lib/mongoid/fields/{serializable → internal}/foreign_keys/object.rb +11 -2
  67. data/lib/mongoid/fields/{serializable → internal}/hash.rb +1 -1
  68. data/lib/mongoid/fields/{serializable → internal}/integer.rb +16 -1
  69. data/lib/mongoid/fields/{serializable → internal}/localized.rb +23 -2
  70. data/lib/mongoid/fields/{serializable → internal}/nil_class.rb +16 -1
  71. data/lib/mongoid/fields/{serializable → internal}/object.rb +1 -1
  72. data/lib/mongoid/fields/{serializable → internal}/object_id.rb +16 -1
  73. data/lib/mongoid/fields/{serializable → internal}/range.rb +21 -2
  74. data/lib/mongoid/fields/{serializable → internal}/set.rb +16 -1
  75. data/lib/mongoid/fields/{serializable → internal}/string.rb +16 -1
  76. data/lib/mongoid/fields/{serializable → internal}/symbol.rb +17 -1
  77. data/lib/mongoid/fields/{serializable → internal}/time.rb +1 -1
  78. data/lib/mongoid/fields/{serializable → internal}/time_with_zone.rb +1 -1
  79. data/lib/mongoid/fields/{serializable → internal}/timekeeping.rb +16 -1
  80. data/lib/mongoid/fields/mappings.rb +8 -3
  81. data/lib/mongoid/fields/serializable.rb +34 -3
  82. data/lib/mongoid/hierarchy.rb +14 -14
  83. data/lib/mongoid/identity_map.rb +3 -2
  84. data/lib/mongoid/logger.rb +1 -7
  85. data/lib/mongoid/named_scope.rb +16 -12
  86. data/lib/mongoid/observer.rb +5 -1
  87. data/lib/mongoid/paranoia.rb +1 -0
  88. data/lib/mongoid/persistence.rb +11 -4
  89. data/lib/mongoid/persistence/atomic.rb +4 -1
  90. data/lib/mongoid/persistence/atomic/add_to_set.rb +17 -1
  91. data/lib/mongoid/persistence/atomic/sets.rb +1 -1
  92. data/lib/mongoid/railties/database.rake +1 -1
  93. data/lib/mongoid/relations.rb +1 -3
  94. data/lib/mongoid/relations/auto_save.rb +1 -1
  95. data/lib/mongoid/relations/builders.rb +1 -1
  96. data/lib/mongoid/relations/builders/embedded/many.rb +2 -6
  97. data/lib/mongoid/relations/builders/nested_attributes/many.rb +1 -1
  98. data/lib/mongoid/relations/builders/nested_attributes/one.rb +1 -1
  99. data/lib/mongoid/relations/builders/referenced/many_to_many.rb +1 -1
  100. data/lib/mongoid/relations/cascading/delete.rb +1 -1
  101. data/lib/mongoid/relations/cyclic.rb +10 -6
  102. data/lib/mongoid/relations/embedded/atomic.rb +3 -3
  103. data/lib/mongoid/relations/embedded/many.rb +98 -20
  104. data/lib/mongoid/relations/macros.rb +2 -0
  105. data/lib/mongoid/relations/many.rb +13 -0
  106. data/lib/mongoid/relations/metadata.rb +3 -3
  107. data/lib/mongoid/relations/nested_builder.rb +4 -3
  108. data/lib/mongoid/relations/proxy.rb +0 -1
  109. data/lib/mongoid/relations/referenced/batch.rb +3 -2
  110. data/lib/mongoid/relations/referenced/in.rb +3 -3
  111. data/lib/mongoid/relations/referenced/many.rb +89 -10
  112. data/lib/mongoid/relations/referenced/many_to_many.rb +34 -43
  113. data/lib/mongoid/relations/referenced/one.rb +8 -4
  114. data/lib/mongoid/relations/synchronization.rb +22 -5
  115. data/lib/mongoid/threaded.rb +38 -276
  116. data/lib/mongoid/threaded/lifecycle.rb +18 -18
  117. data/lib/mongoid/timestamps/updated.rb +13 -3
  118. data/lib/mongoid/validations.rb +22 -1
  119. data/lib/mongoid/validations/presence.rb +40 -0
  120. data/lib/mongoid/validations/uniqueness.rb +14 -3
  121. data/lib/mongoid/version.rb +1 -1
  122. data/lib/mongoid/versioning.rb +6 -2
  123. data/lib/rails/mongoid.rb +7 -1
  124. metadata +64 -45
  125. data/lib/mongoid/fields/serializable/foreign_keys/array.rb +0 -42
  126. data/lib/mongoid/relations/embedded/sort.rb +0 -31
@@ -22,7 +22,7 @@ pt:
22
22
  "Invalid option :%{invalid} provided to relation :%{name}. Valid options
23
23
  are: %{valid}."
24
24
  unsupported_version:
25
- MongoDB %{version} não é suportada, por favor actualize para a
25
+ MongoDB %{version} não é suportada, por favor actualize para a
26
26
  versão %{mongo_version}.
27
27
  validations:
28
28
  A validação falhou - %{errors}.
@@ -32,7 +32,7 @@ pt:
32
32
  do documento raiz.
33
33
  invalid_field:
34
34
  Não é permitido definir um campo com o nome %{name}. Não defina
35
- campos que entrem em conflito com os nomes dos atributos internos e métodos
35
+ campos que entrem em conflito com os nomes dos atributos internos e métodos
36
36
  do Mongoid. Use Document#instance_methods para consultar esses nomes.
37
37
  too_many_nested_attribute_records:
38
38
  A aceitação de atributos encadeados para %{association} encontra-se limitada
@@ -51,3 +51,9 @@ pt:
51
51
  mixed_relations:
52
52
  Referencing a(n) %{embedded} document from the %{root} document via a
53
53
  relational association is not allowed since the %{embedded} is embedded.
54
+ no_environment:
55
+ "Mongoid attempted to find the appropriate environment but no Rails.env,
56
+ Sinatra::Base.environment, or RACK_ENV could be found."
57
+ scope_overwrite:
58
+ "Cannot create scope :%{scope_name}, because of existing method
59
+ %{model_name}.%{scope_name}."
@@ -57,3 +57,9 @@ ro:
57
57
  mixed_relations:
58
58
  Referencing a(n) %{embedded} document from the %{root} document via a
59
59
  relational association is not allowed since the %{embedded} is embedded.
60
+ no_environment:
61
+ "Mongoid attempted to find the appropriate environment but no Rails.env,
62
+ Sinatra::Base.environment, or RACK_ENV could be found."
63
+ scope_overwrite:
64
+ "Cannot create scope :%{scope_name}, because of existing method
65
+ %{model_name}.%{scope_name}."
@@ -52,3 +52,9 @@ ru:
52
52
  mixed_relations:
53
53
  Referencing a(n) %{embedded} document from the %{root} document via a
54
54
  relational association is not allowed since the %{embedded} is embedded.
55
+ no_environment:
56
+ "Mongoid attempted to find the appropriate environment but no Rails.env,
57
+ Sinatra::Base.environment, or RACK_ENV could be found."
58
+ scope_overwrite:
59
+ "Cannot create scope :%{scope_name}, because of existing method
60
+ %{model_name}.%{scope_name}."
@@ -51,3 +51,9 @@ sv:
51
51
  mixed_relations:
52
52
  Referencing a(n) %{embedded} document from the %{root} document via a
53
53
  relational association is not allowed since the %{embedded} is embedded.
54
+ no_environment:
55
+ "Mongoid attempted to find the appropriate environment but no Rails.env,
56
+ Sinatra::Base.environment, or RACK_ENV could be found."
57
+ scope_overwrite:
58
+ "Cannot create scope :%{scope_name}, because of existing method
59
+ %{model_name}.%{scope_name}."
@@ -16,7 +16,7 @@ vi:
16
16
  invalid_time:
17
17
  "'%{value}' is not a valid Time."
18
18
  invalid_type:
19
- Trường được định nghĩa là một %{klass}, nhưng nhận được %{other} với
19
+ Trường được định nghĩa là một %{klass}, nhưng nhận được %{other} với
20
20
  giá trị %{value}.
21
21
  invalid_options:
22
22
  "Invalid option :%{invalid} provided to relation :%{name}. Valid options
@@ -27,15 +27,15 @@ vi:
27
27
  validations:
28
28
  Kiểm định không thành công - %{errors}.
29
29
  invalid_collection:
30
- Không được phép truy cập tập văn bản cuả %{klass} vì đây
31
- là một văn bản nhúng, xin vui lòng truy cập tập văn bản từ
30
+ Không được phép truy cập tập văn bản cuả %{klass} vì đây
31
+ là một văn bản nhúng, xin vui lòng truy cập tập văn bản từ
32
32
  văn bản gốc.
33
33
  invalid_field:
34
- Không được phép định nghĩa tên trường là %{name}. Không định nghĩa
34
+ Không được phép định nghĩa tên trường là %{name}. Không định nghĩa
35
35
  tên trường trùng với các thuộc tính có sẵn hoặc các tên hàm cuả Mongoid.
36
36
  Sử dụng Use Document#instance_methods để xem danh sách tên đã được dùng.
37
37
  too_many_nested_attribute_records:
38
- Số lượng các thuộc tính lồng cho %{association} bị giới hạn
38
+ Số lượng các thuộc tính lồng cho %{association} bị giới hạn
39
39
  ở %{limit} bản ghi.
40
40
  embedded_in_must_have_inverse_of:
41
41
  Phải cho thêm inverse_of vào lực chọn cuả kết hợp embedded_in.
@@ -43,13 +43,19 @@ vi:
43
43
  Lựa chọn dependent => destroy|delete
44
44
  chỉ có hiệu lực với kết hợp references_one hoặc references_many.
45
45
  association_cant_have_inverse_of:
46
- Không được phép định nghĩa inverse_of cho kết hợp này. Chỉ
46
+ Không được phép định nghĩa inverse_of cho kết hợp này. Chỉ
47
47
  sử dụng lựa chọn này cho embedded_in or references_many as array.
48
48
  calling_document_find_with_nil_is_invalid:
49
49
  Không được phép gọi Document#find với giá trị nil
50
50
  unsaved_document:
51
- Bạn không được gọi create hoặc create! thông qua một quan hệ kết hợp
51
+ Bạn không được gọi create hoặc create! thông qua một quan hệ kết hợp
52
52
  mà văn bản gốc (%{base}) chưa được lưu.
53
53
  mixed_relations:
54
- Không được phép tham chiếu một văn bản %{embedded} từ văn bản %{root} thông qua một
54
+ Không được phép tham chiếu một văn bản %{embedded} từ văn bản %{root} thông qua một
55
55
  quan hệ kết hợp vì %{embedded} là kiểu nhúng.
56
+ no_environment:
57
+ "Mongoid attempted to find the appropriate environment but no Rails.env,
58
+ Sinatra::Base.environment, or RACK_ENV could be found."
59
+ scope_overwrite:
60
+ "Cannot create scope :%{scope_name}, because of existing method
61
+ %{model_name}.%{scope_name}."
@@ -44,3 +44,9 @@ zh-CN:
44
44
  mixed_relations:
45
45
  Referencing a(n) %{embedded} document from the %{root} document via a
46
46
  relational association is not allowed since the %{embedded} is embedded.
47
+ no_environment:
48
+ "Mongoid attempted to find the appropriate environment but no Rails.env,
49
+ Sinatra::Base.environment, or RACK_ENV could be found."
50
+ scope_overwrite:
51
+ "Cannot create scope :%{scope_name}, because of existing method
52
+ %{model_name}.%{scope_name}."
@@ -9,6 +9,16 @@ module Mongoid #:nodoc:
9
9
  module Atomic
10
10
  extend ActiveSupport::Concern
11
11
 
12
+ UPDATES = [
13
+ :atomic_array_pushes,
14
+ :atomic_array_pulls,
15
+ :atomic_array_add_to_sets,
16
+ :atomic_pulls,
17
+ :atomic_unsets,
18
+ :delayed_atomic_sets,
19
+ :delayed_atomic_pulls
20
+ ]
21
+
12
22
  included do
13
23
 
14
24
  # When MongoDB finally fully implements the positional operator, we can
@@ -16,6 +26,55 @@ module Mongoid #:nodoc:
16
26
  attr_accessor :_index
17
27
  end
18
28
 
29
+ # Add the document as an atomic pull.
30
+ #
31
+ # @example Add the atomic pull.
32
+ # person.add_atomic_pull(address)
33
+ #
34
+ # @param [ Document ] The embedded document to pull.
35
+ #
36
+ # @since 2.2.0
37
+ def add_atomic_pull(document)
38
+ document.flagged_for_destroy = true
39
+ (delayed_atomic_pulls[document.metadata.name.to_s] ||= []).push(document)
40
+ end
41
+
42
+ # For array fields these are the pushes that need to happen.
43
+ #
44
+ # @example Get the array pushes.
45
+ # person.atomic_array_pushes
46
+ #
47
+ # @return [ Hash ] The array pushes.
48
+ #
49
+ # @since 2.4.0
50
+ def atomic_array_pushes
51
+ @atomic_array_pushes ||= {}
52
+ end
53
+
54
+ # For array fields these are the pulls that need to happen.
55
+ #
56
+ # @example Get the array pulls.
57
+ # person.atomic_array_pulls
58
+ #
59
+ # @return [ Hash ] The array pulls.
60
+ #
61
+ # @since 2.4.0
62
+ def atomic_array_pulls
63
+ @atomic_array_pulls ||= {}
64
+ end
65
+
66
+ # For array fields these are the unique adds that need to happen.
67
+ #
68
+ # @example Get the array unique adds.
69
+ # person.atomic_array_add_to_sets
70
+ #
71
+ # @return [ Hash ] The array add_to_sets.
72
+ #
73
+ # @since 2.4.0
74
+ def atomic_array_add_to_sets
75
+ @atomic_array_add_to_sets ||= {}
76
+ end
77
+
19
78
  # Get all the atomic updates that need to happen for the current
20
79
  # +Document+. This includes all changes that need to happen in the
21
80
  # entire hierarchy that exists below where the save call was made.
@@ -112,19 +171,6 @@ module Mongoid #:nodoc:
112
171
  end
113
172
  end
114
173
 
115
- # Add the document as an atomic pull.
116
- #
117
- # @example Add the atomic pull.
118
- # person.add_atomic_pull(address)
119
- #
120
- # @param [ Document ] The embedded document to pull.
121
- #
122
- # @since 2.2.0
123
- def add_atomic_pull(document)
124
- document.flagged_for_destroy = true
125
- (delayed_atomic_pulls[document.metadata.name.to_s] ||= []).push(document)
126
- end
127
-
128
174
  # Get all the push attributes that need to occur.
129
175
  #
130
176
  # @example Get the pushes.
@@ -225,6 +271,9 @@ module Mongoid #:nodoc:
225
271
  mods.set(doc.atomic_sets)
226
272
  mods.set(doc.delayed_atomic_sets)
227
273
  mods.push(doc.atomic_pushes)
274
+ mods.push(doc.atomic_array_pushes)
275
+ mods.add_to_set(doc.atomic_array_add_to_sets)
276
+ mods.pull(doc.atomic_array_pulls)
228
277
  end
229
278
  end
230
279
  end
@@ -6,6 +6,26 @@ module Mongoid #:nodoc:
6
6
  # database.
7
7
  class Modifiers < Hash
8
8
 
9
+ # Add the atomic $addToSet modifiers to the hash.
10
+ #
11
+ # @example Add the $addToSet modifiers.
12
+ # modifiers.add_to_set({ "preference_ids" => [ "one" ] })
13
+ #
14
+ # @param [ Hash ] modifications The add to set modifiers.
15
+ #
16
+ # @since 2.4.0
17
+ def add_to_set(modifications)
18
+ modifications.each_pair do |field, value|
19
+ if add_to_sets.has_key?(field)
20
+ value.each do |val|
21
+ add_to_sets[field]["$each"].push(val)
22
+ end
23
+ else
24
+ add_to_sets[field] = { "$each" => value }
25
+ end
26
+ end
27
+ end
28
+
9
29
  # Adds pull modifiers to the modifiers hash.
10
30
  #
11
31
  # @example Add pull operations.
@@ -90,6 +110,18 @@ module Mongoid #:nodoc:
90
110
  end
91
111
  end
92
112
 
113
+ # Get the $addToSet operations or intialize a new one.
114
+ #
115
+ # @example Get the $addToSet operations.
116
+ # modifiers.add_to_sets
117
+ #
118
+ # @return [ Hash ] The $addToSet operations.
119
+ #
120
+ # @since 2.4.0
121
+ def add_to_sets
122
+ self["$addToSet"] ||= {}
123
+ end
124
+
93
125
  # Is the operation going to be a conflict for a $set?
94
126
  #
95
127
  # @example Is this a conflict for a set?
@@ -194,7 +226,7 @@ module Mongoid #:nodoc:
194
226
  # Get the $pullAll operations or intialize a new one.
195
227
  #
196
228
  # @example Get the $pullAll operations.
197
- # modifiers.pulles
229
+ # modifiers.pulls
198
230
  #
199
231
  # @return [ Hash ] The $pullAll operations.
200
232
  #
@@ -157,24 +157,6 @@ module Mongoid #:nodoc:
157
157
 
158
158
  protected
159
159
 
160
- # Set any missing default values in the attributes.
161
- #
162
- # @example Get the raw attributes after defaults have been applied.
163
- # person.apply_defaults
164
- #
165
- # @return [ Hash ] The raw attributes.
166
- #
167
- # @since 2.0.0.rc.8
168
- def apply_defaults
169
- defaults.each do |name|
170
- unless attributes.has_key?(name)
171
- if field = fields[name]
172
- attributes[name] = field.eval_default(self)
173
- end
174
- end
175
- end
176
- end
177
-
178
160
  # Used for allowing accessor methods for dynamic attributes.
179
161
  #
180
162
  # @param [ String, Symbol ] name The name of the method.
@@ -226,8 +208,12 @@ module Mongoid #:nodoc:
226
208
  alias :#{name} :#{original}
227
209
  alias :#{name}= :#{original}=
228
210
  alias :#{name}? :#{original}?
211
+ alias :#{name}_change :#{original}_change
212
+ alias :#{name}_changed? :#{original}_changed?
213
+ alias :reset_#{name}! :reset_#{original}!
214
+ alias :#{name}_was :#{original}_was
215
+ alias :#{name}_will_change! :#{original}_will_change!
229
216
  RUBY
230
- super
231
217
  end
232
218
  end
233
219
  end
@@ -7,7 +7,7 @@ module Mongoid #:nodoc:
7
7
 
8
8
  CALLBACKS = [
9
9
  :before_validation, :after_validation,
10
- :after_initialize,
10
+ :after_initialize, :after_build,
11
11
  :before_create, :around_create, :after_create,
12
12
  :before_destroy, :around_destroy, :after_destroy,
13
13
  :before_save, :around_save, :after_save,
@@ -19,6 +19,7 @@ module Mongoid #:nodoc:
19
19
  include ActiveModel::Validations::Callbacks
20
20
 
21
21
  define_model_callbacks :initialize, :only => :after
22
+ define_model_callbacks :build, :only => :after
22
23
  define_model_callbacks :create, :destroy, :save, :update
23
24
  end
24
25
 
@@ -91,7 +91,7 @@ module Mongoid #:nodoc
91
91
  #
92
92
  # @since 2.0.2, batch-relational-insert
93
93
  def insert(documents, options = {})
94
- consumer = Threaded.insert
94
+ consumer = Threaded.insert(name)
95
95
  if consumer
96
96
  consumer.consume(documents, options)
97
97
  else
@@ -142,7 +142,7 @@ module Mongoid #:nodoc
142
142
  #
143
143
  # @since 2.0.0
144
144
  def update(selector, document, options = {})
145
- updater = Threaded.update_consumer(name)
145
+ updater = Threaded.update_consumer(klass)
146
146
  if updater
147
147
  updater.consume(selector, document, options)
148
148
  else
@@ -28,18 +28,30 @@ module Mongoid #:nodoc:
28
28
  begin
29
29
  yield
30
30
  rescue Mongo::ConnectionFailure => ex
31
- retries += 1
32
- raise ex if retries > Mongoid.max_retries_on_connection_failure
33
- Kernel.sleep(0.5)
34
- log_retry retries
31
+ retries = increase_retry_attempts(retries, ex)
35
32
  retry
33
+ rescue Mongo::OperationFailure => ex
34
+ if ex.message =~ /not master/
35
+ retries = increase_retry_attempts(retries, ex)
36
+ retry
37
+ else
38
+ raise ex
39
+ end
36
40
  end
37
41
  end
38
42
 
39
43
  private
40
44
 
41
- def log_retry(retry_number)
42
- Mongoid.logger.warn "A Mongo::ConnectionFailure was raised. Retry attempt ##{retry_number}."
45
+ def increase_retry_attempts(retries, ex)
46
+ retries += 1
47
+ raise ex if retries > Mongoid.max_retries_on_connection_failure
48
+ Kernel.sleep(0.5)
49
+ log_retry retries, ex
50
+ retries
51
+ end
52
+
53
+ def log_retry(retry_number, ex)
54
+ Mongoid.logger.warn "A #{ex.class.name} was raised. Retry attempt ##{retry_number}."
43
55
  end
44
56
  end
45
57
  end
@@ -8,6 +8,8 @@ module Mongoid #:nodoc
8
8
  included do
9
9
  extend ActiveModel::Translation
10
10
  extend Mongoid::Finders
11
+
12
+ class_attribute :paranoid
11
13
  end
12
14
 
13
15
  include ActiveModel::Conversion
@@ -1,7 +1,9 @@
1
1
  # encoding: utf-8
2
2
  require "uri"
3
3
  require "mongoid/config/database"
4
+ require "mongoid/config/environment"
4
5
  require "mongoid/config/replset_database"
6
+ require "mongoid/config/options"
5
7
 
6
8
  module Mongoid #:nodoc
7
9
 
@@ -11,40 +13,11 @@ module Mongoid #:nodoc
11
13
  # @todo Durran: This module needs an overhaul, remove singleton, etc.
12
14
  module Config
13
15
  extend self
16
+ extend Options
14
17
  include ActiveModel::Observing
15
18
 
16
- attr_accessor :master, :settings, :defaults
17
- @settings = {}
18
- @defaults = {}
19
-
20
- # Define a configuration option with a default.
21
- #
22
- # @example Define the option.
23
- # Config.option(:persist_in_safe_mode, :default => false)
24
- #
25
- # @param [ Symbol ] name The name of the configuration option.
26
- # @param [ Hash ] options Extras for the option.
27
- #
28
- # @option options [ Object ] :default The default value.
29
- #
30
- # @since 2.0.0.rc.1
31
- def option(name, options = {})
32
- defaults[name] = settings[name] = options[:default]
33
-
34
- class_eval <<-RUBY
35
- def #{name}
36
- settings[#{name.inspect}]
37
- end
38
-
39
- def #{name}=(value)
40
- settings[#{name.inspect}] = value
41
- end
42
-
43
- def #{name}?
44
- #{name}
45
- end
46
- RUBY
47
- end
19
+ # @attribute [rw] master The master database.
20
+ attr_accessor :master
48
21
 
49
22
  option :allow_dynamic_fields, :default => true
50
23
  option :autocreate_indexes, :default => false
@@ -52,6 +25,7 @@ module Mongoid #:nodoc
52
25
  option :include_root_in_json, :default => false
53
26
  option :max_retries_on_connection_failure, :default => 0
54
27
  option :parameterize_keys, :default => true
28
+ option :scope_overwrite_exception, :default => false
55
29
  option :persist_in_safe_mode, :default => false
56
30
  option :preload_models, :default => false
57
31
  option :raise_not_found_error, :default => true
@@ -130,10 +104,8 @@ module Mongoid #:nodoc
130
104
  #
131
105
  # @since 2.0.1
132
106
  def load!(path)
133
- environment = defined?(Rails) && Rails.respond_to?(:env) ? Rails.env : ENV["RACK_ENV"]
134
- settings = YAML.load(ERB.new(File.new(path).read).result)[environment]
135
- if settings.present?
136
- from_hash(settings)
107
+ Environment.load_yaml(path).tap do |settings|
108
+ from_hash(settings) if settings.present?
137
109
  end
138
110
  end
139
111
 
@@ -258,24 +230,6 @@ module Mongoid #:nodoc
258
230
  end
259
231
  end
260
232
 
261
- # Reset the configuration options to the defaults.
262
- #
263
- # @example Reset the configuration options.
264
- # config.reset
265
- def reset
266
- settings.replace(defaults)
267
- end
268
-
269
- # @deprecated User replica sets instead.
270
- def slaves
271
- slave_warning!
272
- end
273
-
274
- # @deprecated User replica sets instead.
275
- def slaves=(dbs)
276
- slave_warning!
277
- end
278
-
279
233
  protected
280
234
 
281
235
  # Check if the database is valid and the correct version.
@@ -334,14 +288,5 @@ module Mongoid #:nodoc
334
288
  end
335
289
  end
336
290
  end
337
-
338
- # Temporarily here so people can move to replica sets.
339
- def slave_warning!
340
- warn(
341
- "Using Mongoid for traditional slave databases will be removed in the " +
342
- "next release in preference of replica sets. Please change your setup " +
343
- "accordingly."
344
- )
345
- end
346
291
  end
347
292
  end