mongoid 1.2.12 → 1.2.13

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.2.12
1
+ 1.2.13
@@ -61,6 +61,7 @@ require "mongoid/matchers"
61
61
  require "mongoid/memoization"
62
62
  require "mongoid/named_scope"
63
63
  require "mongoid/scope"
64
+ require "mongoid/state"
64
65
  require "mongoid/timestamps"
65
66
  require "mongoid/versioning"
66
67
  require "mongoid/components"
@@ -9,7 +9,7 @@ module Mongoid #:nodoc:
9
9
  #
10
10
  # doc: A new +Document+ that is going to be deleted.
11
11
  def self.execute(doc)
12
- delete(doc)
12
+ doc.destroyed = true if delete(doc)
13
13
  end
14
14
  end
15
15
  end
@@ -11,7 +11,7 @@ module Mongoid #:nodoc:
11
11
  # doc: A new +Document+ that is going to be destroyed.
12
12
  def self.execute(doc)
13
13
  doc.run_callbacks :before_destroy
14
- delete(doc)
14
+ doc.destroyed = true if delete(doc)
15
15
  doc.run_callbacks :after_destroy
16
16
  end
17
17
  end
@@ -14,6 +14,7 @@ module Mongoid #:nodoc
14
14
  include Mongoid::Indexes
15
15
  include Mongoid::Matchers
16
16
  include Mongoid::Memoization
17
+ include Mongoid::State
17
18
  include Observable
18
19
  include Validatable
19
20
  extend Mongoid::Finders
@@ -175,23 +175,6 @@ module Mongoid #:nodoc:
175
175
  "#<#{self.class.name} _id: #{id}, #{attrs}>"
176
176
  end
177
177
 
178
- # Returns true is the +Document+ has not been persisted to the database,
179
- # false if it has. This is determined by the variable @new_record
180
- # and NOT if the object has an id.
181
- def new_record?
182
- @new_record == true
183
- end
184
-
185
- # Sets the new_record boolean - used after document is saved.
186
- def new_record=(saved)
187
- @new_record = saved
188
- end
189
-
190
- # Checks if the document has been saved to the database.
191
- def persisted?
192
- !new_record?
193
- end
194
-
195
178
  # Set the changed state of the +Document+ then notify observers that it has changed.
196
179
  #
197
180
  # Example:
@@ -0,0 +1,32 @@
1
+ # encoding: utf-8
2
+ module Mongoid #:nodoc:
3
+ module State #:nodoc:
4
+ # Returns true if the +Document+ has not been persisted to the database,
5
+ # false if it has. This is determined by the variable @new_record
6
+ # and NOT if the object has an id.
7
+ def new_record?
8
+ @new_record == true
9
+ end
10
+
11
+ # Sets the new_record boolean - used after document is saved.
12
+ def new_record=(saved)
13
+ @new_record = saved
14
+ end
15
+
16
+ # Checks if the document has been saved to the database.
17
+ def persisted?
18
+ !new_record?
19
+ end
20
+
21
+ # Returns true if the +Document+ has been succesfully destroyed, and false if it hasn't.
22
+ # This is determined by the variable @destroyed and NOT by checking the database.
23
+ def destroyed?
24
+ @destroyed == true
25
+ end
26
+
27
+ # Sets the destroyed boolean - used after document is destroyed.
28
+ def destroyed=(destroyed)
29
+ @destroyed = destroyed && true
30
+ end
31
+ end
32
+ end
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{mongoid}
8
- s.version = "1.2.12"
8
+ s.version = "1.2.13"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Durran Jordan"]
@@ -114,6 +114,7 @@ Gem::Specification.new do |s|
114
114
  "lib/mongoid/memoization.rb",
115
115
  "lib/mongoid/named_scope.rb",
116
116
  "lib/mongoid/scope.rb",
117
+ "lib/mongoid/state.rb",
117
118
  "lib/mongoid/timestamps.rb",
118
119
  "lib/mongoid/versioning.rb",
119
120
  "mongoid.gemspec",
@@ -232,6 +233,7 @@ Gem::Specification.new do |s|
232
233
  "spec/unit/mongoid/memoization_spec.rb",
233
234
  "spec/unit/mongoid/named_scope_spec.rb",
234
235
  "spec/unit/mongoid/scope_spec.rb",
236
+ "spec/unit/mongoid/state_spec.rb",
235
237
  "spec/unit/mongoid/timestamps_spec.rb",
236
238
  "spec/unit/mongoid/versioning_spec.rb",
237
239
  "spec/unit/mongoid_spec.rb"
@@ -355,6 +357,7 @@ Gem::Specification.new do |s|
355
357
  "spec/unit/mongoid/memoization_spec.rb",
356
358
  "spec/unit/mongoid/named_scope_spec.rb",
357
359
  "spec/unit/mongoid/scope_spec.rb",
360
+ "spec/unit/mongoid/state_spec.rb",
358
361
  "spec/unit/mongoid/timestamps_spec.rb",
359
362
  "spec/unit/mongoid/versioning_spec.rb",
360
363
  "spec/unit/mongoid_spec.rb"
@@ -121,6 +121,12 @@ describe Mongoid::Document do
121
121
  lambda { Person.find(@person.id) }.should raise_error
122
122
  end
123
123
 
124
+ it "marks the document as destroyed" do
125
+ @person.should_not be_destroyed
126
+ @person.destroy
127
+ @person.should be_destroyed
128
+ end
129
+
124
130
  end
125
131
 
126
132
  context "on an embedded document" do
@@ -14,6 +14,12 @@ describe Mongoid::Commands::Delete do
14
14
  Mongoid::Commands::Delete.execute(@document)
15
15
  end
16
16
 
17
+ it "sets the destroyed flag to true" do
18
+ @collection.expects(:remove).with({ :_id => @document.id }).returns(true)
19
+ @document.expects(:destroyed=).with(true)
20
+ Mongoid::Commands::Delete.execute(@document)
21
+ end
22
+
17
23
  context "when the document is embedded" do
18
24
 
19
25
  before do
@@ -27,9 +33,6 @@ describe Mongoid::Commands::Delete do
27
33
  Mongoid::Commands::Delete.execute(@address)
28
34
  @parent.addresses.should be_empty
29
35
  end
30
-
31
36
  end
32
-
33
37
  end
34
-
35
38
  end
@@ -23,6 +23,12 @@ describe Mongoid::Commands::Destroy do
23
23
  Mongoid::Commands::Destroy.execute(@document)
24
24
  end
25
25
 
26
+ it "sets the destroy flag on the document" do
27
+ @collection.expects(:remove).with({ :_id => @document.id }).returns(true)
28
+ @document.expects(:destroyed=).with(true)
29
+ Mongoid::Commands::Destroy.execute(@document)
30
+ end
31
+
26
32
  context "when the document is embedded" do
27
33
 
28
34
  before do
@@ -39,9 +45,6 @@ describe Mongoid::Commands::Destroy do
39
45
  Mongoid::Commands::Destroy.execute(@address)
40
46
  @parent.addresses.should be_empty
41
47
  end
42
-
43
48
  end
44
-
45
49
  end
46
-
47
50
  end
@@ -471,45 +471,6 @@ describe Mongoid::Document do
471
471
 
472
472
  end
473
473
 
474
- describe "#new_record?" do
475
-
476
- context "when the object has been saved" do
477
-
478
- before do
479
- @person = Person.new(:_id => "1")
480
- end
481
-
482
- it "returns false" do
483
- @person.new_record?.should be_false
484
- end
485
-
486
- end
487
-
488
- context "when the object has not been saved" do
489
-
490
- before do
491
- @person = Person.new
492
- end
493
-
494
- it "returns true" do
495
- @person.new_record?.should be_true
496
- end
497
-
498
- end
499
-
500
- end
501
-
502
- describe "#persisted?" do
503
-
504
- before do
505
- @person = Person.new
506
- end
507
-
508
- it "delegates to new_record?" do
509
- @person.persisted?.should be_false
510
- end
511
- end
512
-
513
474
  describe "#_parent" do
514
475
 
515
476
  before do
@@ -600,9 +561,7 @@ describe Mongoid::Document do
600
561
  @person.remove(@name)
601
562
  @person.name.should be_nil
602
563
  end
603
-
604
564
  end
605
-
606
565
  end
607
566
 
608
567
  describe "#_root" do
@@ -620,7 +579,6 @@ describe Mongoid::Document do
620
579
  it "returns self" do
621
580
  @person._root.should == @person
622
581
  end
623
-
624
582
  end
625
583
 
626
584
  context "when document is embedded one level" do
@@ -628,7 +586,6 @@ describe Mongoid::Document do
628
586
  it "returns the parent" do
629
587
  @phone_number._root.should == @person
630
588
  end
631
-
632
589
  end
633
590
 
634
591
  context "when document is embedded multiple levels" do
@@ -636,9 +593,7 @@ describe Mongoid::Document do
636
593
  it "returns the top level parent" do
637
594
  @country_code._root.should == @person
638
595
  end
639
-
640
596
  end
641
-
642
597
  end
643
598
 
644
599
  describe ".store_in" do
@@ -0,0 +1,83 @@
1
+ require "spec_helper"
2
+
3
+ describe Mongoid::State do
4
+
5
+ describe "#new_record?" do
6
+
7
+ context "when the object has been saved" do
8
+
9
+ before do
10
+ @person = Person.new(:_id => "1")
11
+ end
12
+
13
+ it "returns false" do
14
+ @person.new_record?.should be_false
15
+ end
16
+
17
+ end
18
+
19
+ context "when the object has not been saved" do
20
+
21
+ before do
22
+ @person = Person.new
23
+ end
24
+
25
+ it "returns true" do
26
+ @person.new_record?.should be_true
27
+ end
28
+
29
+ end
30
+
31
+ end
32
+
33
+ describe "#persisted?" do
34
+
35
+ before do
36
+ @person = Person.new
37
+ end
38
+
39
+ it "delegates to new_record?" do
40
+ @person.persisted?.should be_false
41
+ end
42
+ end
43
+
44
+ describe "destroyed?" do
45
+
46
+ before do
47
+ @person = Person.new
48
+ end
49
+
50
+ context "when destroyed is true" do
51
+
52
+ before do
53
+ @person.destroyed = true
54
+ end
55
+
56
+ it "returns true" do
57
+ @person.destroyed?.should be_true
58
+ end
59
+ end
60
+
61
+ context "when destroyed is false" do
62
+
63
+ before do
64
+ @person.destroyed = false
65
+ end
66
+
67
+ it "returns true" do
68
+ @person.destroyed?.should be_false
69
+ end
70
+ end
71
+
72
+ context "when destroyed is nil" do
73
+
74
+ before do
75
+ @person.destroyed = nil
76
+ end
77
+
78
+ it "returns false" do
79
+ @person.destroyed?.should be_false
80
+ end
81
+ end
82
+ end
83
+ end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 1
7
7
  - 2
8
- - 12
9
- version: 1.2.12
8
+ - 13
9
+ version: 1.2.13
10
10
  platform: ruby
11
11
  authors:
12
12
  - Durran Jordan
@@ -208,6 +208,7 @@ files:
208
208
  - lib/mongoid/memoization.rb
209
209
  - lib/mongoid/named_scope.rb
210
210
  - lib/mongoid/scope.rb
211
+ - lib/mongoid/state.rb
211
212
  - lib/mongoid/timestamps.rb
212
213
  - lib/mongoid/versioning.rb
213
214
  - mongoid.gemspec
@@ -326,6 +327,7 @@ files:
326
327
  - spec/unit/mongoid/memoization_spec.rb
327
328
  - spec/unit/mongoid/named_scope_spec.rb
328
329
  - spec/unit/mongoid/scope_spec.rb
330
+ - spec/unit/mongoid/state_spec.rb
329
331
  - spec/unit/mongoid/timestamps_spec.rb
330
332
  - spec/unit/mongoid/versioning_spec.rb
331
333
  - spec/unit/mongoid_spec.rb
@@ -473,6 +475,7 @@ test_files:
473
475
  - spec/unit/mongoid/memoization_spec.rb
474
476
  - spec/unit/mongoid/named_scope_spec.rb
475
477
  - spec/unit/mongoid/scope_spec.rb
478
+ - spec/unit/mongoid/state_spec.rb
476
479
  - spec/unit/mongoid/timestamps_spec.rb
477
480
  - spec/unit/mongoid/versioning_spec.rb
478
481
  - spec/unit/mongoid_spec.rb