mongoid 0.5.6 → 0.5.7
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/associations/has_one_association.rb +1 -1
- data/lib/mongoid/document.rb +10 -3
- data/lib/mongoid/extensions/array/conversions.rb +15 -0
- data/mongoid.gemspec +1 -1
- data/spec/spec_helper.rb +4 -0
- data/spec/unit/mongoid/associations/has_one_association_spec.rb +6 -6
- data/spec/unit/mongoid/document_spec.rb +4 -4
- data/spec/unit/mongoid/extensions/array/conversions_spec.rb +22 -3
- data/spec/unit/mongoid/extensions/object/conversions_spec.rb +2 -2
- metadata +1 -1
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.5.
|
1
|
+
0.5.7
|
@@ -13,7 +13,7 @@ module Mongoid #:nodoc:
|
|
13
13
|
# to the internal document itself.
|
14
14
|
def initialize(name, document, options = {})
|
15
15
|
class_name = options[:class_name]
|
16
|
-
klass = class_name ? class_name.constantize : name.to_s.
|
16
|
+
klass = class_name ? class_name.constantize : name.to_s.camelize.constantize
|
17
17
|
attributes = document.attributes[name]
|
18
18
|
@document = klass.new(attributes)
|
19
19
|
@document.parent = document
|
data/lib/mongoid/document.rb
CHANGED
@@ -5,15 +5,17 @@ module Mongoid #:nodoc:
|
|
5
5
|
extend Associations
|
6
6
|
|
7
7
|
attr_accessor :association_name, :parent
|
8
|
-
attr_reader :attributes
|
8
|
+
attr_reader :attributes, :new_record
|
9
9
|
|
10
10
|
define_callbacks \
|
11
11
|
:after_create,
|
12
12
|
:after_destroy,
|
13
13
|
:after_save,
|
14
|
+
:after_update,
|
14
15
|
:before_create,
|
15
16
|
:before_destroy,
|
16
|
-
:before_save
|
17
|
+
:before_save,
|
18
|
+
:before_update
|
17
19
|
|
18
20
|
class << self
|
19
21
|
|
@@ -188,6 +190,7 @@ module Mongoid #:nodoc:
|
|
188
190
|
# If no attributes are provided, they will be initialized with an empty Hash.
|
189
191
|
def initialize(attributes = {})
|
190
192
|
@attributes = process(fields, attributes)
|
193
|
+
@new_record = true if id.nil?
|
191
194
|
generate_key
|
192
195
|
end
|
193
196
|
|
@@ -198,7 +201,7 @@ module Mongoid #:nodoc:
|
|
198
201
|
|
199
202
|
# Returns true is the Document has not been persisted to the database, false if it has.
|
200
203
|
def new_record?
|
201
|
-
@
|
204
|
+
@new_record == true
|
202
205
|
end
|
203
206
|
|
204
207
|
# Notify observers that this Document has changed.
|
@@ -225,7 +228,9 @@ module Mongoid #:nodoc:
|
|
225
228
|
|
226
229
|
# Write to the attributes hash.
|
227
230
|
def write_attribute(name, value)
|
231
|
+
run_callbacks(:before_update)
|
228
232
|
@attributes[name] = fields[name].set(value)
|
233
|
+
run_callbacks(:after_update)
|
229
234
|
notify
|
230
235
|
end
|
231
236
|
|
@@ -241,6 +246,8 @@ module Mongoid #:nodoc:
|
|
241
246
|
if primary_key
|
242
247
|
values = primary_key.collect { |key| @attributes[key] }
|
243
248
|
@attributes[:_id] = values.join(" ").parameterize.to_s
|
249
|
+
else
|
250
|
+
@attributes[:_id] = Mongo::ObjectID.new unless id
|
244
251
|
end
|
245
252
|
end
|
246
253
|
end
|
@@ -7,6 +7,21 @@ module Mongoid #:nodoc:
|
|
7
7
|
def mongoidize
|
8
8
|
collect { |obj| obj.attributes }
|
9
9
|
end
|
10
|
+
|
11
|
+
def self.included(base)
|
12
|
+
base.class_eval do
|
13
|
+
extend ClassMethods
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
module ClassMethods
|
18
|
+
def get(value)
|
19
|
+
value
|
20
|
+
end
|
21
|
+
def set(value)
|
22
|
+
value
|
23
|
+
end
|
24
|
+
end
|
10
25
|
end
|
11
26
|
end
|
12
27
|
end
|
data/mongoid.gemspec
CHANGED
data/spec/spec_helper.rb
CHANGED
@@ -3,20 +3,20 @@ require File.join(File.dirname(__FILE__), "/../../../spec_helper.rb")
|
|
3
3
|
describe Mongoid::Associations::HasOneAssociation do
|
4
4
|
|
5
5
|
before do
|
6
|
-
@attributes = { :
|
6
|
+
@attributes = { :mixed_drink => { :name => "Jack and Coke" } }
|
7
7
|
@document = stub(:attributes => @attributes)
|
8
8
|
end
|
9
9
|
|
10
10
|
describe "#decorate!" do
|
11
11
|
|
12
12
|
before do
|
13
|
-
@association = Mongoid::Associations::HasOneAssociation.new(:
|
13
|
+
@association = Mongoid::Associations::HasOneAssociation.new(:mixed_drink, @document)
|
14
14
|
end
|
15
15
|
|
16
16
|
context "when getting values" do
|
17
17
|
|
18
18
|
it "delegates to the document" do
|
19
|
-
@association.
|
19
|
+
@association.name.should == "Jack and Coke"
|
20
20
|
end
|
21
21
|
|
22
22
|
end
|
@@ -24,12 +24,12 @@ describe Mongoid::Associations::HasOneAssociation do
|
|
24
24
|
context "when setting values" do
|
25
25
|
|
26
26
|
it "delegates to the document" do
|
27
|
-
@association.
|
28
|
-
@association.
|
27
|
+
@association.name = "Jack and Coke"
|
28
|
+
@association.name.should == "Jack and Coke"
|
29
29
|
end
|
30
30
|
|
31
31
|
end
|
32
32
|
|
33
33
|
end
|
34
34
|
|
35
|
-
end
|
35
|
+
end
|
@@ -21,8 +21,8 @@ describe Mongoid::Document do
|
|
21
21
|
context "when attributes are equal" do
|
22
22
|
|
23
23
|
before do
|
24
|
-
@document = Person.new(:title => "Sir")
|
25
|
-
@other = Person.new(:title => "Sir")
|
24
|
+
@document = Person.new(:_id => 1, :title => "Sir")
|
25
|
+
@other = Person.new(:_id => 1, :title => "Sir")
|
26
26
|
end
|
27
27
|
|
28
28
|
it "returns true" do
|
@@ -219,7 +219,7 @@ describe Mongoid::Document do
|
|
219
219
|
|
220
220
|
it "finds the first document from the collection and instantiates it" do
|
221
221
|
@collection.expects(:find_one).with({ :test => "Test" }, {}).returns(@attributes)
|
222
|
-
Person.first(:conditions => {:test => "Test"}).attributes.should == @attributes
|
222
|
+
Person.first(:conditions => {:test => "Test"}).attributes.except(:_id).should == @attributes
|
223
223
|
end
|
224
224
|
|
225
225
|
end
|
@@ -228,7 +228,7 @@ describe Mongoid::Document do
|
|
228
228
|
|
229
229
|
it "finds the first document from the collection and instantiates it" do
|
230
230
|
@collection.expects(:find_one).with(nil, {}).returns(@attributes)
|
231
|
-
Person.first.attributes.should == @attributes
|
231
|
+
Person.first.attributes.except(:_id).should == @attributes
|
232
232
|
end
|
233
233
|
|
234
234
|
end
|
@@ -5,10 +5,29 @@ describe Mongoid::Extensions::Array::Conversions do
|
|
5
5
|
describe "#mongoidize" do
|
6
6
|
|
7
7
|
it "collects each of its attributes" do
|
8
|
-
array = [
|
8
|
+
array = [
|
9
|
+
Person.new(:_id => 1, :title => "Sir"),
|
10
|
+
Person.new(:_id => 2, :title => "Madam")
|
11
|
+
]
|
9
12
|
array.mongoidize.should ==
|
10
|
-
[HashWithIndifferentAccess.new({ :title => "Sir", :age => 100 }),
|
11
|
-
|
13
|
+
[ HashWithIndifferentAccess.new({ :_id => 1, :title => "Sir", :age => 100 }),
|
14
|
+
HashWithIndifferentAccess.new({ :_id => 2, :title => "Madam", :age => 100 }) ]
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
describe "#get" do
|
20
|
+
|
21
|
+
it "returns the array" do
|
22
|
+
Array.get(["test"]).should == ["test"]
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
describe "#set" do
|
28
|
+
|
29
|
+
it "returns the array" do
|
30
|
+
Array.set(["test"]).should == ["test"]
|
12
31
|
end
|
13
32
|
|
14
33
|
end
|
@@ -5,8 +5,8 @@ describe Mongoid::Extensions::Object::Conversions do
|
|
5
5
|
describe "#mongoidize" do
|
6
6
|
|
7
7
|
it "returns its attributes" do
|
8
|
-
Person.new(:title => "Sir").mongoidize.should ==
|
9
|
-
HashWithIndifferentAccess.new({ :title => "Sir", :age => 100 })
|
8
|
+
Person.new(:_id => 1, :title => "Sir").mongoidize.should ==
|
9
|
+
HashWithIndifferentAccess.new({ :_id => 1, :title => "Sir", :age => 100 })
|
10
10
|
end
|
11
11
|
|
12
12
|
end
|