mongoid 7.0.1 → 7.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/lib/mongoid/association.rb +0 -1
  5. data/lib/mongoid/association/depending.rb +22 -1
  6. data/lib/mongoid/association/embedded/embeds_one/proxy.rb +3 -3
  7. data/lib/mongoid/association/referenced/has_many/proxy.rb +1 -1
  8. data/lib/mongoid/association/relatable.rb +16 -2
  9. data/lib/mongoid/attributes/nested.rb +14 -3
  10. data/lib/mongoid/contextual/map_reduce.rb +1 -1
  11. data/lib/mongoid/copyable.rb +3 -2
  12. data/lib/mongoid/document.rb +2 -0
  13. data/lib/mongoid/matchable.rb +3 -0
  14. data/lib/mongoid/matchable/nor.rb +37 -0
  15. data/lib/mongoid/persistable/settable.rb +58 -13
  16. data/lib/mongoid/persistence_context.rb +5 -1
  17. data/lib/mongoid/touchable.rb +102 -0
  18. data/lib/mongoid/version.rb +1 -1
  19. data/spec/app/models/array_field.rb +7 -0
  20. data/spec/app/models/updatable.rb +7 -0
  21. data/spec/mongoid/association/accessors_spec.rb +39 -0
  22. data/spec/mongoid/association/depending_spec.rb +253 -0
  23. data/spec/mongoid/association/polymorphic_spec.rb +59 -0
  24. data/spec/mongoid/association/referenced/belongs_to_spec.rb +3 -3
  25. data/spec/mongoid/association/referenced/has_one_spec.rb +59 -0
  26. data/spec/mongoid/attributes/nested_spec.rb +18 -2
  27. data/spec/mongoid/clients/factory_spec.rb +3 -3
  28. data/spec/mongoid/clients/options_spec.rb +28 -13
  29. data/spec/mongoid/clients/sessions_spec.rb +3 -3
  30. data/spec/mongoid/clients/transactions_spec.rb +369 -0
  31. data/spec/mongoid/copyable_spec.rb +16 -0
  32. data/spec/mongoid/matchable/nor_spec.rb +209 -0
  33. data/spec/mongoid/matchable_spec.rb +26 -1
  34. data/spec/mongoid/persistable/settable_spec.rb +128 -9
  35. data/spec/mongoid/{association/touchable_spec.rb → touchable_spec.rb} +28 -7
  36. data/spec/spec_helper.rb +8 -0
  37. metadata +457 -448
  38. metadata.gz.sig +0 -0
  39. data/lib/mongoid/association/touchable.rb +0 -97
metadata.gz.sig CHANGED
Binary file
@@ -1,97 +0,0 @@
1
- # encoding: utf-8
2
- module Mongoid
3
- module Association
4
- module Touchable
5
-
6
- module InstanceMethods
7
-
8
- # Touch the document, in effect updating its updated_at timestamp and
9
- # optionally the provided field to the current time. If any belongs_to
10
- # relations exist with a touch option, they will be updated as well.
11
- #
12
- # @example Update the updated_at timestamp.
13
- # document.touch
14
- #
15
- # @example Update the updated_at and provided timestamps.
16
- # document.touch(:audited)
17
- #
18
- # @note This will not autobuild relations if those options are set.
19
- #
20
- # @param [ Symbol ] field The name of an additional field to update.
21
- #
22
- # @return [ true/false ] false if record is new_record otherwise true.
23
- #
24
- # @since 3.0.0
25
- def touch(field = nil)
26
- return false if _root.new_record?
27
- current = Time.now
28
- field = database_field_name(field)
29
- write_attribute(:updated_at, current) if respond_to?("updated_at=")
30
- write_attribute(field, current) if field
31
-
32
- touches = touch_atomic_updates(field)
33
- unless touches["$set"].blank?
34
- selector = atomic_selector
35
- _root.collection.find(selector).update_one(positionally(selector, touches), session: _session)
36
- end
37
- run_callbacks(:touch)
38
- true
39
- end
40
- end
41
-
42
- extend self
43
-
44
- # Add the association to the touchable relations if the touch option was
45
- # provided.
46
- #
47
- # @example Add the touchable.
48
- # Model.define_touchable!(assoc)
49
- #
50
- # @param [ Association ] association The association metadata.
51
- #
52
- # @return [ Class ] The model class.
53
- #
54
- # @since 3.0.0
55
- def define_touchable!(association)
56
- name = association.name
57
- method_name = define_relation_touch_method(name, association)
58
- association.inverse_class.tap do |klass|
59
- klass.after_save method_name
60
- klass.after_destroy method_name
61
- klass.after_touch method_name
62
- end
63
- end
64
-
65
- private
66
-
67
- # Define the method that will get called for touching belongs_to
68
- # relations.
69
- #
70
- # @api private
71
- #
72
- # @example Define the touch relation.
73
- # Model.define_relation_touch_method(:band)
74
- # Model.define_relation_touch_method(:band, :band_updated_at)
75
- #
76
- # @param [ Symbol ] name The name of the relation.
77
- # @param [ Association ] association The association metadata.
78
- #
79
- # @since 3.1.0
80
- #
81
- # @return [ Symbol ] The method name.
82
- def define_relation_touch_method(name, association)
83
- association.relation_class.send(:include, InstanceMethods)
84
- method_name = "touch_#{name}_after_create_or_destroy"
85
- association.inverse_class.class_eval <<-TOUCH, __FILE__, __LINE__ + 1
86
- def #{method_name}
87
- without_autobuild do
88
- relation = __send__(:#{name})
89
- relation.touch #{":#{association.touch_field}" if association.touch_field} if relation
90
- end
91
- end
92
- TOUCH
93
- method_name.to_sym
94
- end
95
- end
96
- end
97
- end