mongoid 3.1.5 → 3.1.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 45a87057b0667e0b764ac4cd7816d93ecbbacbed
4
- data.tar.gz: 7ca89da136c467233031ddec0c17eb77cb103019
3
+ metadata.gz: d24616c46c93a31777a37b97c93cca0b10df2625
4
+ data.tar.gz: 000fc93a80cfc7e296f305a1ddca2d77517578aa
5
5
  SHA512:
6
- metadata.gz: ebbee26a46ea97357737bdccad4cf96a1f6e57d815a8d68f1bac91d5c62c40b3c31812e90266860d5da23b65cccc540a62f5aba0d728ab64ba604405925cf8d2
7
- data.tar.gz: 3abd08faeb591bd9cf71cd976f5d8e99063fed8f2830ea916fb720464eebf4e0525b6135963f0a9c0f674fbf154091b185f0372285c9980e088dc63b1e271f56
6
+ metadata.gz: 71d177604ae5df43f63a5f45b1fa68ea2bedf263009ca58406e23ef6c3fdbe09f0b76bdf5e4c70a9612c515f3aa2fca8ae384d62ede1e86629f788905822e483
7
+ data.tar.gz: f840e9a8796efaa27b3536c95270c9e5abcbe233718a546095cc09671fd1d6edb8c7d16b55505ad33e1737ab610fb422b04318d0486c37841a2b9c495959ca2f
@@ -3,6 +3,19 @@
3
3
  For instructions on upgrading to newer versions, visit
4
4
  [mongoid.org](http://mongoid.org/en/mongoid/docs/upgrading.html).
5
5
 
6
+ ## 3.1.6
7
+
8
+ ### Resolved Issues
9
+
10
+ * \#3337 Ensure localized fields map is cloned with inheritance.
11
+
12
+ * \#3262 Fixed atomic array operations on HABTM foreign key fields from turning
13
+ single elements into arrays.
14
+
15
+ * \#3282 Fixed .timeless option to use a thread local instead of a class attribute.
16
+ Also remove the timeless methods from all docs, and only add to timestamps docs.
17
+ (Arthur Neves)
18
+
6
19
  ## 3.1.5
7
20
 
8
21
  ### Resolved Issues
@@ -38,7 +38,6 @@ module Mongoid
38
38
  include Mongoid::Sharding
39
39
  include Mongoid::State
40
40
  include Mongoid::Threaded::Lifecycle
41
- include Mongoid::Timestamps::Timeless
42
41
  include Mongoid::Validations
43
42
  include Mongoid::Callbacks
44
43
  include Mongoid::Copyable
@@ -97,7 +97,6 @@ module Mongoid
97
97
  def post_persist
98
98
  reset_persisted_children
99
99
  move_changes
100
- clear_timeless_option
101
100
  end
102
101
 
103
102
  # Get the previous changes on the document.
@@ -40,11 +40,11 @@ module Mongoid
40
40
  each_pair do |key, value|
41
41
  if key =~ /\$/
42
42
  value.each_pair do |_key, _value|
43
- value[_key] = mongoize_for(klass, _key, _value)
43
+ value[_key] = mongoize_for(key, klass, _key, _value)
44
44
  end
45
45
  (consolidated[key] ||= {}).merge!(value)
46
46
  else
47
- (consolidated["$set"] ||= {}).merge!(key => mongoize_for(klass, key, value))
47
+ (consolidated["$set"] ||= {}).merge!(key => mongoize_for(key, klass, key, value))
48
48
  end
49
49
  end
50
50
  consolidated
@@ -166,9 +166,17 @@ module Mongoid
166
166
  # @return [ Object ] The mongoized value.
167
167
  #
168
168
  # @since 3.1.0
169
- def mongoize_for(klass, key, value)
169
+ def mongoize_for(operator, klass, key, value)
170
170
  field = klass.fields[key.to_s]
171
- field ? field.mongoize(value) : value
171
+ if field
172
+ val = field.mongoize(value)
173
+ if Mongoid::Persistence::LIST_OPERATIONS.include?(operator) && field.resizable?
174
+ val = val.first if !value.is_a?(Array)
175
+ end
176
+ val
177
+ else
178
+ value
179
+ end
172
180
  end
173
181
 
174
182
  module ClassMethods
@@ -169,6 +169,7 @@ module Mongoid
169
169
  super
170
170
  @_type = nil
171
171
  subclass.aliased_fields = aliased_fields.dup
172
+ subclass.localized_fields = localized_fields.dup
172
173
  subclass.fields = fields.dup
173
174
  subclass.pre_processed_defaults = pre_processed_defaults.dup
174
175
  subclass.post_processed_defaults = post_processed_defaults.dup
@@ -64,7 +64,6 @@ module Mongoid
64
64
  update({ "$set" => { paranoid_field => time }})
65
65
  @destroyed = true
66
66
  IdentityMap.remove(self)
67
- clear_timeless_option
68
67
  true
69
68
  end
70
69
  alias :delete :remove
@@ -21,6 +21,11 @@ module Mongoid
21
21
  include Atomic
22
22
  include Mongoid::Atomic::Positionable
23
23
 
24
+ # The atomic operations that deal with arrays or sets in the db.
25
+ #
26
+ # @since 4.0.0
27
+ LIST_OPERATIONS = [ "$addToSet", "$push", "$pull", "$pullAll" ].freeze
28
+
24
29
  # Remove the document from the database with callbacks.
25
30
  #
26
31
  # @example Destroy a document.
@@ -24,7 +24,6 @@ module Mongoid
24
24
  document.freeze
25
25
  document.destroyed = true
26
26
  IdentityMap.remove(document)
27
- document.clear_timeless_option
28
27
  true
29
28
  end
30
29
  end
@@ -304,7 +304,6 @@ module Mongoid
304
304
  _unscoped.delete_one(doc)
305
305
  unbind_one(doc)
306
306
  execute_callback :after_remove, doc
307
- doc.clear_timeless_option
308
307
  doc.as_document
309
308
  end
310
309
  end
@@ -1,8 +1,8 @@
1
1
  # encoding: utf-8
2
+ require "mongoid/timestamps/timeless"
2
3
  require "mongoid/timestamps/created"
3
4
  require "mongoid/timestamps/updated"
4
5
  require "mongoid/timestamps/short"
5
- require "mongoid/timestamps/timeless"
6
6
 
7
7
  module Mongoid
8
8
 
@@ -9,8 +9,10 @@ module Mongoid
9
9
  extend ActiveSupport::Concern
10
10
 
11
11
  included do
12
+ include Mongoid::Timestamps::Timeless
13
+
12
14
  field :created_at, type: Time
13
- set_callback :create, :before, :set_created_at, if: :timestamping?
15
+ set_callback :create, :before, :set_created_at
14
16
  end
15
17
 
16
18
  # Update the created_at field on the Document to the current time. This is
@@ -19,11 +21,13 @@ module Mongoid
19
21
  # @example Set the created at time.
20
22
  # person.set_created_at
21
23
  def set_created_at
22
- if !created_at
24
+ if !timeless? && !created_at
23
25
  time = Time.now.utc
24
26
  self.updated_at = time if is_a?(Updated) && !updated_at_changed?
25
27
  self.created_at = time
26
28
  end
29
+
30
+ self.class.clear_timeless_option
27
31
  end
28
32
  end
29
33
  end
@@ -7,11 +7,6 @@ module Mongoid
7
7
  module Timeless
8
8
  extend ActiveSupport::Concern
9
9
 
10
- included do
11
- class_attribute :timestamping
12
- self.timestamping = true
13
- end
14
-
15
10
  # Clears out the timeless option.
16
11
  #
17
12
  # @example Clear the timeless option.
@@ -21,7 +16,7 @@ module Mongoid
21
16
  #
22
17
  # @since 3.1.4
23
18
  def clear_timeless_option
24
- self.class.timestamping = true
19
+ self.class.clear_timeless_option
25
20
  end
26
21
 
27
22
  # Begin an execution that should skip timestamping.
@@ -33,10 +28,22 @@ module Mongoid
33
28
  #
34
29
  # @since 2.3.0
35
30
  def timeless
36
- self.class.timestamping = false
31
+ self.class.timeless
37
32
  self
38
33
  end
39
34
 
35
+ def timeless?
36
+ self.class.timeless?
37
+ end
38
+
39
+ class << self
40
+
41
+ def timeless_table
42
+ Thread.current['[mongoid]:timeless'] ||= Hash.new
43
+ end
44
+ delegate :[]=, :[], to: :timeless_table
45
+ end
46
+
40
47
  private
41
48
 
42
49
  module ClassMethods
@@ -50,9 +57,24 @@ module Mongoid
50
57
  #
51
58
  # @since 2.3.0
52
59
  def timeless
53
- self.timestamping = false
60
+ counter = 0
61
+ counter += 1 if self < Mongoid::Timestamps::Created
62
+ counter += 1 if self < Mongoid::Timestamps::Updated
63
+ Timeless[name] = counter
54
64
  self
55
65
  end
66
+
67
+ def clear_timeless_option
68
+ if counter = Timeless[name]
69
+ counter -= 1
70
+ Timeless[name] = (counter == 0) ? nil : counter
71
+ end
72
+ true
73
+ end
74
+
75
+ def timeless?
76
+ !!Timeless[name]
77
+ end
56
78
  end
57
79
  end
58
80
  end
@@ -9,9 +9,11 @@ module Mongoid
9
9
  extend ActiveSupport::Concern
10
10
 
11
11
  included do
12
+ include Mongoid::Timestamps::Timeless
13
+
12
14
  field :updated_at, type: Time
13
- set_callback :create, :before, :set_updated_at, if: :able_to_set_updated_at?
14
- set_callback :update, :before, :set_updated_at, if: :able_to_set_updated_at?
15
+ set_callback :create, :before, :set_updated_at
16
+ set_callback :update, :before, :set_updated_at
15
17
  end
16
18
 
17
19
  # Update the updated_at field on the Document to the current time.
@@ -20,7 +22,11 @@ module Mongoid
20
22
  # @example Set the updated at time.
21
23
  # person.set_updated_at
22
24
  def set_updated_at
23
- self.updated_at = Time.now.utc unless updated_at_changed?
25
+ if able_to_set_updated_at?
26
+ self.updated_at = Time.now.utc unless updated_at_changed?
27
+ end
28
+
29
+ self.class.clear_timeless_option
24
30
  end
25
31
 
26
32
  # Is the updated timestamp able to be set?
@@ -32,7 +38,7 @@ module Mongoid
32
38
  #
33
39
  # @since 2.4.0
34
40
  def able_to_set_updated_at?
35
- !frozen? && timestamping? && (new_record? || changed?)
41
+ !frozen? && !timeless? && (new_record? || changed?)
36
42
  end
37
43
  end
38
44
  end
@@ -1,4 +1,4 @@
1
1
  # encoding: utf-8
2
2
  module Mongoid
3
- VERSION = "3.1.5"
3
+ VERSION = "3.1.6"
4
4
  end
@@ -4,5 +4,6 @@ class Definition
4
4
  field :p, as: :part, type: String
5
5
  field :regular, type: Boolean
6
6
  field :syn, as: :synonyms, localize: true, type: String
7
+ field :active, type: Boolean, localize: true, default: true
7
8
  embedded_in :word
8
9
  end
@@ -1500,6 +1500,26 @@ describe Mongoid::Contextual::Mongo do
1500
1500
  described_class.new(criteria)
1501
1501
  end
1502
1502
 
1503
+ context "when adding an element to a HABTM set" do
1504
+
1505
+ let(:person) do
1506
+ Person.create
1507
+ end
1508
+
1509
+ let(:preference) do
1510
+ Preference.create
1511
+ end
1512
+
1513
+ before do
1514
+ Person.where(id: person.id).
1515
+ update("$addToSet" => { preference_ids: preference.id })
1516
+ end
1517
+
1518
+ it "adds a single element to the array" do
1519
+ expect(person.reload.preference_ids).to eq([ preference.id ])
1520
+ end
1521
+ end
1522
+
1503
1523
  context "when providing attributes" do
1504
1524
 
1505
1525
  context "when the attributes are of the correct type" do
@@ -1250,4 +1250,18 @@ describe Mongoid::Fields do
1250
1250
  end
1251
1251
  end
1252
1252
  end
1253
+
1254
+ context "when a localized field is a boolean" do
1255
+
1256
+ context "when the default is true" do
1257
+
1258
+ let(:definition) do
1259
+ Definition.new
1260
+ end
1261
+
1262
+ it "returns the proper predicate result" do
1263
+ expect(definition).to be_active
1264
+ end
1265
+ end
1266
+ end
1253
1267
  end
@@ -100,6 +100,13 @@ describe Mongoid::Hierarchy do
100
100
  end
101
101
  end
102
102
 
103
+ describe "#inherited" do
104
+
105
+ it "duplicates the localized fields" do
106
+ expect(Actress.localized_fields).to_not equal(Actor.localized_fields)
107
+ end
108
+ end
109
+
103
110
  describe "#parentize" do
104
111
 
105
112
  let(:address) do
@@ -18,6 +18,25 @@ describe Mongoid::Relations::Referenced::Many do
18
18
 
19
19
  describe "##{method}" do
20
20
 
21
+ context "when providing the base class in child contructor" do
22
+
23
+ let(:person) do
24
+ Person.create
25
+ end
26
+
27
+ let!(:post) do
28
+ person.posts.send(method, Post.new(person: person))
29
+ end
30
+
31
+ it "only adds the relation once" do
32
+ expect(person.posts.size).to eq(1)
33
+ end
34
+
35
+ it "only persists the relation once" do
36
+ expect(person.reload.posts.size).to eq(1)
37
+ end
38
+ end
39
+
21
40
  context "when the relations are not polymorphic" do
22
41
 
23
42
  context "when the parent is a new record" do
@@ -81,7 +81,7 @@ describe Mongoid::Timestamps::Timeless do
81
81
  end
82
82
 
83
83
  it "clears out the timeless option after save" do
84
- expect(document).to be_timestamping
84
+ expect(document).to_not be_timeless
85
85
  end
86
86
 
87
87
  context "when subsequently persisting" do
@@ -111,7 +111,7 @@ describe Mongoid::Timestamps::Timeless do
111
111
  end
112
112
 
113
113
  it "clears out the timeless option after save" do
114
- expect(document).to be_timestamping
114
+ expect(document).to_not be_timeless
115
115
  end
116
116
 
117
117
  context "when subsequently persisting" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.1.5
4
+ version: 3.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Durran Jordan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-09-18 00:00:00.000000000 Z
11
+ date: 2013-12-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -774,7 +774,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
774
774
  version: 1.3.6
775
775
  requirements: []
776
776
  rubyforge_project: mongoid
777
- rubygems_version: 2.0.6
777
+ rubygems_version: 2.1.11
778
778
  signing_key:
779
779
  specification_version: 4
780
780
  summary: Elegant Persistance in Ruby for MongoDB.