mongoid 1.2.11 → 1.2.12

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.11
1
+ 1.2.12
@@ -8,11 +8,11 @@ module Mongoid #:nodoc:
8
8
 
9
9
  # Appends the object to the +Array+, setting its parent in
10
10
  # the process.
11
- def <<(*objects)
12
- objects.flatten.each do |object|
13
- object.parentize(@parent, @association_name)
14
- @target << object
15
- object.notify
11
+ def <<(*documents)
12
+ documents.flatten.each do |doc|
13
+ doc.parentize(@parent, @association_name)
14
+ @target << doc
15
+ doc.notify
16
16
  end
17
17
  end
18
18
 
@@ -22,9 +22,9 @@ module Mongoid #:nodoc:
22
22
  # Clears the association, and notifies the parents of the removal.
23
23
  def clear
24
24
  unless @target.empty?
25
- object = @target.first
26
- object.changed(true)
27
- object.notify_observers(object, true)
25
+ document = @target.first
26
+ document.changed(true)
27
+ document.notify_observers(document, true)
28
28
  @target.clear
29
29
  end
30
30
  end
@@ -37,11 +37,11 @@ module Mongoid #:nodoc:
37
37
  #
38
38
  # The newly created Document.
39
39
  def build(attrs = {}, type = nil)
40
- object = type ? type.instantiate : @klass.instantiate
41
- object.parentize(@parent, @association_name)
42
- object.write_attributes(attrs)
43
- @target << object
44
- object
40
+ document = type ? type.instantiate : @klass.instantiate
41
+ document.parentize(@parent, @association_name)
42
+ document.write_attributes(attrs)
43
+ @target << document
44
+ document
45
45
  end
46
46
 
47
47
  # Creates a new Document and adds it to the association collection. The
@@ -51,13 +51,28 @@ module Mongoid #:nodoc:
51
51
  #
52
52
  # Returns:
53
53
  #
54
- # Rhe newly created Document.
54
+ # The newly created Document.
55
55
  def create(attrs = {}, type = nil)
56
- object = build(attrs, type)
57
- object.run_callbacks(:before_create)
58
- object.save
59
- object.run_callbacks(:after_create)
60
- object
56
+ document = build(attrs, type)
57
+ document.run_callbacks(:before_create)
58
+ document.run_callbacks(:after_create) if document.save
59
+ document
60
+ end
61
+
62
+ # Creates a new Document and adds it to the association collection. The
63
+ # document created will be of the same class as the others in the
64
+ # association, and the attributes will be passed into the constructor and
65
+ # the new object will then be saved. If validation fails an error will
66
+ # get raised.
67
+ #
68
+ # Returns:
69
+ #
70
+ # The newly created Document.
71
+ def create!(attrs = {}, type = nil)
72
+ document = create(attrs, type)
73
+ errors = document.errors
74
+ raise Errors::Validations.new(errors) unless errors.empty?
75
+ document
61
76
  end
62
77
 
63
78
  # Finds a document in this association.
@@ -176,7 +191,6 @@ module Mongoid #:nodoc:
176
191
  instantiate(parent, options)
177
192
  end
178
193
  end
179
-
180
194
  end
181
195
  end
182
196
  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.11"
8
+ s.version = "1.2.12"
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"]
@@ -130,6 +130,7 @@ Gem::Specification.new do |s|
130
130
  "spec/integration/mongoid/named_scope_spec.rb",
131
131
  "spec/models/address.rb",
132
132
  "spec/models/animal.rb",
133
+ "spec/models/callbacks.rb",
133
134
  "spec/models/comment.rb",
134
135
  "spec/models/country_code.rb",
135
136
  "spec/models/employer.rb",
@@ -253,6 +254,7 @@ Gem::Specification.new do |s|
253
254
  "spec/integration/mongoid/named_scope_spec.rb",
254
255
  "spec/models/address.rb",
255
256
  "spec/models/animal.rb",
257
+ "spec/models/callbacks.rb",
256
258
  "spec/models/comment.rb",
257
259
  "spec/models/country_code.rb",
258
260
  "spec/models/employer.rb",
@@ -49,6 +49,16 @@ describe Mongoid::Associations do
49
49
 
50
50
  end
51
51
 
52
+ context "creation of an embedded association on a callback" do
53
+
54
+ it "allows the use of create!" do
55
+ artist = Artist.create!(:name => "Depeche Mode")
56
+ artist.songs.size.should == 2
57
+ artist.songs.first.title.should == "0"
58
+ artist.songs.last.title.should == "1"
59
+ end
60
+ end
61
+
52
62
  context "criteria on has many embedded associations" do
53
63
 
54
64
  before do
@@ -0,0 +1,18 @@
1
+ class Artist
2
+ include Mongoid::Document
3
+ field :name
4
+ has_many :songs
5
+
6
+ after_create :create_songs
7
+
8
+ protected
9
+ def create_songs
10
+ 2.times { |n| songs.create!(:title => "#{n}") }
11
+ end
12
+ end
13
+
14
+ class Song
15
+ include Mongoid::Document
16
+ field :title
17
+ belongs_to :artist, :inverse_of => :songs
18
+ end
@@ -166,6 +166,49 @@ describe Mongoid::Associations::HasMany do
166
166
 
167
167
  end
168
168
 
169
+ describe "#create!" do
170
+
171
+ context "when validations pass" do
172
+
173
+ before do
174
+ @association = Mongoid::Associations::HasMany.new(
175
+ @document,
176
+ Mongoid::Associations::Options.new(:name => :addresses)
177
+ )
178
+ @address = mock(:parentize => true, :write_attributes => true, :errors => [])
179
+ Address.expects(:instantiate).returns(@address)
180
+ @address.expects(:run_callbacks).with(:before_create)
181
+ @address.expects(:run_callbacks).with(:after_create)
182
+ end
183
+
184
+ it "builds and saves a new object" do
185
+ @address.expects(:save).returns(true)
186
+ address = @association.create!({ :street => "Yet Another" })
187
+ address.should == @address
188
+ end
189
+ end
190
+
191
+ context "when validations fail" do
192
+
193
+ before do
194
+ @association = Mongoid::Associations::HasMany.new(
195
+ @document,
196
+ Mongoid::Associations::Options.new(:name => :addresses)
197
+ )
198
+ @address = mock(:parentize => true, :write_attributes => true, :errors => [ "test" ])
199
+ Address.expects(:instantiate).returns(@address)
200
+ @address.expects(:run_callbacks).with(:before_create)
201
+ end
202
+
203
+ it "builds and saves a new object" do
204
+ @address.expects(:save).returns(false)
205
+ lambda {
206
+ @association.create!({ :street => "Yet Another" })
207
+ }.should raise_error(Mongoid::Errors::Validations)
208
+ end
209
+ end
210
+ end
211
+
169
212
  describe "#concat" do
170
213
 
171
214
  before do
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 1
7
7
  - 2
8
- - 11
9
- version: 1.2.11
8
+ - 12
9
+ version: 1.2.12
10
10
  platform: ruby
11
11
  authors:
12
12
  - Durran Jordan
@@ -224,6 +224,7 @@ files:
224
224
  - spec/integration/mongoid/named_scope_spec.rb
225
225
  - spec/models/address.rb
226
226
  - spec/models/animal.rb
227
+ - spec/models/callbacks.rb
227
228
  - spec/models/comment.rb
228
229
  - spec/models/country_code.rb
229
230
  - spec/models/employer.rb
@@ -371,6 +372,7 @@ test_files:
371
372
  - spec/integration/mongoid/named_scope_spec.rb
372
373
  - spec/models/address.rb
373
374
  - spec/models/animal.rb
375
+ - spec/models/callbacks.rb
374
376
  - spec/models/comment.rb
375
377
  - spec/models/country_code.rb
376
378
  - spec/models/employer.rb