mongoid 1.0.4 → 1.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.4
1
+ 1.0.5
@@ -38,8 +38,11 @@ module Mongoid #:nodoc:
38
38
  #
39
39
  # Returns the newly created object.
40
40
  def build(attrs = {}, type = nil)
41
- object = type ? type.instantiate(attrs) : @klass.instantiate(attrs)
42
- push(object)
41
+ object = type ? type.instantiate : @klass.instantiate
42
+ object.parentize(@parent, @association_name)
43
+ object.write_attributes(attrs)
44
+ object.generate_key
45
+ @documents << object
43
46
  object
44
47
  end
45
48
 
@@ -6,19 +6,6 @@ module Mongoid #:nodoc:
6
6
 
7
7
  attr_reader :association_name, :document, :parent, :options
8
8
 
9
- # Build a new object for the association.
10
- def build(attrs = {}, type = nil)
11
- @document = attrs.assimilate(@parent, @options, type)
12
- self
13
- end
14
-
15
- # Create a new object for the association and save it.
16
- def create(attrs = {}, type = nil)
17
- build(attrs, type)
18
- @document.save
19
- self
20
- end
21
-
22
9
  # Creates the new association by finding the attributes in
23
10
  # the parent document with its name, and instantiating a
24
11
  # new document for it.
@@ -58,6 +45,13 @@ module Mongoid #:nodoc:
58
45
  @document.valid?
59
46
  end
60
47
 
48
+ protected
49
+ # Build a new object for the association.
50
+ def build(attrs = {}, type = nil)
51
+ @document = attrs.assimilate(@parent, @options, type)
52
+ self
53
+ end
54
+
61
55
  class << self
62
56
  # Preferred method of instantiating a new +HasOne+, since nil values
63
57
  # will be handled properly.
@@ -137,6 +137,15 @@ module Mongoid #:nodoc:
137
137
  self.class.instantiate(@attributes.except(:_id).except(:versions).dup, true)
138
138
  end
139
139
 
140
+ # Generate an id for this +Document+.
141
+ def generate_key
142
+ if primary_key
143
+ @attributes[:_id] = extract_keys.join(" ").identify
144
+ else
145
+ @attributes[:_id] = Mongo::ObjectID.new.to_s unless id
146
+ end
147
+ end
148
+
140
149
  # Instantiate a new +Document+, setting the Document's attributes if
141
150
  # given. If no attributes are provided, they will be initialized with
142
151
  # an empty +Hash+.
@@ -269,14 +278,6 @@ module Mongoid #:nodoc:
269
278
  end
270
279
 
271
280
  protected
272
- def generate_key
273
- if primary_key
274
- @attributes[:_id] = extract_keys.join(" ").identify
275
- else
276
- @attributes[:_id] = Mongo::ObjectID.new.to_s unless id
277
- end
278
- end
279
-
280
281
  def generate_type
281
282
  @attributes[:_type] ||= self.class.name
282
283
  end
@@ -18,7 +18,10 @@ module Mongoid #:nodoc:
18
18
  #
19
19
  # Returns: The child +Document+.
20
20
  def assimilate(parent, options, type = nil)
21
- child = type ? type.instantiate(self.merge(:_type => type.name)) : options.klass.instantiate(self)
21
+ klass = type ? type : options.klass
22
+ child = klass.instantiate(:_parent => parent)
23
+ child.write_attributes(self.merge(:_type => klass.name))
24
+ child.generate_key
22
25
  child.assimilate(parent, options)
23
26
  end
24
27
  end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{mongoid}
8
- s.version = "1.0.4"
8
+ s.version = "1.0.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"]
12
- s.date = %q{2010-01-12}
12
+ s.date = %q{2010-01-13}
13
13
  s.email = %q{durran@gmail.com}
14
14
  s.extra_rdoc_files = [
15
15
  "README.rdoc"
@@ -149,9 +149,14 @@ class Address
149
149
  field :city
150
150
  field :state
151
151
  field :post_code
152
+ field :parent_title
152
153
  key :street
153
154
  has_many :locations
154
155
  belongs_to :addressable, :inverse_of => :addresses
156
+
157
+ def set_parent=(set = false)
158
+ self.parent_title = addressable.title if set
159
+ end
155
160
  end
156
161
 
157
162
  class Location
@@ -164,8 +169,13 @@ class Name
164
169
  include Mongoid::Document
165
170
  field :first_name
166
171
  field :last_name
172
+ field :parent_title
167
173
  key :first_name, :last_name
168
174
  belongs_to :person, :inverse_of => :name
175
+
176
+ def set_parent=(set = false)
177
+ self.parent_title = person.title if set
178
+ end
169
179
  end
170
180
 
171
181
  class Comment
@@ -60,6 +60,20 @@ describe Mongoid::Associations::HasMany do
60
60
 
61
61
  describe "#build" do
62
62
 
63
+ context "setting the parent relationship" do
64
+
65
+ before do
66
+ @person = Person.new
67
+ end
68
+
69
+ it "happens before any other operation" do
70
+ address = @person.addresses.build(:set_parent => true, :street => "Madison Ave")
71
+ address._parent.should == @person
72
+ @person.addresses.first.should == address
73
+ end
74
+
75
+ end
76
+
63
77
  context "when a type is not provided" do
64
78
 
65
79
  before do
@@ -10,7 +10,7 @@ describe Mongoid::Associations::HasOne do
10
10
  @document = stub(:attributes => @attributes, :update => true)
11
11
  end
12
12
 
13
- describe "#build" do
13
+ describe "#build_*" do
14
14
 
15
15
  context "when attributes provided" do
16
16
 
@@ -23,7 +23,7 @@ describe Mongoid::Associations::HasOne do
23
23
  end
24
24
 
25
25
  it "replaces the existing has_one" do
26
- drink = @association.build({ :name => "Sapphire and Tonic" })
26
+ drink = @association.send(:build, { :name => "Sapphire and Tonic" })
27
27
  drink.name.should == "Sapphire and Tonic"
28
28
  end
29
29
 
@@ -40,51 +40,23 @@ describe Mongoid::Associations::HasOne do
40
40
  end
41
41
 
42
42
  it "instantiates a class of that type" do
43
- writer = @association.build({ :speed => 500 }, HtmlWriter)
43
+ writer = @association.send(:build, { :speed => 500 }, HtmlWriter)
44
44
  writer.should be_a_kind_of(HtmlWriter)
45
45
  writer.speed.should == 500
46
46
  end
47
47
 
48
48
  end
49
49
 
50
- end
51
-
52
- describe "#create" do
53
-
54
- context "when attributes provided" do
50
+ context "setting the parent relationship" do
55
51
 
56
52
  before do
57
- @association = Mongoid::Associations::HasOne.new(
58
- @document,
59
- @attributes[:mixed_drink],
60
- Mongoid::Associations::Options.new(:name => :mixed_drink)
61
- )
62
- @drink = MixedDrink.new(:name => "Sapphire and Tonic")
53
+ @person = Person.new
63
54
  end
64
55
 
65
- it "replaces and saves the existing has_one" do
66
- Mongoid::Commands::Save.expects(:execute).returns(true)
67
- drink = @association.create({ :name => "Sapphire and Tonic" })
68
- drink.name.should == "Sapphire and Tonic"
69
- end
70
-
71
- end
72
-
73
- context "when a type is supplied" do
74
-
75
- before do
76
- @association = Mongoid::Associations::HasOne.new(
77
- @document,
78
- @attributes[:writer],
79
- Mongoid::Associations::Options.new(:name => :writer)
80
- )
81
- end
82
-
83
- it "instantiates a class of that type" do
84
- Mongoid::Commands::Save.expects(:execute).returns(true)
85
- writer = @association.create({ :speed => 500 }, HtmlWriter)
86
- writer.should be_a_kind_of(HtmlWriter)
87
- writer.speed.should == 500
56
+ it "happens before any other operation" do
57
+ name = @person.build_name(:set_parent => true, :street => "Madison Ave")
58
+ name._parent.should == @person
59
+ @person.name.should == name
88
60
  end
89
61
 
90
62
  end
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: 1.0.4
4
+ version: 1.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Durran Jordan
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-01-12 00:00:00 -05:00
12
+ date: 2010-01-13 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency