mongoid 0.8.6 → 0.8.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -47,13 +47,14 @@ Example of a simple domain model:
47
47
  field :city
48
48
  field :state
49
49
  field :post_code
50
- belongs_to :person
50
+ belongs_to :person, :inverse_of => :addresses
51
51
  end
52
52
 
53
53
  class Name < Mongoid::Document
54
54
  include Mongoid::Timestamps
55
55
  field :first_name
56
56
  field :last_name
57
+ belongs_to :person, :inverse_of => :name
57
58
  end
58
59
  </pre>
59
60
 
@@ -110,7 +111,7 @@ Old School:
110
111
  New School:
111
112
 
112
113
  <pre>
113
- Person.select(:first_name, :last_name).where(:title => "Sir").skip(10).limit(10).execute
114
+ Person.select(:first_name, :last_name).where(:title => "Sir").skip(10).limit(10).paginate
114
115
  Person.select(:title).aggregate
115
116
  Criteria.translate(:conditions => { :title => "Sir" }).execute
116
117
  </pre>
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.8.6
1
+ 0.8.7
@@ -72,6 +72,9 @@ module Mongoid
72
72
  end
73
73
  end
74
74
 
75
+ # Raised when invalid options are passed into an association.
76
+ class InvalidOptionsError < RuntimeError; end
77
+
75
78
  # Connect to the database name supplied. This should be run
76
79
  # for initial setup, potentially in a rails initializer.
77
80
  def self.connect_to(name)
@@ -39,9 +39,12 @@ module Mongoid # :nodoc:
39
39
  # end
40
40
  #
41
41
  # class Address < Mongoid::Document
42
- # belongs_to :person
42
+ # belongs_to :person, :inverse_of => :addresses
43
43
  # end
44
44
  def belongs_to(name, options = {})
45
+ unless options.has_key?(:inverse_of)
46
+ raise InvalidOptionsError.new("Options for belongs_to association must include :inverse_of")
47
+ end
45
48
  @embedded = true
46
49
  add_association(Associations::BelongsTo, Associations::Options.new(options.merge(:name => name)))
47
50
  end
@@ -61,7 +64,7 @@ module Mongoid # :nodoc:
61
64
  # end
62
65
  #
63
66
  # class Address < Mongoid::Document
64
- # belongs_to :person
67
+ # belongs_to :person, :inverse_of => :addresses
65
68
  # end
66
69
  def has_many(name, options = {})
67
70
  add_association(Associations::HasMany, Associations::Options.new(options.merge(:name => name)))
@@ -26,13 +26,7 @@ module Mongoid #:nodoc:
26
26
  # is initialized by setting a parent object as the association on the
27
27
  # +Document+. Will properly set a has_one or a has_many.
28
28
  def update(parent, child, options)
29
- name = child.class.name.demodulize.downcase
30
- has_one = parent.associations[name]
31
- if has_one
32
- child.parentize(parent, name)
33
- else
34
- child.parentize(parent, name.tableize)
35
- end
29
+ child.parentize(parent, options.inverse_of)
36
30
  child.notify
37
31
  parent
38
32
  end
@@ -13,6 +13,11 @@ module Mongoid #:nodoc:
13
13
  @attributes[:name]
14
14
  end
15
15
 
16
+ # Returns the name of the inverse_of association
17
+ def inverse_of
18
+ @attributes[:inverse_of]
19
+ end
20
+
16
21
  # Return a +Class+ for the options. If a class_name was provided, then the
17
22
  # constantized class_name will be returned. If not, a constant based on the
18
23
  # association name will be returned.
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{mongoid}
8
- s.version = "0.8.6"
8
+ s.version = "0.8.7"
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-11-23}
12
+ s.date = %q{2009-11-24}
13
13
  s.email = %q{durran@gmail.com}
14
14
  s.extra_rdoc_files = [
15
15
  "README.textile"
@@ -53,20 +53,20 @@ end
53
53
  class CountryCode < Mongoid::Document
54
54
  field :code, :type => Integer
55
55
  key :code
56
- belongs_to :phone_number
56
+ belongs_to :phone_number, :inverse_of => :country_codes
57
57
  end
58
58
 
59
59
  class Phone < Mongoid::Document
60
60
  field :number
61
61
  key :number
62
- belongs_to :person
62
+ belongs_to :person, :inverse_of => :phone_numbers
63
63
  has_one :country_code
64
64
  end
65
65
 
66
66
  class Animal < Mongoid::Document
67
67
  field :name
68
68
  key :name
69
- belongs_to :person
69
+ belongs_to :person, :inverse_of => :pet
70
70
  end
71
71
 
72
72
  class PetOwner < Mongoid::Document
@@ -78,12 +78,12 @@ class Pet < Mongoid::Document
78
78
  field :name
79
79
  field :weight, :type => Float, :default => 0.0
80
80
  has_many :vet_visits
81
- belongs_to :pet_owner
81
+ belongs_to :pet_owner, :inverse_of => :pet
82
82
  end
83
83
 
84
84
  class VetVisit < Mongoid::Document
85
85
  field :date, :type => Date
86
- belongs_to :pet
86
+ belongs_to :pet, :inverse_of => :vet_visits
87
87
  end
88
88
 
89
89
  class Address < Mongoid::Document
@@ -92,14 +92,14 @@ class Address < Mongoid::Document
92
92
  field :state
93
93
  field :post_code
94
94
  key :street
95
- belongs_to :addressable
95
+ belongs_to :addressable, :inverse_of => :addresses
96
96
  end
97
97
 
98
98
  class Name < Mongoid::Document
99
99
  field :first_name
100
100
  field :last_name
101
101
  key :first_name, :last_name
102
- belongs_to :person
102
+ belongs_to :person, :inverse_of => :name
103
103
  end
104
104
 
105
105
  class Comment < Mongoid::Document
@@ -57,7 +57,7 @@ describe Mongoid::Associations::BelongsTo do
57
57
  before do
58
58
  @name = Name.new(:first_name => "Test", :last_name => "User")
59
59
  @person = Person.new(:title => "Mrs")
60
- @options = Mongoid::Associations::Options.new(:name => :person)
60
+ @options = Mongoid::Associations::Options.new(:name => :person, :inverse_of => :name)
61
61
  Mongoid::Associations::BelongsTo.update(@person, @name, @options)
62
62
  end
63
63
 
@@ -74,7 +74,8 @@ describe Mongoid::Associations::BelongsTo do
74
74
  before do
75
75
  @address = Address.new(:street => "Broadway")
76
76
  @person = Person.new(:title => "Mrs")
77
- Mongoid::Associations::BelongsTo.update(@person, @address, :person)
77
+ @options = Mongoid::Associations::Options.new(:name => :person, :inverse_of => :addresses)
78
+ Mongoid::Associations::BelongsTo.update(@person, @address, @options)
78
79
  end
79
80
 
80
81
  it "updates the parent document" do
@@ -15,6 +15,19 @@ describe Mongoid::Associations::Options do
15
15
 
16
16
  end
17
17
 
18
+ describe "#inverse_of" do
19
+
20
+ before do
21
+ @attributes = { :inverse_of => :addresses }
22
+ @options = Mongoid::Associations::Options.new(@attributes)
23
+ end
24
+
25
+ it "returns the inverse_of value" do
26
+ @options.inverse_of.should == :addresses
27
+ end
28
+
29
+ end
30
+
18
31
  describe "#klass" do
19
32
 
20
33
  context "when class_name provided" do
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.8.6
4
+ version: 0.8.7
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-11-23 00:00:00 -05:00
12
+ date: 2009-11-24 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency