mongoid_delorean 1.2.0 → 1.2.1

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: 6046b3012b6e28a626ce3601fa23e58298e9fe91
4
- data.tar.gz: 4ec10da2a928268a7327040ad719b230a338f796
3
+ metadata.gz: d7df0efa1af0ba01ca6bbb2b5625d0d03e3637d5
4
+ data.tar.gz: d781202b5b614f3e911921a5cee6355f3be06c54
5
5
  SHA512:
6
- metadata.gz: 8786110df221df1adcd8ca77c475c4a556f8e9245ebc5ff547f3db18083d040022fef15f38d338a1bf5110ef10691c5d45989c74060c3b017c91206b2af1d361
7
- data.tar.gz: d8e501f26a7b05e8c6af845badc918b9b52fa6dbc9ccc64bb4ef142c64a2e859a2291732a2f0e65bc3a672492b72a47437711c4f3cbc5d0c10ee2fa670a6c36b
6
+ metadata.gz: 5f58c034a757c4f02f6ada2e5a6cf56b516352dbfb544c92eae6a7f31b7d4f93c21dcb853b94e56c54f3718992d71654bc5c976b17b264ab41520248447dd5ba
7
+ data.tar.gz: 96ac147ef9a1261f5aeacbbff1cc856f6fde19d28e0a2594a4aa23dc70ded5b72dc80bcfc3688b1b6bf44f3a9f2161a62bc3a8e3a6c1d292b67e9d80edaead9c
@@ -6,6 +6,7 @@ module Mongoid
6
6
  super
7
7
  klass.field :version, type: Integer, default: 0
8
8
  klass.before_save :save_version
9
+ klass.after_save :after_save_version
9
10
  klass.send(:include, Mongoid::Delorean::Trackable::CommonInstanceMethods)
10
11
  end
11
12
 
@@ -23,12 +24,22 @@ module Mongoid
23
24
  _changes = self.changes_with_relations.dup
24
25
  _changes.merge!("version" => [self.version_was, _version])
25
26
 
26
- Mongoid::Delorean::History.create(original_class: self.class.name, original_class_id: self.id, version: _version, altered_attributes: _changes, full_attributes: _attributes).inspect
27
+ Mongoid::Delorean::History.create(original_class: self.class.name, original_class_id: self.id, version: _version, altered_attributes: _changes, full_attributes: _attributes)
27
28
  self.without_history_tracking do
28
29
  self.version = _version
29
- self.save!
30
+ unless(self.new_record?)
31
+ self.set(:version, _version)
32
+ end
30
33
  end
34
+
35
+ @__track_changes = false
31
36
  end
37
+
38
+ true
39
+ end
40
+
41
+ def after_save_version
42
+ @__track_changes = Mongoid::Delorean.config.track_history
32
43
  end
33
44
 
34
45
  def track_history?
@@ -36,9 +47,10 @@ module Mongoid
36
47
  end
37
48
 
38
49
  def without_history_tracking
50
+ previous_track_change = @__track_changes
39
51
  @__track_changes = false
40
52
  yield
41
- @__track_changes = Mongoid::Delorean.config.track_history
53
+ @__track_changes = previous_track_change
42
54
  end
43
55
 
44
56
  def revert!(version = (self.version - 1))
@@ -54,7 +66,17 @@ module Mongoid
54
66
  module CommonEmbeddedMethods
55
67
 
56
68
  def save_version
57
- self._parent.save_version if self._parent.respond_to?(:save_version)
69
+ if self._parent.respond_to?(:save_version)
70
+ if self._parent.respond_to?(:track_history?)
71
+ if self._parent.track_history?
72
+ self._parent.save_version
73
+ end
74
+ else
75
+ self._parent.save_version
76
+ end
77
+ end
78
+
79
+ true
58
80
  end
59
81
 
60
82
  end
@@ -1,5 +1,5 @@
1
1
  module Mongoid
2
2
  module Delorean
3
- VERSION = "1.2.0"
3
+ VERSION = "1.2.1"
4
4
  end
5
5
  end
data/spec/config.yml CHANGED
@@ -3,4 +3,9 @@ test:
3
3
  default:
4
4
  database: mongoid_delorean_test
5
5
  hosts:
6
- - localhost:27017
6
+ - localhost:27017
7
+ options:
8
+ safe: true
9
+ consistency: :strong
10
+ max_retries: 1
11
+ retry_interval: 0
@@ -64,6 +64,14 @@ describe Mongoid::Delorean::Trackable do
64
64
  version.full_attributes.except("created_at", "updated_at").should eql({"_id"=>u.id, "version"=>2, "name"=>"Mark", "age"=>36})
65
65
  end
66
66
 
67
+ it "passes validate options to save" do
68
+ u = User.create!(email: "test@example.com")
69
+
70
+ u.email = "invalid"
71
+ expect { u.save! }.to raise_error
72
+ expect { u.save!(validate: false) }.to_not raise_error
73
+ end
74
+
67
75
  describe "#without_history_tracking" do
68
76
 
69
77
  it "it doesn't track the history of the save" do
@@ -163,12 +171,12 @@ describe Mongoid::Delorean::Trackable do
163
171
  a = Article.create!(name: "My Article")
164
172
 
165
173
  version = a.versions.first
166
- version.full_attributes.should eql({"_id"=>a.id, "version"=>1, "name"=>"My Article", "pages"=>[]})
174
+ version.full_attributes.should eql({"_id"=>a.id, "version"=>1, "name"=>"My Article", "pages"=>[], "authors" => []})
167
175
 
168
176
  a.update_attributes(summary: "Summary about the article")
169
177
 
170
178
  version = a.versions.last
171
- version.full_attributes.except("created_at", "updated_at").should eql({"_id"=>a.id, "version"=>2, "name"=>"My Article", "summary"=>"Summary about the article", "pages"=>[]})
179
+ version.full_attributes.except("created_at", "updated_at").should eql({"_id"=>a.id, "version"=>2, "name"=>"My Article", "summary"=>"Summary about the article", "pages"=>[], "authors" => []})
172
180
  end
173
181
 
174
182
  it "tracks the full set of attributes including embeds at the time of saving" do
@@ -177,12 +185,12 @@ describe Mongoid::Delorean::Trackable do
177
185
  a.save!
178
186
 
179
187
  version = a.versions.first
180
- version.full_attributes.should eql({"_id"=>a.id, "version"=>1, "name"=>"My Article", "pages"=>[{"_id"=>page.id, "name"=>"Page 1", "sections"=>[]}]})
188
+ version.full_attributes.should eql({"_id"=>a.id, "version"=>1, "name"=>"My Article", "pages"=>[{"_id"=>page.id, "name"=>"Page 1", "sections"=>[]}], "authors" => []})
181
189
 
182
190
  a.update_attributes(summary: "Summary about the article")
183
191
 
184
192
  version = a.versions.last
185
- version.full_attributes.except("created_at", "updated_at").should eql({"_id"=>a.id, "version"=>2, "name"=>"My Article", "pages"=>[{"_id"=>page.id, "name"=>"Page 1", "sections"=>[]}], "summary"=>"Summary about the article"})
193
+ version.full_attributes.except("created_at", "updated_at").should eql({"_id"=>a.id, "version"=>2, "name"=>"My Article", "pages"=>[{"_id"=>page.id, "name"=>"Page 1", "sections"=>[]}], "summary"=>"Summary about the article", "authors" => []})
186
194
  end
187
195
 
188
196
  it "tracks changes when an embedded document is saved" do
@@ -198,6 +206,70 @@ describe Mongoid::Delorean::Trackable do
198
206
  page.name.should eql("The 1st Page")
199
207
  end
200
208
 
209
+ it "handles embeds with cascade callbacks" do
210
+ a = Article.new(name: "Article 1")
211
+ a.authors.build(name: "John Doe")
212
+ a.authors.build(name: "Jane Doe")
213
+ a.authors.last.influences.build(name: "Poe")
214
+ a.authors.last.influences.build(name: "Twain")
215
+
216
+ a.save!
217
+ a.version.should eql(1)
218
+
219
+ a.authors.first.name = "Joe Blow"
220
+ a.save!
221
+ a.reload
222
+ a.version.should eql(2)
223
+ end
224
+
225
+ it "saves parent versions when saving embedded documents multiple levels deep" do
226
+ a = Article.new(name: "Article 1")
227
+ page = a.pages.build(name: "Page 1")
228
+ section = page.sections.build(body: "some body text")
229
+
230
+ a.save!
231
+ a.version.should eql(1)
232
+
233
+ a.reload
234
+ section = a.pages.first.sections.first
235
+ section.body = "updated body text"
236
+ section.save!
237
+
238
+ a.reload
239
+ a.version.should eql(2)
240
+ end
241
+
242
+ it "doesn't force validations on the parent document when an embedded document is saved" do
243
+ a = Article.new(name: "Article 1", publish_year: -20)
244
+ page = a.pages.build(name: "Page 1")
245
+
246
+ expect { a.save! }.to raise_error
247
+ a.save!(validate: false)
248
+
249
+ a.version.should eql(1)
250
+ page.name = "Number One Page"
251
+ page.save!
252
+
253
+ a.reload
254
+ a.version.should eql(2)
255
+ page = a.pages.first
256
+ page.name.should eql("Number One Page")
257
+ end
258
+
259
+ it "doesn't save the parent document when the embedded document fails validation" do
260
+ a = Article.new(name: "Article 1")
261
+ page = a.pages.build(name: "Page 1", number: 1)
262
+ a.save!
263
+
264
+ page.number = -10
265
+ expect { page.save! }.to raise_error
266
+
267
+ a.reload
268
+ a.version.should eql(1)
269
+ page = a.pages.first
270
+ page.number.should eql(1)
271
+ end
272
+
201
273
  describe '#revert!' do
202
274
 
203
275
  it "reverts to the last version" do
@@ -15,8 +15,12 @@ class Article
15
15
 
16
16
  field :name, type: String
17
17
  field :summary, type: String
18
+ field :publish_year, type: String
18
19
 
19
20
  embeds_many :pages
21
+ embeds_many :authors, cascade_callbacks: true
22
+
23
+ validates :publish_year, numericality: { greater_than_or_equal_to: 0, allow_nil: true }
20
24
  end
21
25
 
22
26
  class Page
@@ -24,6 +28,9 @@ class Page
24
28
  include Mongoid::Timestamps
25
29
 
26
30
  field :name, type: String
31
+ field :number, type: Integer
32
+
33
+ validates :number, numericality: { greater_than_or_equal_to: 0, allow_nil: true }
27
34
 
28
35
  embedded_in :article, inverse_of: :pages
29
36
  embeds_many :sections
@@ -37,6 +44,26 @@ class Footer
37
44
  field :content, type: String
38
45
 
39
46
  embedded_in :page
47
+
48
+ end
49
+
50
+ class Author
51
+ include Mongoid::Document
52
+ include Mongoid::Timestamps
53
+
54
+ field :name, type: String
55
+
56
+ embeds_many :influences, cascade_callbacks: true
57
+ embedded_in :article, inverse_of: :authors
58
+ end
59
+
60
+ class Influence
61
+ include Mongoid::Document
62
+ include Mongoid::Timestamps
63
+
64
+ field :name, type: String
65
+
66
+ embedded_in :author
40
67
  end
41
68
 
42
69
  class User
@@ -46,4 +73,7 @@ class User
46
73
 
47
74
  field :name, type: String
48
75
  field :age, type: Integer
76
+ field :email, type: String
77
+
78
+ validates :email, format: { with: /.+@.+\..+/, allow_nil: true }
49
79
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid_delorean
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mark Bates