mongoid 0.6.7 → 0.6.8
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/VERSION +1 -1
- data/lib/mongoid/attributes.rb +16 -5
- data/lib/mongoid/document.rb +3 -3
- data/mongoid.gemspec +1 -1
- data/spec/unit/mongoid/attributes_spec.rb +17 -0
- metadata +1 -1
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.6.
|
1
|
+
0.6.8
|
data/lib/mongoid/attributes.rb
CHANGED
@@ -2,13 +2,24 @@ module Mongoid #:nodoc:
|
|
2
2
|
module Attributes #:nodoc:
|
3
3
|
# Process the provided attributes casting them to their proper values if a
|
4
4
|
# field exists for them on the +Document+.
|
5
|
-
def process(
|
6
|
-
attributes = HashWithIndifferentAccess.new(params)
|
5
|
+
def process(params)
|
6
|
+
@attributes = HashWithIndifferentAccess.new(params)
|
7
|
+
process_fields
|
8
|
+
process_associations
|
9
|
+
end
|
10
|
+
|
11
|
+
protected
|
12
|
+
def process_fields
|
7
13
|
fields.values.each do |field|
|
8
|
-
value = field.set(attributes[field.name])
|
9
|
-
attributes[field.name] = value if value
|
14
|
+
value = field.set(@attributes[field.name])
|
15
|
+
@attributes[field.name] = value if value
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def process_associations
|
20
|
+
@attributes.each_pair do |key, value|
|
21
|
+
@attributes[key] = send("#{key}=", value) if value.is_a?(Document)
|
10
22
|
end
|
11
|
-
attributes
|
12
23
|
end
|
13
24
|
|
14
25
|
end
|
data/lib/mongoid/document.rb
CHANGED
@@ -209,8 +209,8 @@ module Mongoid #:nodoc:
|
|
209
209
|
|
210
210
|
# Instantiate a new Document, setting the Document's attributes if given.
|
211
211
|
# If no attributes are provided, they will be initialized with an empty Hash.
|
212
|
-
def initialize(
|
213
|
-
|
212
|
+
def initialize(attrs = {})
|
213
|
+
process(attrs)
|
214
214
|
@new_record = true if id.nil?
|
215
215
|
generate_key
|
216
216
|
end
|
@@ -275,7 +275,7 @@ module Mongoid #:nodoc:
|
|
275
275
|
# Writes all the attributes of this Document, and delegate up to
|
276
276
|
# the parent.
|
277
277
|
def write_attributes(attrs)
|
278
|
-
|
278
|
+
process(attrs)
|
279
279
|
notify
|
280
280
|
end
|
281
281
|
|
data/mongoid.gemspec
CHANGED
@@ -26,6 +26,23 @@ describe Mongoid::Attributes do
|
|
26
26
|
attrs = Person.new(@attributes).attributes
|
27
27
|
attrs[:age].should == 30
|
28
28
|
attrs[:terms].should == true
|
29
|
+
attrs[:_id].should == "1"
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
context "when associations provided in the attributes" do
|
35
|
+
|
36
|
+
before do
|
37
|
+
@name = Name.new(:first_name => "Testy")
|
38
|
+
@attributes = {
|
39
|
+
:name => @name
|
40
|
+
}
|
41
|
+
@person = Person.new(@attributes)
|
42
|
+
end
|
43
|
+
|
44
|
+
it "sets the associations" do
|
45
|
+
@person.name.should == @name
|
29
46
|
end
|
30
47
|
|
31
48
|
end
|