mongoid 0.11.3 → 0.11.4

Sign up to get free protection for your applications and to get access to all the features.
data/HISTORY CHANGED
@@ -1,3 +1,11 @@
1
+ === 0.11.4
2
+ - Fixed issue with dynamic fields: checking whether
3
+ the document responded to the attribute's method
4
+ should have checked the setter and not the getter
5
+
6
+ - Fixed has_one associations not being able to be
7
+ set to nil.
8
+
1
9
  === 0.11.3
2
10
  - Fixed issue with Document#save! not calling before
3
11
  and after create callbacks if document is new
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.11.3
1
+ 0.11.4
@@ -36,13 +36,14 @@ module Mongoid #:nodoc:
36
36
  unless attributes.nil?
37
37
  klass = attributes[:_type] ? attributes[:_type].constantize : nil
38
38
  @document = attributes.assimilate(@parent, @options, klass)
39
+ else
40
+ @document = nil
39
41
  end
40
42
  end
41
43
 
42
44
  # Delegate all missing methods over to the +Document+.
43
45
  def method_missing(name, *args, &block)
44
- @document = {}.assimilate(@parent, @options) if @document.nil?
45
- @document.send(name, *args, &block)
46
+ @document.send(name, *args, &block) unless @document.nil?
46
47
  end
47
48
 
48
49
  # Used for setting the association via a nested attributes setter on the
@@ -51,6 +52,16 @@ module Mongoid #:nodoc:
51
52
  build(attributes)
52
53
  end
53
54
 
55
+ # This should delegate to the document.
56
+ def nil?
57
+ @document.nil?
58
+ end
59
+
60
+ # This will get deprecated
61
+ def to_a
62
+ [@document]
63
+ end
64
+
54
65
  # Need to override here for when the underlying document is nil.
55
66
  def valid?
56
67
  @document ? @document.valid? : false
@@ -87,7 +98,11 @@ module Mongoid #:nodoc:
87
98
  #
88
99
  # <tt>HasOne.update({:first_name => "Hank"}, person, options)</tt>
89
100
  def update(child, parent, options)
90
- child.assimilate(parent, options)
101
+ if child.nil?
102
+ return parent.attributes[options.name] = nil
103
+ else
104
+ return child.assimilate(parent, options)
105
+ end
91
106
  end
92
107
  end
93
108
 
@@ -14,7 +14,7 @@ module Mongoid #:nodoc:
14
14
  # put into the document's attributes.
15
15
  def process(attrs = {})
16
16
  attrs.each_pair do |key, value|
17
- unless respond_to?(key)
17
+ unless respond_to?("#{key}=")
18
18
  self.class.field key, :type => value.class if Mongoid.allow_dynamic_fields
19
19
  end
20
20
  send("#{key}=", value) unless value.blank?
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{mongoid}
8
- s.version = "0.11.3"
8
+ s.version = "0.11.4"
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"]
@@ -210,23 +210,45 @@ describe Mongoid::Associations::HasOne do
210
210
 
211
211
  describe ".update" do
212
212
 
213
- before do
214
- @name = Name.new(:first_name => "Donald")
215
- @person = Person.new(:title => "Sir")
216
- Mongoid::Associations::HasOne.update(
217
- @name,
218
- @person,
219
- Mongoid::Associations::Options.new(:name => :name)
220
- )
221
- end
213
+ context "when setting to a non-nil value" do
214
+
215
+ before do
216
+ @name = Name.new(:first_name => "Donald")
217
+ @person = Person.new(:title => "Sir")
218
+ Mongoid::Associations::HasOne.update(
219
+ @name,
220
+ @person,
221
+ Mongoid::Associations::Options.new(:name => :name)
222
+ )
223
+ end
224
+
225
+ it "parentizes the child document" do
226
+ @name._parent.should == @person
227
+ end
228
+
229
+ it "sets the attributes of the child on the parent" do
230
+ @person.attributes[:name].should ==
231
+ { "_id" => "donald", "first_name" => "Donald", "_type" => "Name" }
232
+ end
222
233
 
223
- it "parentizes the child document" do
224
- @name._parent.should == @person
225
234
  end
226
235
 
227
- it "sets the attributes of the child on the parent" do
228
- @person.attributes[:name].should ==
229
- { "_id" => "donald", "first_name" => "Donald", "_type" => "Name" }
236
+ context "when setting the object to nil" do
237
+
238
+ before do
239
+ @name = Name.new(:first_name => "Donald")
240
+ @person = Person.new(:title => "Sir")
241
+ Mongoid::Associations::HasOne.update(
242
+ nil,
243
+ @person,
244
+ Mongoid::Associations::Options.new(:name => :name)
245
+ )
246
+ end
247
+
248
+ it "clears out the association" do
249
+ @person.name.should be_nil
250
+ end
251
+
230
252
  end
231
253
 
232
254
  end
@@ -96,7 +96,8 @@ describe Mongoid::Attributes do
96
96
  before do
97
97
  @attributes = {
98
98
  :nofieldstring => "Testing",
99
- :nofieldint => 5
99
+ :nofieldint => 5,
100
+ :employer => Employer.new
100
101
  }
101
102
  end
102
103
 
@@ -127,6 +128,15 @@ describe Mongoid::Attributes do
127
128
 
128
129
  end
129
130
 
131
+ context "when a method has been defined for the attribute" do
132
+
133
+ it "does not create the field" do
134
+ @person.fields.keys.should_not include("employer")
135
+ @person.fields.keys.should_not include("employer=")
136
+ end
137
+
138
+ end
139
+
130
140
  end
131
141
 
132
142
  context "when not allowing dynamic fields" do
@@ -954,11 +954,11 @@ describe Mongoid::Document do
954
954
 
955
955
  context "when the associated is nil" do
956
956
 
957
- it "returns false" do
957
+ it "returns true" do
958
958
  Person.class_eval do
959
959
  validates_associated :name
960
960
  end
961
- @person.valid?.should be_false
961
+ @person.valid?.should be_true
962
962
  end
963
963
 
964
964
  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: 0.11.3
4
+ version: 0.11.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Durran Jordan