mongoid 2.5.2 → 2.6.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,6 +3,12 @@
3
3
  For instructions on upgrading to newer versions, visit
4
4
  [mongoid.org](http://mongoid.org/docs/upgrading.html).
5
5
 
6
+ ## 2.6.0
7
+
8
+ ### New Features
9
+
10
+ * \#2709 Backported the `touch` functionality from Mongoid 3.
11
+
6
12
  ## 2.5.2
7
13
 
8
14
  ### Resolved Issues
@@ -281,5 +281,29 @@ module Mongoid #:nodoc:
281
281
  mods.add_to_set(doc.atomic_array_add_to_sets)
282
282
  mods.pull_all(doc.atomic_array_pulls)
283
283
  end
284
+
285
+
286
+ # Get the atomic updates for a touch operation. Should only include the
287
+ # updated_at field and the optional extra field.
288
+ #
289
+ # @api private
290
+ #
291
+ # @example Get the touch atomic updates.
292
+ # document.touch_atomic_updates
293
+ #
294
+ # @param [ Symbol ] field The optional field.
295
+ #
296
+ # @return [ Hash ] The atomic updates.
297
+ #
298
+ # @since 2.6.0
299
+ def touch_atomic_updates(field = nil)
300
+ updates = atomic_updates
301
+ return {} unless atomic_updates.has_key?("$set")
302
+ touches = {}
303
+ updates["$set"].each_pair do |key, value|
304
+ touches.merge!({ key => value }) if key =~ /updated_at|#{field}/
305
+ end
306
+ { "$set" => touches }
307
+ end
284
308
  end
285
309
  end
@@ -79,6 +79,39 @@ module Mongoid #:nodoc:
79
79
  return true
80
80
  end
81
81
 
82
+
83
+ # Touch the document, in effect updating its updated_at timestamp and
84
+ # optionally the provided field to the current time. If any belongs_to
85
+ # relations exist with a touch option, they will be updated as well.
86
+ #
87
+ # @example Update the updated_at timestamp.
88
+ # document.touch
89
+ #
90
+ # @example Update the updated_at and provided timestamps.
91
+ # document.touch(:audited)
92
+ #
93
+ # @note This will not autobuild relations if those options are set.
94
+ #
95
+ # @param [ Symbol ] field The name of an additional field to update.
96
+ #
97
+ # @return [ true/false ] false if record is new_record otherwise true.
98
+ #
99
+ # @since 2.6.0
100
+ def touch(field = nil)
101
+ return false if _root.new_record?
102
+ current = Time.now
103
+ write_attribute(:updated_at, current) if fields["updated_at"]
104
+ write_attribute(field, current) if field
105
+
106
+ touches = touch_atomic_updates(field)
107
+ unless touches.empty?
108
+ _root.collection.update(atomic_selector, touches)
109
+ end
110
+
111
+ touchables.each { |name| send(name).try(:touch) }
112
+ move_changes and true
113
+ end
114
+
82
115
  # Update the document in the datbase.
83
116
  #
84
117
  # @example Update an existing document.
@@ -24,6 +24,7 @@ require "mongoid/relations/referenced/many_to_many"
24
24
  require "mongoid/relations/referenced/one"
25
25
  require "mongoid/relations/reflections"
26
26
  require "mongoid/relations/synchronization"
27
+ require "mongoid/relations/touchable"
27
28
  require "mongoid/relations/metadata"
28
29
  require "mongoid/relations/macros"
29
30
 
@@ -43,6 +44,7 @@ module Mongoid # :nodoc:
43
44
  include Polymorphic
44
45
  include Reflections
45
46
  include Synchronization
47
+ include Touchable
46
48
 
47
49
  attr_accessor :metadata
48
50
 
@@ -134,6 +134,7 @@ module Mongoid # :nodoc:
134
134
  reference(meta)
135
135
  autosave(meta)
136
136
  validates_relation(meta)
137
+ touchable(meta)
137
138
  end
138
139
  end
139
140
  alias :belongs_to_related :belongs_to
@@ -754,6 +754,18 @@ module Mongoid # :nodoc:
754
754
  !!order
755
755
  end
756
756
 
757
+ # Is this relation touchable?
758
+ #
759
+ # @example Is the relation touchable?
760
+ # metadata.touchable?
761
+ #
762
+ # @return [ true, false ] If the relation can be touched.
763
+ #
764
+ # @since 3.0.0
765
+ def touchable?
766
+ !!self[:touch]
767
+ end
768
+
757
769
  private
758
770
 
759
771
  # Returns the class name for the relation.
@@ -240,7 +240,7 @@ module Mongoid # :nodoc:
240
240
  #
241
241
  # @since 2.1.0
242
242
  def valid_options
243
- [ :autosave, :foreign_key, :index, :polymorphic ]
243
+ [ :autosave, :foreign_key, :index, :polymorphic, :touch ]
244
244
  end
245
245
 
246
246
  # Get the default validation setting for the relation. Determines if
@@ -0,0 +1,32 @@
1
+ # encoding: utf-8
2
+ module Mongoid
3
+ module Relations
4
+ module Touchable
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ class_attribute :touchables
9
+ self.touchables = []
10
+ end
11
+
12
+ module ClassMethods
13
+
14
+ # Add the metadata to the touchable relations if the touch option was
15
+ # provided.
16
+ #
17
+ # @example Add the touchable.
18
+ # Model.touchable(meta)
19
+ #
20
+ # @param [ Metadata ] metadata The relation metadata.
21
+ #
22
+ # @return [ Class ] The model class.
23
+ #
24
+ # @since 3.0.0
25
+ def touchable(metadata)
26
+ self.touchables.push(metadata.name) if metadata.touchable?
27
+ self
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -1,4 +1,4 @@
1
1
  # encoding: utf-8
2
2
  module Mongoid #:nodoc
3
- VERSION = "2.5.2"
3
+ VERSION = "2.6.0"
4
4
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid
3
3
  version: !ruby/object:Gem::Version
4
- hash: 31
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 2
8
- - 5
9
- - 2
10
- version: 2.5.2
8
+ - 6
9
+ - 0
10
+ version: 2.6.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Durran Jordan
@@ -123,6 +123,22 @@ dependencies:
123
123
  type: :development
124
124
  name: guard-rspec
125
125
  requirement: *id007
126
+ - !ruby/object:Gem::Dependency
127
+ version_requirements: &id008 !ruby/object:Gem::Requirement
128
+ none: false
129
+ requirements:
130
+ - - ~>
131
+ - !ruby/object:Gem::Version
132
+ hash: 57
133
+ segments:
134
+ - 0
135
+ - 9
136
+ - 1
137
+ version: 0.9.1
138
+ prerelease: false
139
+ type: :development
140
+ name: rb-fsevent
141
+ requirement: *id008
126
142
  description: Mongoid is an ODM (Object Document Mapper) Framework for MongoDB, written in Ruby.
127
143
  email:
128
144
  - durran@gmail.com
@@ -380,6 +396,7 @@ files:
380
396
  - lib/mongoid/relations/synchronization.rb
381
397
  - lib/mongoid/relations/targets/enumerable.rb
382
398
  - lib/mongoid/relations/targets.rb
399
+ - lib/mongoid/relations/touchable.rb
383
400
  - lib/mongoid/relations.rb
384
401
  - lib/mongoid/reloading.rb
385
402
  - lib/mongoid/safety.rb