mongoid 0.10.4 → 0.10.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/HISTORY CHANGED
@@ -1,3 +1,11 @@
1
+ === 0.10.5
2
+
3
+ - Documents that are embedded not properly respond to
4
+ Document#destroy and Document#delete.
5
+
6
+ - Documents can now be saved sans validation with
7
+ Document#save(false)
8
+
1
9
  === 0.10.4
2
10
 
3
11
  - Documents no longer inherit from Mongoid::Document.
@@ -15,4 +23,3 @@
15
23
  now set the name of the database collection to persist to.
16
24
 
17
25
  - Mongoid::Criteria#select becomes Mongoid::Criteria#only
18
-
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.10.4
1
+ 0.10.5
@@ -1,5 +1,6 @@
1
1
  # encoding: utf-8
2
2
  require "mongoid/commands/create"
3
+ require "mongoid/commands/deletion"
3
4
  require "mongoid/commands/delete"
4
5
  require "mongoid/commands/delete_all"
5
6
  require "mongoid/commands/destroy"
@@ -46,17 +47,17 @@ module Mongoid #:nodoc:
46
47
  end
47
48
 
48
49
  # Save the +Document+. Delegates to the Save command.
49
- def save
50
- new_record? ? Create.execute(self) : Save.execute(self)
50
+ def save(validate = true)
51
+ new_record? ? Create.execute(self, validate) : Save.execute(self, validate)
51
52
  end
52
53
 
53
54
  # Save the +Document+. Delegates to the Save command. If the command
54
55
  # returns false then a +ValidationError+ will be raised.
55
56
  def save!
56
57
  if new_record?
57
- return Create.execute(self) || (raise Errors::Validations.new(self.errors))
58
+ return Create.execute(self, true) || (raise Errors::Validations.new(self.errors))
58
59
  else
59
- return Save.execute(self) || (raise Errors::Validations.new(self.errors))
60
+ return Save.execute(self, true) || (raise Errors::Validations.new(self.errors))
60
61
  end
61
62
  end
62
63
 
@@ -10,9 +10,9 @@ module Mongoid #:nodoc:
10
10
  # doc: A new +Document+ that is going to be persisted.
11
11
  #
12
12
  # Returns: +Document+.
13
- def self.execute(doc)
13
+ def self.execute(doc, validate = true)
14
14
  doc.run_callbacks :before_create
15
- Save.execute(doc)
15
+ Save.execute(doc, validate)
16
16
  doc.run_callbacks :after_create
17
17
  return doc
18
18
  end
@@ -2,13 +2,14 @@
2
2
  module Mongoid #:nodoc:
3
3
  module Commands
4
4
  class Delete
5
+ extend Deletion
5
6
  # Performs a delete of the supplied +Document+ without any callbacks.
6
7
  #
7
8
  # Options:
8
9
  #
9
10
  # doc: A new +Document+ that is going to be deleted.
10
11
  def self.execute(doc)
11
- doc.collection.remove(:_id => doc.id)
12
+ delete(doc)
12
13
  end
13
14
  end
14
15
  end
@@ -0,0 +1,12 @@
1
+ module Mongoid #:nodoc
2
+ module Commands #:nodoc
3
+ module Deletion #:nodoc
4
+ # If the +Document+ has a parent, delete it from the parent's attributes,
5
+ # otherwise delete it from it's collection.
6
+ def delete(doc)
7
+ parent = doc.parent
8
+ parent ? parent.remove(doc) : doc.collection.remove(:_id => doc.id)
9
+ end
10
+ end
11
+ end
12
+ end
@@ -2,6 +2,7 @@
2
2
  module Mongoid #:nodoc:
3
3
  module Commands
4
4
  class Destroy
5
+ extend Deletion
5
6
  # Performs a destroy of the supplied +Document+, with the necessary
6
7
  # callbacks. It then deletes the record from the collection.
7
8
  #
@@ -10,7 +11,7 @@ module Mongoid #:nodoc:
10
11
  # doc: A new +Document+ that is going to be destroyed.
11
12
  def self.execute(doc)
12
13
  doc.run_callbacks :before_destroy
13
- doc.collection.remove(:_id => doc.id)
14
+ delete(doc)
14
15
  doc.run_callbacks :after_destroy
15
16
  end
16
17
  end
@@ -10,8 +10,8 @@ module Mongoid #:nodoc:
10
10
  # doc: A +Document+ that is going to be persisted.
11
11
  #
12
12
  # Returns: +Document+ if validation passes, +false+ if not.
13
- def self.execute(doc)
14
- return false unless doc.valid?
13
+ def self.execute(doc, validate = true)
14
+ return false if validate && !doc.valid?
15
15
  doc.run_callbacks :before_save
16
16
  parent = doc.parent
17
17
  doc.new_record = false
@@ -253,6 +253,15 @@ module Mongoid #:nodoc:
253
253
  @attributes = collection.find_one(:_id => id).with_indifferent_access
254
254
  end
255
255
 
256
+ # Remove a child document from this parent +Document+. Will reset the
257
+ # memoized association and notify the parent of the change.
258
+ def remove(child)
259
+ name = child.association_name
260
+ @attributes.remove(name, child.attributes)
261
+ remove_instance_variable("@#{name}")
262
+ notify
263
+ end
264
+
256
265
  # Return the root +Document+ in the object graph. If the current +Document+
257
266
  # is the root object in the graph it will return self.
258
267
  def root
@@ -3,6 +3,21 @@ module Mongoid #:nodoc:
3
3
  module Extensions #:nodoc:
4
4
  module Hash #:nodoc:
5
5
  module Accessors #:nodoc:
6
+
7
+ # Remove a set of attributes from a hash. If the attributes are
8
+ # contained in an array it will remove it from the array, otherwise it
9
+ # will delete the child attribute completely.
10
+ def remove(key, attrs)
11
+ elements = self[key]
12
+ if elements
13
+ key.singular? ? self[key] = nil : elements.delete(attrs)
14
+ end
15
+ end
16
+
17
+ # Inserts new attributes into the hash. If the elements are present in
18
+ # the hash it will update them, otherwise it will add the new
19
+ # attributes into the hash as either a child hash or child array of
20
+ # hashes.
6
21
  def insert(key, attrs)
7
22
  elements = self[key]
8
23
  if elements
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{mongoid}
8
- s.version = "0.10.4"
8
+ s.version = "0.10.5"
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"]
@@ -37,6 +37,7 @@ Gem::Specification.new do |s|
37
37
  "lib/mongoid/commands/create.rb",
38
38
  "lib/mongoid/commands/delete.rb",
39
39
  "lib/mongoid/commands/delete_all.rb",
40
+ "lib/mongoid/commands/deletion.rb",
40
41
  "lib/mongoid/commands/destroy.rb",
41
42
  "lib/mongoid/commands/destroy_all.rb",
42
43
  "lib/mongoid/commands/save.rb",
@@ -257,6 +257,30 @@ describe Mongoid::Document do
257
257
 
258
258
  end
259
259
 
260
+ context "without validation" do
261
+
262
+ before do
263
+ @comment = Comment.new
264
+ end
265
+
266
+ it "always persists" do
267
+ @comment.save(false).should be_true
268
+ end
269
+
270
+ end
271
+
272
+ context "with failing validation" do
273
+
274
+ before do
275
+ @comment = Comment.new
276
+ end
277
+
278
+ it "returns false" do
279
+ @comment.save.should_not be_valid
280
+ end
281
+
282
+ end
283
+
260
284
  end
261
285
 
262
286
  context "when has many exists through a has one" do
@@ -9,19 +9,19 @@ describe Mongoid::Commands::Create do
9
9
  end
10
10
 
11
11
  it "executes a save command" do
12
- Mongoid::Commands::Save.expects(:execute).with(@document).returns(@document)
12
+ Mongoid::Commands::Save.expects(:execute).with(@document, true).returns(@document)
13
13
  Mongoid::Commands::Create.execute(@document)
14
14
  end
15
15
 
16
16
  it "runs the before and after create callbacks" do
17
17
  @document.expects(:run_callbacks).with(:before_create)
18
- Mongoid::Commands::Save.expects(:execute).with(@document).returns(@document)
18
+ Mongoid::Commands::Save.expects(:execute).with(@document, true).returns(@document)
19
19
  @document.expects(:run_callbacks).with(:after_create)
20
20
  Mongoid::Commands::Create.execute(@document)
21
21
  end
22
22
 
23
23
  it "returns the document" do
24
- Mongoid::Commands::Save.expects(:execute).with(@document).returns(@document)
24
+ Mongoid::Commands::Save.expects(:execute).with(@document, true).returns(@document)
25
25
  Mongoid::Commands::Create.execute(@document).should == @document
26
26
  end
27
27
 
@@ -6,7 +6,7 @@ describe Mongoid::Commands::Delete do
6
6
 
7
7
  before do
8
8
  @collection = mock
9
- @document = stub(:collection => @collection, :id => "1")
9
+ @document = stub(:collection => @collection, :id => "1", :parent => false)
10
10
  end
11
11
 
12
12
  it "removes the document from its collection" do
@@ -14,6 +14,22 @@ describe Mongoid::Commands::Delete do
14
14
  Mongoid::Commands::Delete.execute(@document)
15
15
  end
16
16
 
17
+ context "when the document is embedded" do
18
+
19
+ before do
20
+ @parent = Person.new
21
+ @address = Address.new(:street => "Genoa Pl")
22
+ @parent.addresses << @address
23
+ end
24
+
25
+ it "removes the document from the parent attributes" do
26
+ @parent.addresses.should == [@address]
27
+ Mongoid::Commands::Delete.execute(@address)
28
+ @parent.addresses.should be_empty
29
+ end
30
+
31
+ end
32
+
17
33
  end
18
34
 
19
35
  end
@@ -8,7 +8,8 @@ describe Mongoid::Commands::Destroy do
8
8
  @collection = stub_everything
9
9
  @document = stub(:run_callbacks => true,
10
10
  :collection => @collection,
11
- :id => "1")
11
+ :id => "1",
12
+ :parent => false)
12
13
  end
13
14
 
14
15
  it "runs the before and after destroy callbacks" do
@@ -22,6 +23,22 @@ describe Mongoid::Commands::Destroy do
22
23
  Mongoid::Commands::Destroy.execute(@document)
23
24
  end
24
25
 
26
+ context "when the document is embedded" do
27
+
28
+ before do
29
+ @parent = Person.new
30
+ @address = Address.new(:street => "Genoa Pl")
31
+ @parent.addresses << @address
32
+ end
33
+
34
+ it "removes the document from the parent attributes" do
35
+ @parent.addresses.should == [@address]
36
+ Mongoid::Commands::Destroy.execute(@address)
37
+ @parent.addresses.should be_empty
38
+ end
39
+
40
+ end
41
+
25
42
  end
26
43
 
27
44
  end
@@ -72,6 +72,18 @@ describe Mongoid::Commands::Save do
72
72
 
73
73
  end
74
74
 
75
+ context "when saving without validation" do
76
+
77
+ before do
78
+ @document.stubs(:valid?).returns(false)
79
+ end
80
+
81
+ it "ignores validation and returns true" do
82
+ Mongoid::Commands::Save.execute(@document, false).should be_true
83
+ end
84
+
85
+ end
86
+
75
87
  end
76
88
 
77
89
  context "when the document is embedded" do
@@ -27,7 +27,7 @@ describe Mongoid::Commands do
27
27
  describe "#save" do
28
28
 
29
29
  it "delegates to the Save command" do
30
- Mongoid::Commands::Save.expects(:execute).with(@person).returns(true)
30
+ Mongoid::Commands::Save.expects(:execute).with(@person, true).returns(true)
31
31
  @person.save
32
32
  end
33
33
 
@@ -38,12 +38,25 @@ describe Mongoid::Commands do
38
38
  end
39
39
 
40
40
  it "delegates to the Create command" do
41
- Mongoid::Commands::Create.expects(:execute).with(@person).returns(@person)
41
+ Mongoid::Commands::Create.expects(:execute).with(@person, true).returns(true)
42
42
  @person.save
43
43
  end
44
44
 
45
45
  end
46
46
 
47
+ context "when not validating" do
48
+
49
+ before do
50
+ @person = Person.new
51
+ end
52
+
53
+ it "passes the validate param to the command" do
54
+ Mongoid::Commands::Create.expects(:execute).with(@person, false).returns(true)
55
+ @person.save(false)
56
+ end
57
+
58
+ end
59
+
47
60
  end
48
61
 
49
62
  describe "#save!" do
@@ -51,8 +64,8 @@ describe Mongoid::Commands do
51
64
  context "when validation passes" do
52
65
 
53
66
  it "it returns the person" do
54
- Mongoid::Commands::Save.expects(:execute).with(@person).returns(@person)
55
- @person.save!.should == @person
67
+ Mongoid::Commands::Save.expects(:execute).with(@person, true).returns(true)
68
+ @person.save!
56
69
  end
57
70
 
58
71
  end
@@ -60,7 +73,7 @@ describe Mongoid::Commands do
60
73
  context "when validation fails" do
61
74
 
62
75
  it "it raises an error" do
63
- Mongoid::Commands::Save.expects(:execute).with(@person).returns(false)
76
+ Mongoid::Commands::Save.expects(:execute).with(@person, true).returns(false)
64
77
  lambda { @person.save! }.should raise_error
65
78
  end
66
79
 
@@ -73,14 +86,14 @@ describe Mongoid::Commands do
73
86
  end
74
87
 
75
88
  it "delegates to the Create command" do
76
- Mongoid::Commands::Create.expects(:execute).with(@person).returns(@person)
89
+ Mongoid::Commands::Create.expects(:execute).with(@person, true).returns(true)
77
90
  @person.save!
78
91
  end
79
92
 
80
93
  context "when validation fails" do
81
94
 
82
95
  it "it raises an error " do
83
- Mongoid::Commands::Create.expects(:execute).with(@person).returns(false)
96
+ Mongoid::Commands::Create.expects(:execute).with(@person, true).returns(false)
84
97
  lambda { @person.save! }.should raise_error
85
98
  end
86
99
 
@@ -93,7 +106,7 @@ describe Mongoid::Commands do
93
106
  describe "#update_attributes" do
94
107
 
95
108
  it "delegates to the Save command" do
96
- Mongoid::Commands::Save.expects(:execute).with(@person).returns(@person)
109
+ Mongoid::Commands::Save.expects(:execute).with(@person, true).returns(true)
97
110
  @person.update_attributes({})
98
111
  end
99
112
 
@@ -104,8 +117,8 @@ describe Mongoid::Commands do
104
117
  context "when validation passes" do
105
118
 
106
119
  it "it returns the person" do
107
- Mongoid::Commands::Save.expects(:execute).with(@person).returns(@person)
108
- @person.update_attributes({}).should == @person
120
+ Mongoid::Commands::Save.expects(:execute).with(@person, true).returns(true)
121
+ @person.update_attributes({}).should be_true
109
122
  end
110
123
 
111
124
  end
@@ -113,7 +126,7 @@ describe Mongoid::Commands do
113
126
  context "when validation fails" do
114
127
 
115
128
  it "it raises an error" do
116
- Mongoid::Commands::Save.expects(:execute).with(@person).returns(false)
129
+ Mongoid::Commands::Save.expects(:execute).with(@person, true).returns(false)
117
130
  lambda { @person.update_attributes!({}) }.should raise_error
118
131
  end
119
132
 
@@ -504,6 +504,40 @@ describe Mongoid::Document do
504
504
 
505
505
  end
506
506
 
507
+ describe "#remove" do
508
+
509
+ context "when removing an element from a has many" do
510
+
511
+ before do
512
+ @person = Person.new
513
+ @address = Address.new(:street => "Testing")
514
+ @person.addresses << @address
515
+ end
516
+
517
+ it "removes the child document attributes" do
518
+ @person.remove(@address)
519
+ @person.addresses.size.should == 0
520
+ end
521
+
522
+ end
523
+
524
+ context "when removing a has one" do
525
+
526
+ before do
527
+ @person = Person.new
528
+ @name = Name.new(:first_name => "Neytiri")
529
+ @person.name = @name
530
+ end
531
+
532
+ it "removes the child document attributes" do
533
+ @person.remove(@name)
534
+ @person.name.document.should be_nil
535
+ end
536
+
537
+ end
538
+
539
+ end
540
+
507
541
  describe "#root" do
508
542
 
509
543
  before do
@@ -2,27 +2,73 @@ require "spec_helper"
2
2
 
3
3
  describe Mongoid::Extensions::Hash::Accessors do
4
4
 
5
- describe "#insert" do
5
+ before do
6
+ @hash = {
7
+ :_id => 1,
8
+ :title => "value",
9
+ :name => {
10
+ :_id => 2, :first_name => "Test", :last_name => "User"
11
+ },
12
+ :pet => {
13
+ :_id => 6,
14
+ :name => "Fido",
15
+ :vet_visits => [ { :date => Date.new(2007, 1, 1) } ]
16
+ },
17
+ :addresses => [
18
+ { :_id => 3, :street => "First Street" },
19
+ { :_id => 4, :street => "Second Street" }
20
+ ]
21
+ }
22
+ end
23
+
24
+ describe "#remove" do
25
+
26
+ context "when removing from an array" do
27
+
28
+ context "when the elements exist" do
29
+
30
+ it "removes the element from the array" do
31
+ @hash.remove(:addresses, { :_id => 3, :street => "First Street" })
32
+ @hash[:addresses].size.should == 1
33
+ end
34
+
35
+ end
36
+
37
+ context "when the elements do not exist" do
38
+
39
+ it "does not raise an error" do
40
+ @hash.remove(:aliases, { :_id => 3, :street => "First Street" })
41
+ end
42
+
43
+ end
6
44
 
7
- before do
8
- @hash = {
9
- :_id => 1,
10
- :title => "value",
11
- :name => {
12
- :_id => 2, :first_name => "Test", :last_name => "User"
13
- },
14
- :pet => {
15
- :_id => 6,
16
- :name => "Fido",
17
- :vet_visits => [ { :date => Date.new(2007, 1, 1) } ]
18
- },
19
- :addresses => [
20
- { :_id => 3, :street => "First Street" },
21
- { :_id => 4, :street => "Second Street" }
22
- ]
23
- }
24
45
  end
25
46
 
47
+ context "when removing a single object" do
48
+
49
+ context "when the element exists" do
50
+
51
+ it "removes the element from the array" do
52
+ @hash.remove(:name, { :_id => 2, :first_name => "Test", :last_name => "User" })
53
+ @hash[:name].should be_nil
54
+ end
55
+
56
+ end
57
+
58
+ context "when the element does not exist" do
59
+
60
+ it "does not raise an error" do
61
+ @hash.remove(:alias, { :_id => 3, :street => "First Street" })
62
+ end
63
+
64
+ end
65
+
66
+ end
67
+
68
+ end
69
+
70
+ describe "#insert" do
71
+
26
72
  context "when writing to a hash with a child array" do
27
73
 
28
74
  context "when child attributes not present" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.4
4
+ version: 0.10.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Durran Jordan
@@ -113,6 +113,7 @@ files:
113
113
  - lib/mongoid/commands/create.rb
114
114
  - lib/mongoid/commands/delete.rb
115
115
  - lib/mongoid/commands/delete_all.rb
116
+ - lib/mongoid/commands/deletion.rb
116
117
  - lib/mongoid/commands/destroy.rb
117
118
  - lib/mongoid/commands/destroy_all.rb
118
119
  - lib/mongoid/commands/save.rb