mongoid 0.10.1 → 0.10.2

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.10.1
1
+ 0.10.2
@@ -33,6 +33,7 @@ require "time"
33
33
  require "validatable"
34
34
  require "active_support/callbacks"
35
35
  require "active_support/core_ext"
36
+ require "active_support/inflector"
36
37
  require "active_support/time_with_zone"
37
38
  require "will_paginate/collection"
38
39
  require "mongo"
@@ -33,7 +33,7 @@ module Mongoid #:nodoc:
33
33
  # options: The association +Options+.
34
34
  def instantiate(document, options)
35
35
  foreign_key = document.send(options.foreign_key)
36
- foreign_key.nil? ? nil : new(document, foreign_key, options)
36
+ foreign_key.blank? ? nil : new(document, foreign_key, options)
37
37
  end
38
38
 
39
39
  # Returns the macro used to create the association.
@@ -54,7 +54,8 @@ module Mongoid #:nodoc:
54
54
  #
55
55
  # <tt>BelongsToRelated.update(game, person, options)</tt>
56
56
  def update(related, parent, options)
57
- parent.send("#{options.foreign_key}=", related.id); related
57
+ parent.send("#{options.foreign_key}=", related.id) if related
58
+ related
58
59
  end
59
60
  end
60
61
 
@@ -4,7 +4,7 @@ module Mongoid #:nodoc:
4
4
  class HasMany
5
5
  include Proxy
6
6
 
7
- attr_accessor :association_name, :klass
7
+ attr_accessor :association_name, :klass, :options
8
8
 
9
9
  # Appends the object to the +Array+, setting its parent in
10
10
  # the process.
@@ -68,7 +68,7 @@ module Mongoid #:nodoc:
68
68
  # This then delegated all methods to the array class since this is
69
69
  # essentially a proxy to an array itself.
70
70
  def initialize(document, options)
71
- @parent, @association_name, @klass = document, options.name, options.klass
71
+ @parent, @association_name, @klass, @options = document, options.name, options.klass, options
72
72
  attributes = document.attributes[@association_name]
73
73
  @documents = attributes ? attributes.collect do |attribute|
74
74
  child = @klass.instantiate(attribute)
@@ -21,7 +21,8 @@ module Mongoid #:nodoc:
21
21
  #
22
22
  # Returns the newly created object.
23
23
  def build(attributes = {})
24
- object = @klass.instantiate(attributes.merge(@foreign_key => @parent.id))
24
+ name = @parent.class.to_s.underscore
25
+ object = @klass.instantiate(attributes.merge(name => @parent))
25
26
  @documents << object
26
27
  object
27
28
  end
@@ -42,6 +43,12 @@ module Mongoid #:nodoc:
42
43
  object.save
43
44
  end
44
45
 
46
+ # Finds a document in this association.
47
+ # If an id is passed, will return the document for that id.
48
+ def find(id)
49
+ @klass.find(id)
50
+ end
51
+
45
52
  # Initializing a related association only requires looking up the objects
46
53
  # by their ids.
47
54
  #
@@ -11,7 +11,8 @@ module Mongoid #:nodoc:
11
11
  # Returns the newly created object.
12
12
  def build(attributes = {})
13
13
  @document = @klass.instantiate(attributes)
14
- @document.send("#{@foreign_key}=", @parent.id)
14
+ name = @parent.class.to_s.underscore
15
+ @document.send("#{name}=", @parent)
15
16
  end
16
17
 
17
18
  # Builds a new Document and sets it as the association, then saves the
@@ -82,8 +82,8 @@ module Mongoid #:nodoc:
82
82
  #
83
83
  # This will also cause the observing +Document+ to notify it's parent if
84
84
  # there is any.
85
- def write_attributes(attrs)
86
- process(attrs)
85
+ def write_attributes(attrs = nil)
86
+ process(attrs || {})
87
87
  notify
88
88
  end
89
89
 
@@ -66,7 +66,7 @@ module Mongoid #:nodoc:
66
66
  # Adds an index on the field specified. Options can be :unique => true or
67
67
  # :unique => false. It will default to the latter.
68
68
  def index(name, options = { :unique => false })
69
- collection.create_index(name, options)
69
+ collection.create_index(name, options[:unique])
70
70
  end
71
71
 
72
72
  # Instantiate a new object, only when loaded from the database.
@@ -4,6 +4,11 @@ module Mongoid #:nodoc:
4
4
  module String #:nodoc:
5
5
  module Inflections #:nodoc:
6
6
 
7
+ ActiveSupport::Inflector.inflections do |inflect|
8
+ inflect.singular("address", "address")
9
+ inflect.singular("addresses", "address")
10
+ end
11
+
7
12
  REVERSALS = {
8
13
  "asc" => "desc",
9
14
  "ascending" => "descending",
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{mongoid}
8
- s.version = "0.10.1"
8
+ s.version = "0.10.2"
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"]
12
- s.date = %q{2009-12-24}
12
+ s.date = %q{2009-12-25}
13
13
  s.email = %q{durran@gmail.com}
14
14
  s.extra_rdoc_files = [
15
15
  "README.textile"
@@ -7,7 +7,7 @@ class Person < Mongoid::Document
7
7
  include Mongoid::Versioning
8
8
  field :birth_date, :type => Date
9
9
  has_one :name
10
- has_many :addresses
10
+ has_one :address
11
11
  has_many :phones
12
12
  end
13
13
 
@@ -26,7 +26,7 @@ class Address < Mongoid::Document
26
26
  field :state
27
27
  field :post_code
28
28
  field :type
29
- belongs_to :person, :inverse_of => :addresses
29
+ belongs_to :person, :inverse_of => :address
30
30
  end
31
31
 
32
32
  class Phone < Mongoid::Document
@@ -58,7 +58,7 @@ puts "Starting benchmark..."
58
58
  )
59
59
  phone = Phone.new(:country_code => 1, :number => "415-555-1212", :type => "Mobile")
60
60
  person.name = name
61
- person.addresses << address
61
+ person.address = address
62
62
  person.phones << phone
63
63
  person.save
64
64
  end
@@ -22,7 +22,11 @@ describe Mongoid::Associations do
22
22
  @from_db.game.should == @game
23
23
  end
24
24
 
25
- it "sets the reverse association" do
25
+ it "sets the reverse association before save" do
26
+ @game.person.should == @person
27
+ end
28
+
29
+ it "sets the reverse association after save" do
26
30
  @from_db = Game.find(@game.id)
27
31
  @game.person.should == @person
28
32
  end
@@ -43,6 +47,19 @@ describe Mongoid::Associations do
43
47
  @from_db.posts.should == [@post]
44
48
  end
45
49
 
50
+ context "when building" do
51
+
52
+ before do
53
+ @person = Person.new(:title => "Mr")
54
+ @post = @person.posts.build(:title => "First")
55
+ end
56
+
57
+ it "sets the reverse association" do
58
+ @post.person.should == @person
59
+ end
60
+
61
+ end
62
+
46
63
  end
47
64
 
48
65
  context "nested embedded associations" do
@@ -18,11 +18,13 @@ describe Mongoid::Document do
18
18
  describe "#count" do
19
19
 
20
20
  before do
21
- Person.create(:title => "Sir")
21
+ 5.times do |n|
22
+ Person.create(:title => "Sir")
23
+ end
22
24
  end
23
25
 
24
26
  it "returns the count" do
25
- Person.count.should == 1
27
+ Person.count.should == 5
26
28
  end
27
29
 
28
30
  end
@@ -101,6 +101,7 @@ end
101
101
  class PetOwner < Mongoid::Document
102
102
  field :title
103
103
  has_one :pet
104
+ has_one :address
104
105
  end
105
106
 
106
107
  class Pet < Mongoid::Document
@@ -107,6 +107,14 @@ describe Mongoid::Associations::BelongsToRelated do
107
107
  Mongoid::Associations::BelongsToRelated.update(@related, @child, @options).should == @related
108
108
  end
109
109
 
110
+ context "when related is nil" do
111
+
112
+ it "returns nil" do
113
+ Mongoid::Associations::BelongsToRelated.update(nil, @child, @options).should be_nil
114
+ end
115
+
116
+ end
117
+
110
118
  end
111
119
 
112
120
  end
@@ -86,6 +86,11 @@ describe Mongoid::Associations::HasManyRelated do
86
86
  @association.build(:title => "Sassy").title.should == "Sassy"
87
87
  end
88
88
 
89
+ it "sets the parent object reference on the child" do
90
+ @association.build(:title => "Sassy")
91
+ @association.first.person.should == @parent
92
+ end
93
+
89
94
  end
90
95
 
91
96
  describe "#concat" do
@@ -162,6 +167,30 @@ describe Mongoid::Associations::HasManyRelated do
162
167
 
163
168
  end
164
169
 
170
+ describe "#find" do
171
+
172
+ before do
173
+ @parent = stub(:id => "5", :class => Person)
174
+ Post.expects(:all).returns([])
175
+ @association = Mongoid::Associations::HasManyRelated.new(@parent, options)
176
+ end
177
+
178
+ context "when finding by id" do
179
+
180
+ before do
181
+ @post = stub
182
+ end
183
+
184
+ it "returns the document in the array with that id" do
185
+ Post.expects(:find).with("5").returns(@post)
186
+ post = @association.find("5")
187
+ post.should == @post
188
+ end
189
+
190
+ end
191
+
192
+ end
193
+
165
194
  describe ".initialize" do
166
195
 
167
196
  context "when related id has been set" do
@@ -23,6 +23,11 @@ describe Mongoid::Associations::HasOneRelated do
23
23
  @association.person_id.should == @parent.id
24
24
  end
25
25
 
26
+ it "sets the parent object reference on the child" do
27
+ @association.build(:score => 100)
28
+ @association.person.should == @parent
29
+ end
30
+
26
31
  end
27
32
 
28
33
  describe "#create" do
@@ -265,6 +265,11 @@ describe Mongoid::Attributes do
265
265
  @person.age.should == 50
266
266
  end
267
267
 
268
+ it "allows passing of nil" do
269
+ @person.write_attributes(nil)
270
+ @person.age.should == 100
271
+ end
272
+
268
273
  end
269
274
 
270
275
  context "on a parent document" do
@@ -270,7 +270,7 @@ describe Mongoid::Document do
270
270
  context "when unique options are not provided" do
271
271
 
272
272
  it "delegates to collection with unique => false" do
273
- @collection.expects(:create_index).with(:title, :unique => false)
273
+ @collection.expects(:create_index).with(:title, false)
274
274
  Person.index :title
275
275
  end
276
276
 
@@ -279,7 +279,7 @@ describe Mongoid::Document do
279
279
  context "when unique option is provided" do
280
280
 
281
281
  it "delegates to collection with unique option" do
282
- @collection.expects(:create_index).with(:title, :unique => true)
282
+ @collection.expects(:create_index).with(:title, true)
283
283
  Person.index :title, :unique => true
284
284
  end
285
285
 
@@ -10,6 +10,14 @@ describe Mongoid::Extensions::String::Inflections do
10
10
  "bat".singular?.should be_true
11
11
  end
12
12
 
13
+ context "when string is added to inflections" do
14
+
15
+ it "returns true" do
16
+ "address".singular?.should be_true
17
+ end
18
+
19
+ end
20
+
13
21
  end
14
22
 
15
23
  context "when plural" do
@@ -18,6 +26,14 @@ describe Mongoid::Extensions::String::Inflections do
18
26
  "bats".singular?.should be_false
19
27
  end
20
28
 
29
+ context "when string is added to inflections" do
30
+
31
+ it "returns false" do
32
+ "addresses".singular?.should be_false
33
+ end
34
+
35
+ end
36
+
21
37
  end
22
38
 
23
39
  end
@@ -30,6 +46,14 @@ describe Mongoid::Extensions::String::Inflections do
30
46
  "bat".plural?.should be_false
31
47
  end
32
48
 
49
+ context "when string is added to inflections" do
50
+
51
+ it "returns false" do
52
+ "address".plural?.should be_false
53
+ end
54
+
55
+ end
56
+
33
57
  end
34
58
 
35
59
  context "when plural" do
@@ -38,6 +62,14 @@ describe Mongoid::Extensions::String::Inflections do
38
62
  "bats".plural?.should be_true
39
63
  end
40
64
 
65
+ context "when string is added to inflections" do
66
+
67
+ it "returns true" do
68
+ "addresses".plural?.should be_true
69
+ end
70
+
71
+ end
72
+
41
73
  end
42
74
 
43
75
  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.10.1
4
+ version: 0.10.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Durran Jordan
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-12-24 00:00:00 -05:00
12
+ date: 2009-12-25 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency