jmonteiro-mongo_mapper 0.1.6 → 0.1.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 CHANGED
@@ -1 +1 @@
1
- 0.1.6
1
+ 0.1.7
@@ -366,10 +366,13 @@ module MongoMapper
366
366
  end
367
367
 
368
368
  def reload
369
- doc = self.class.find(_id)
370
- self.class.associations.each { |name, assoc| send(name).reset if respond_to?(name) }
371
- self.attributes = doc.attributes
372
- self
369
+ if attrs = collection.find_one({:_id => _id})
370
+ self.class.associations.each { |name, assoc| send(name).reset if respond_to?(name) }
371
+ self.attributes = attrs
372
+ self
373
+ else
374
+ raise DocumentNotFound, "Document match #{_id.inspect} does not exist in #{collection.name} collection"
375
+ end
373
376
  end
374
377
 
375
378
  private
@@ -38,9 +38,9 @@ module MongoMapper
38
38
  changed.inject({}) { |h, key| h[key] = key_change(key); h }
39
39
  end
40
40
 
41
- def initialize(attrs={})
41
+ def initialize(*args)
42
42
  super
43
- changed_keys.clear if attrs.blank?
43
+ changed_keys.clear if args.first.blank? || !new?
44
44
  end
45
45
 
46
46
  def save(*args)
@@ -45,13 +45,9 @@ module MongoMapper
45
45
  def load(attrs)
46
46
  begin
47
47
  klass = attrs['_type'].present? ? attrs['_type'].constantize : self
48
- doc = klass.new(attrs)
49
- doc.instance_variable_set("@new", false)
50
- doc
48
+ klass.new(attrs, true)
51
49
  rescue NameError
52
- doc = new(attrs)
53
- doc.instance_variable_set("@new", false)
54
- doc
50
+ new(attrs, true)
55
51
  end
56
52
  end
57
53
 
@@ -140,7 +136,7 @@ module MongoMapper
140
136
  model.key :_id, ObjectId
141
137
  end
142
138
 
143
- def initialize(attrs={})
139
+ def initialize(attrs={}, from_db=false)
144
140
  unless attrs.nil?
145
141
  provided_keys = attrs.keys.map { |k| k.to_s }
146
142
  unless provided_keys.include?('_id') || provided_keys.include?('id')
@@ -148,7 +144,7 @@ module MongoMapper
148
144
  end
149
145
  end
150
146
 
151
- @new = true
147
+ @new = from_db ? false : true
152
148
  self._type = self.class.name if respond_to?(:_type=)
153
149
  self.attributes = attrs
154
150
  end
@@ -1,23 +1,17 @@
1
1
  require 'test_helper'
2
- require 'models'
3
2
 
4
3
  class DirtyTest < Test::Unit::TestCase
5
4
  def setup
6
- @document = Doc do
7
- key :phrase, String
8
- end
9
-
10
- Status.collection.remove
11
- Project.collection.remove
5
+ @document = Doc { key :phrase, String }
12
6
  end
13
-
7
+
14
8
  context "marking changes" do
15
9
  should "not happen if there are none" do
16
10
  doc = @document.new
17
11
  doc.phrase_changed?.should be_false
18
12
  doc.phrase_change.should be_nil
19
13
  end
20
-
14
+
21
15
  should "happen when change happens" do
22
16
  doc = @document.new
23
17
  doc.phrase = 'Golly Gee Willikers Batman'
@@ -25,12 +19,12 @@ class DirtyTest < Test::Unit::TestCase
25
19
  doc.phrase_was.should be_nil
26
20
  doc.phrase_change.should == [nil, 'Golly Gee Willikers Batman']
27
21
  end
28
-
22
+
29
23
  should "happen when initializing" do
30
24
  doc = @document.new(:phrase => 'Foo')
31
25
  doc.changed?.should be_true
32
26
  end
33
-
27
+
34
28
  should "clear changes on save" do
35
29
  doc = @document.new
36
30
  doc.phrase = 'Golly Gee Willikers Batman'
@@ -39,7 +33,7 @@ class DirtyTest < Test::Unit::TestCase
39
33
  doc.phrase_changed?.should_not be_true
40
34
  doc.phrase_change.should be_nil
41
35
  end
42
-
36
+
43
37
  should "clear changes on save!" do
44
38
  doc = @document.new
45
39
  doc.phrase = 'Golly Gee Willikers Batman'
@@ -48,15 +42,18 @@ class DirtyTest < Test::Unit::TestCase
48
42
  doc.phrase_changed?.should_not be_true
49
43
  doc.phrase_change.should be_nil
50
44
  end
51
-
45
+
52
46
  should "not happen when loading from database" do
53
47
  doc = @document.create(:phrase => 'Foo')
48
+ doc = @document.find(doc.id)
49
+
50
+ doc.changed?.should be_false
54
51
  doc.phrase = 'Fart'
55
52
  doc.changed?.should be_true
56
53
  doc.reload
57
54
  doc.changed?.should be_false
58
55
  end
59
-
56
+
60
57
  should "happen if changed after loading from database" do
61
58
  doc = @document.create(:phrase => 'Foo')
62
59
  doc.reload
@@ -65,7 +62,7 @@ class DirtyTest < Test::Unit::TestCase
65
62
  doc.changed?.should be_true
66
63
  end
67
64
  end
68
-
65
+
69
66
  context "blank new value and type integer" do
70
67
  should "not mark changes" do
71
68
  @document.key :age, Integer
@@ -147,10 +144,20 @@ class DirtyTest < Test::Unit::TestCase
147
144
 
148
145
  context "changing a foreign key through association" do
149
146
  should "mark changes" do
150
- status = Status.create(:name => 'Foo')
151
- status.project = Project.create(:name => 'Bar')
152
- status.changed?.should be_true
153
- status.changed.should == %w(project_id)
147
+ project_class = Doc do
148
+ key :name, String
149
+ end
150
+
151
+ milestone_class = Doc do
152
+ key :project_id, ObjectId
153
+ key :name, String
154
+ end
155
+ milestone_class.belongs_to :project, :class => project_class
156
+
157
+ milestone = milestone_class.create(:name => 'Launch')
158
+ milestone.project = project_class.create(:name => 'Harmony')
159
+ milestone.changed?.should be_true
160
+ milestone.changed.should == %w(project_id)
154
161
  end
155
162
  end
156
163
  end
@@ -393,7 +393,7 @@ class DocumentTest < Test::Unit::TestCase
393
393
  end
394
394
 
395
395
  should "return nil if document not found" do
396
- @document.find_by_id(1234).should be(nil)
396
+ @document.find_by_id(1234).should be_nil
397
397
  end
398
398
  end
399
399
 
@@ -1104,6 +1104,11 @@ class DocumentTest < Test::Unit::TestCase
1104
1104
  should "return self" do
1105
1105
  @instance.reload.object_id.should == @instance.object_id
1106
1106
  end
1107
+
1108
+ should "raise DocumentNotFound if not found" do
1109
+ @instance.destroy
1110
+ assert_raises(MongoMapper::DocumentNotFound) { @instance.reload }
1111
+ end
1107
1112
  end
1108
1113
 
1109
1114
  context "Loading a document from the database with keys that are not defined" do
@@ -64,6 +64,25 @@ class IdentityMapTest < Test::Unit::TestCase
64
64
  assert_not_in_map(person)
65
65
  end
66
66
 
67
+ context "reload" do
68
+ setup do
69
+ @person = @person_class.create(:name => 'Fred')
70
+ end
71
+
72
+ should "remove object from identity and re-query" do
73
+ assert_in_map(@person)
74
+ Mongo::Collection.any_instance.expects(:find_one).once.returns({})
75
+ @person.reload
76
+ end
77
+
78
+ should "add object back into map" do
79
+ assert_in_map(@person)
80
+ object_id = @person.object_id
81
+ @person.reload.object_id.should == object_id
82
+ assert_in_map(@person)
83
+ end
84
+ end
85
+
67
86
  context "#load" do
68
87
  setup do
69
88
  @id = Mongo::ObjectID.new
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jmonteiro-mongo_mapper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Nunemaker
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2010-01-18 00:00:00 -02:00
13
+ date: 2010-01-19 00:00:00 -02:00
14
14
  default_executable: mmconsole
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency