mongoid 0.4.3 → 0.4.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -30,24 +30,23 @@ Example of a simple domain model:
30
30
 
31
31
  <pre>
32
32
  class Person < Mongoid::Document
33
- fields :title
33
+ field :title
34
+ field :age, :default => 0
34
35
  has_many :addresses
35
36
  has_one :name
36
37
  end
37
38
 
38
39
  class Address < Mongoid::Document
39
- fields \
40
- :street,
41
- :city,
42
- :state,
43
- :post_code
40
+ field :street
41
+ field :city
42
+ field :state
43
+ field :post_code
44
44
  belongs_to :person
45
45
  end
46
46
 
47
47
  class Name < Mongoid::Document
48
- fields \
49
- :first_name,
50
- :last_name
48
+ field :first_name
49
+ field :last_name
51
50
  has_timestamps
52
51
  end
53
52
  </pre>
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.3
1
+ 0.4.4
@@ -2,22 +2,23 @@ module Mongoid #:nodoc:
2
2
  module Associations #:nodoc:
3
3
  class HasManyAssociation < DelegateClass(Array) #:nodoc:
4
4
 
5
- # Creates the new association by finding the attributes in
6
- # the parent document with its name, and instantiating a
7
- # new document for each one found. These will then be put in an
8
- # internal array.
9
- #
10
- # This then delegated all methods to the array class since this is
11
- # essentially a proxy to an array itself.
12
- def initialize(association_name, document)
13
- @klass = association_name.to_s.classify.constantize
14
- attributes = document.attributes[association_name]
15
- @documents = attributes ? attributes.collect do |attribute|
16
- child = @klass.new(attribute)
17
- child.parent = document
18
- child
19
- end : []
20
- super(@documents)
5
+ # Appends the object to the +Array+, setting its parent in
6
+ # the process.
7
+ def <<(object)
8
+ object.parentize(@parent)
9
+ @documents << object
10
+ end
11
+
12
+ # Appends the object to the +Array+, setting its parent in
13
+ # the process.
14
+ def push(object)
15
+ self << object
16
+ end
17
+
18
+ # Appends the object to the +Array+, setting its parent in
19
+ # the process.
20
+ def concat(object)
21
+ self << object
21
22
  end
22
23
 
23
24
  # Builds a new Document and adds it to the association collection. The
@@ -38,6 +39,26 @@ module Mongoid #:nodoc:
38
39
  return @documents if param == :all
39
40
  return detect { |document| document.id == param }
40
41
  end
42
+
43
+ # Creates the new association by finding the attributes in
44
+ # the parent document with its name, and instantiating a
45
+ # new document for each one found. These will then be put in an
46
+ # internal array.
47
+ #
48
+ # This then delegated all methods to the array class since this is
49
+ # essentially a proxy to an array itself.
50
+ def initialize(association_name, document)
51
+ @parent = document
52
+ @klass = association_name.to_s.classify.constantize
53
+ attributes = document.attributes[association_name]
54
+ @documents = attributes ? attributes.collect do |attribute|
55
+ child = @klass.new(attribute)
56
+ child.parent = document
57
+ child
58
+ end : []
59
+ super(@documents)
60
+ end
61
+
41
62
  end
42
63
  end
43
64
  end
@@ -28,11 +28,13 @@ module Mongoid #:nodoc:
28
28
 
29
29
  # Adds the association back to the parent document.
30
30
  def belongs_to(association_name)
31
+ @embedded = true
31
32
  add_association(:belongs_to, association_name.to_s.classify, association_name)
32
33
  end
33
34
 
34
35
  # Get the Mongo::Collection associated with this Document.
35
36
  def collection
37
+ return nil if @embedded
36
38
  @collection_name = self.to_s.demodulize.tableize
37
39
  @collection ||= Mongoid.database.collection(@collection_name)
38
40
  end
@@ -119,7 +121,7 @@ module Mongoid #:nodoc:
119
121
  0) do |pager|
120
122
  results = collection.find(selector, { :sort => params[:sort],
121
123
  :limit => pager.per_page,
122
- :offset => pager.offset })
124
+ :skip => pager.offset })
123
125
  pager.total_entries = results.count
124
126
  pager.replace(results.collect { |doc| new(doc) })
125
127
  end
@@ -171,6 +173,7 @@ module Mongoid #:nodoc:
171
173
  Mongoid::Associations::Factory.create(type, name, self)
172
174
  end
173
175
  define_method("#{name}=") do |object|
176
+ object.parentize(self)
174
177
  @attributes[name] = object.mongoidize
175
178
  end
176
179
  end
@@ -1,10 +1,14 @@
1
1
  require "mongoid/extensions/array/conversions"
2
+ require "mongoid/extensions/array/parentization"
2
3
  require "mongoid/extensions/object/conversions"
4
+ require "mongoid/extensions/object/parentization"
3
5
 
4
6
  class Array #:nodoc:
5
7
  include Mongoid::Extensions::Array::Conversions
8
+ include Mongoid::Extensions::Array::Parentization
6
9
  end
7
10
 
8
11
  class Object #:nodoc:
9
12
  include Mongoid::Extensions::Object::Conversions
13
+ include Mongoid::Extensions::Object::Parentization
10
14
  end
@@ -0,0 +1,12 @@
1
+ module Mongoid #:nodoc:
2
+ module Extensions #:nodoc:
3
+ module Array #:nodoc:
4
+ module Parentization #:nodoc:
5
+ # Adds the parent document to each element in the array.
6
+ def parentize(parent)
7
+ each { |obj| obj.parentize(parent) }
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ module Mongoid #:nodoc:
2
+ module Extensions #:nodoc:
3
+ module Object #:nodoc:
4
+ module Parentization #:nodoc:
5
+ # Sets the parent object
6
+ def parentize(object)
7
+ self.parent = object
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{mongoid}
8
- s.version = "0.4.3"
8
+ s.version = "0.4.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"]
@@ -39,7 +39,9 @@ Gem::Specification.new do |s|
39
39
  "lib/mongoid/document.rb",
40
40
  "lib/mongoid/extensions.rb",
41
41
  "lib/mongoid/extensions/array/conversions.rb",
42
+ "lib/mongoid/extensions/array/parentization.rb",
42
43
  "lib/mongoid/extensions/object/conversions.rb",
44
+ "lib/mongoid/extensions/object/parentization.rb",
43
45
  "lib/mongoid/field.rb",
44
46
  "mongoid.gemspec",
45
47
  "spec/integration/mongoid/document_spec.rb",
@@ -60,7 +62,9 @@ Gem::Specification.new do |s|
60
62
  "spec/unit/mongoid/criteria_spec.rb",
61
63
  "spec/unit/mongoid/document_spec.rb",
62
64
  "spec/unit/mongoid/extensions/array/conversions_spec.rb",
65
+ "spec/unit/mongoid/extensions/array/parentization_spec.rb",
63
66
  "spec/unit/mongoid/extensions/object/conversions_spec.rb",
67
+ "spec/unit/mongoid/extensions/object/parentization_spec.rb",
64
68
  "spec/unit/mongoid/field_spec.rb"
65
69
  ]
66
70
  s.homepage = %q{http://github.com/durran/mongoid}
@@ -86,7 +90,9 @@ Gem::Specification.new do |s|
86
90
  "spec/unit/mongoid/criteria_spec.rb",
87
91
  "spec/unit/mongoid/document_spec.rb",
88
92
  "spec/unit/mongoid/extensions/array/conversions_spec.rb",
93
+ "spec/unit/mongoid/extensions/array/parentization_spec.rb",
89
94
  "spec/unit/mongoid/extensions/object/conversions_spec.rb",
95
+ "spec/unit/mongoid/extensions/object/parentization_spec.rb",
90
96
  "spec/unit/mongoid/field_spec.rb"
91
97
  ]
92
98
 
@@ -92,4 +92,24 @@ describe Mongoid::Document do
92
92
 
93
93
  end
94
94
 
95
+ describe "#save" do
96
+
97
+ context "on a has_one association" do
98
+
99
+ before do
100
+ @person = Person.new(:title => "Sir")
101
+ @name = Name.new(:first_name => "Test")
102
+ @person.name = @name
103
+ end
104
+
105
+ it "saves the parent document" do
106
+ @name.save
107
+ person = Person.find(@person.id.to_s)
108
+ person.name.first_name.should == @name.first_name
109
+ end
110
+
111
+ end
112
+
113
+ end
114
+
95
115
  end
@@ -40,11 +40,43 @@ describe Mongoid::Associations::HasManyAssociation do
40
40
 
41
41
  before do
42
42
  @association = Mongoid::Associations::HasManyAssociation.new(:addresses, @document)
43
+ @address = Address.new
43
44
  end
44
45
 
45
- it "appends the document to the end of the array" do
46
- @association << Address.new
46
+ it "adds the parent document before appending to the array" do
47
+ @association << @address
48
+ @association.length.should == 3
49
+ @address.parent.should == @document
50
+ end
51
+
52
+ end
53
+
54
+ describe "#concat" do
55
+
56
+ before do
57
+ @association = Mongoid::Associations::HasManyAssociation.new(:addresses, @document)
58
+ @address = Address.new
59
+ end
60
+
61
+ it "adds the parent document before appending to the array" do
62
+ @association.concat [@address]
63
+ @association.length.should == 3
64
+ @address.parent.should == @document
65
+ end
66
+
67
+ end
68
+
69
+ describe "#push" do
70
+
71
+ before do
72
+ @association = Mongoid::Associations::HasManyAssociation.new(:addresses, @document)
73
+ @address = Address.new
74
+ end
75
+
76
+ it "adds the parent document before appending to the array" do
77
+ @association.push @address
47
78
  @association.length.should == 3
79
+ @address.parent.should == @document
48
80
  end
49
81
 
50
82
  end
@@ -7,7 +7,7 @@ describe Mongoid::Associations::HasOneAssociation do
7
7
  @document = stub(:attributes => @attributes)
8
8
  end
9
9
 
10
- describe "#method_missing" do
10
+ describe "#decorate!" do
11
11
 
12
12
  before do
13
13
  @association = Mongoid::Associations::HasOneAssociation.new(:name, @document)
@@ -58,6 +58,20 @@ describe Mongoid::Document do
58
58
 
59
59
  end
60
60
 
61
+ describe "#association=" do
62
+
63
+ before do
64
+ @name = Name.new
65
+ @person = Person.new
66
+ end
67
+
68
+ it "parentizes the association" do
69
+ @person.name = @name
70
+ @name.parent.should == @person
71
+ end
72
+
73
+ end
74
+
61
75
  describe "#belongs_to" do
62
76
 
63
77
  it "creates a reader for the association" do
@@ -82,6 +96,18 @@ describe Mongoid::Document do
82
96
  Person.collection.name.should == "people"
83
97
  end
84
98
 
99
+ context "when document is embedded" do
100
+
101
+ before do
102
+ @address = Address.new
103
+ end
104
+
105
+ it "returns nil" do
106
+ Address.collection.should be_nil
107
+ end
108
+
109
+ end
110
+
85
111
  end
86
112
 
87
113
  describe "#field" do
@@ -382,7 +408,7 @@ describe Mongoid::Document do
382
408
  context "when pagination parameters are passed" do
383
409
 
384
410
  it "delegates to will paginate with the results" do
385
- @collection.expects(:find).with({ :test => "Test" }, { :sort => nil, :limit => 20, :offset => 20}).returns(@cursor)
411
+ @collection.expects(:find).with({ :test => "Test" }, { :sort => nil, :limit => 20, :skip => 20}).returns(@cursor)
386
412
  Person.paginate(:conditions => { :test => "Test" }, :page => 2, :per_page => 20)
387
413
  end
388
414
 
@@ -391,7 +417,7 @@ describe Mongoid::Document do
391
417
  context "when pagination parameters are not passed" do
392
418
 
393
419
  it "delegates to will paginate with default values" do
394
- @collection.expects(:find).with({ :test => "Test" }, { :sort => nil, :limit => 20, :offset => 0}).returns(@cursor)
420
+ @collection.expects(:find).with({ :test => "Test" }, { :sort => nil, :limit => 20, :skip => 0}).returns(@cursor)
395
421
  Person.paginate(:conditions => { :test => "Test" })
396
422
  end
397
423
 
@@ -400,7 +426,7 @@ describe Mongoid::Document do
400
426
  context "when sorting paramters provided" do
401
427
 
402
428
  it "adds the sorting parameters in the collection#find" do
403
- @collection.expects(:find).with({ :test => "Test" }, { :sort => { :test => -1}, :limit => 20, :offset => 0}).returns(@cursor)
429
+ @collection.expects(:find).with({ :test => "Test" }, { :sort => { :test => -1}, :limit => 20, :skip => 0}).returns(@cursor)
404
430
  Person.paginate(:conditions => { :test => "Test" }, :sort => { :test => -1 })
405
431
  end
406
432
 
@@ -0,0 +1,20 @@
1
+ require File.join(File.dirname(__FILE__), "/../../../../spec_helper.rb")
2
+
3
+ describe Mongoid::Extensions::Array::Parentization do
4
+
5
+ describe "#parentize" do
6
+
7
+ before do
8
+ @parent = stub
9
+ @child = mock
10
+ @array = [@child]
11
+ end
12
+
13
+ it "sets the parent on each element" do
14
+ @child.expects(:parent=).with(@parent)
15
+ @array.parentize(@parent)
16
+ end
17
+
18
+ end
19
+
20
+ end
@@ -0,0 +1,19 @@
1
+ require File.join(File.dirname(__FILE__), "/../../../../spec_helper.rb")
2
+
3
+ describe Mongoid::Extensions::Object::Parentization do
4
+
5
+ describe "#parentize" do
6
+
7
+ before do
8
+ @parent = Person.new
9
+ @child = Name.new
10
+ end
11
+
12
+ it "sets the parent on each element" do
13
+ @child.parentize(@parent)
14
+ @child.parent.should == @parent
15
+ end
16
+
17
+ end
18
+
19
+ 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.4.3
4
+ version: 0.4.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Durran Jordan
@@ -85,7 +85,9 @@ files:
85
85
  - lib/mongoid/document.rb
86
86
  - lib/mongoid/extensions.rb
87
87
  - lib/mongoid/extensions/array/conversions.rb
88
+ - lib/mongoid/extensions/array/parentization.rb
88
89
  - lib/mongoid/extensions/object/conversions.rb
90
+ - lib/mongoid/extensions/object/parentization.rb
89
91
  - lib/mongoid/field.rb
90
92
  - mongoid.gemspec
91
93
  - spec/integration/mongoid/document_spec.rb
@@ -106,7 +108,9 @@ files:
106
108
  - spec/unit/mongoid/criteria_spec.rb
107
109
  - spec/unit/mongoid/document_spec.rb
108
110
  - spec/unit/mongoid/extensions/array/conversions_spec.rb
111
+ - spec/unit/mongoid/extensions/array/parentization_spec.rb
109
112
  - spec/unit/mongoid/extensions/object/conversions_spec.rb
113
+ - spec/unit/mongoid/extensions/object/parentization_spec.rb
110
114
  - spec/unit/mongoid/field_spec.rb
111
115
  has_rdoc: true
112
116
  homepage: http://github.com/durran/mongoid
@@ -154,5 +158,7 @@ test_files:
154
158
  - spec/unit/mongoid/criteria_spec.rb
155
159
  - spec/unit/mongoid/document_spec.rb
156
160
  - spec/unit/mongoid/extensions/array/conversions_spec.rb
161
+ - spec/unit/mongoid/extensions/array/parentization_spec.rb
157
162
  - spec/unit/mongoid/extensions/object/conversions_spec.rb
163
+ - spec/unit/mongoid/extensions/object/parentization_spec.rb
158
164
  - spec/unit/mongoid/field_spec.rb