mongoid 0.7.9 → 0.7.10
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.rb +3 -1
- data/lib/mongoid/associations.rb +14 -13
- data/lib/mongoid/associations/accessor.rb +4 -4
- data/lib/mongoid/associations/belongs_to.rb +2 -2
- data/lib/mongoid/associations/has_many.rb +8 -10
- data/lib/mongoid/associations/has_one.rb +7 -9
- data/lib/mongoid/options.rb +30 -0
- data/mongoid.gemspec +4 -1
- data/spec/unit/mongoid/associations/accessor_spec.rb +17 -10
- data/spec/unit/mongoid/associations/belongs_to_spec.rb +6 -3
- data/spec/unit/mongoid/associations/has_many_spec.rb +11 -11
- data/spec/unit/mongoid/associations/has_one_spec.rb +2 -2
- data/spec/unit/mongoid/associations_spec.rb +1 -1
- data/spec/unit/mongoid/options_spec.rb +96 -0
- metadata +4 -1
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.7.
|
1
|
+
0.7.10
|
data/lib/mongoid.rb
CHANGED
@@ -21,7 +21,8 @@
|
|
21
21
|
require "rubygems"
|
22
22
|
|
23
23
|
gem "activesupport", "2.3.4"
|
24
|
-
gem "mongo", "0.
|
24
|
+
gem "mongo", "0.17"
|
25
|
+
gem "mongo_ext", "0.17"
|
25
26
|
gem "durran-validatable", "1.8.2"
|
26
27
|
gem "will_paginate", "2.3.11"
|
27
28
|
|
@@ -40,6 +41,7 @@ require "mongoid/commands"
|
|
40
41
|
require "mongoid/criteria"
|
41
42
|
require "mongoid/extensions"
|
42
43
|
require "mongoid/field"
|
44
|
+
require "mongoid/options"
|
43
45
|
require "mongoid/timestamps"
|
44
46
|
require "mongoid/versioning"
|
45
47
|
require "mongoid/document"
|
data/lib/mongoid/associations.rb
CHANGED
@@ -30,7 +30,7 @@ module Mongoid # :nodoc:
|
|
30
30
|
#
|
31
31
|
# Options:
|
32
32
|
#
|
33
|
-
#
|
33
|
+
# name: A +Symbol+ that matches the name of the parent class.
|
34
34
|
#
|
35
35
|
# Example:
|
36
36
|
#
|
@@ -41,9 +41,9 @@ module Mongoid # :nodoc:
|
|
41
41
|
# class Address < Mongoid::Document
|
42
42
|
# belongs_to :person
|
43
43
|
# end
|
44
|
-
def belongs_to(
|
44
|
+
def belongs_to(name, options = {})
|
45
45
|
@embedded = true
|
46
|
-
add_association(Associations::BelongsTo, association_name
|
46
|
+
add_association(Associations::BelongsTo, Options.new(options.merge(:association_name => name)))
|
47
47
|
end
|
48
48
|
|
49
49
|
# Adds the association from a parent document to its children. The name
|
@@ -52,7 +52,7 @@ module Mongoid # :nodoc:
|
|
52
52
|
#
|
53
53
|
# Options:
|
54
54
|
#
|
55
|
-
#
|
55
|
+
# name: A +Symbol+ that is the plural child class name.
|
56
56
|
#
|
57
57
|
# Example:
|
58
58
|
#
|
@@ -63,8 +63,8 @@ module Mongoid # :nodoc:
|
|
63
63
|
# class Address < Mongoid::Document
|
64
64
|
# belongs_to :person
|
65
65
|
# end
|
66
|
-
def has_many(
|
67
|
-
add_association(Associations::HasMany, association_name
|
66
|
+
def has_many(name, options = {})
|
67
|
+
add_association(Associations::HasMany, Options.new(options.merge(:association_name => name)))
|
68
68
|
end
|
69
69
|
|
70
70
|
# Adds the association from a parent document to its child. The name
|
@@ -73,7 +73,7 @@ module Mongoid # :nodoc:
|
|
73
73
|
#
|
74
74
|
# Options:
|
75
75
|
#
|
76
|
-
#
|
76
|
+
# name: A +Symbol+ that is the plural child class name.
|
77
77
|
#
|
78
78
|
# Example:
|
79
79
|
#
|
@@ -84,22 +84,23 @@ module Mongoid # :nodoc:
|
|
84
84
|
# class Address < Mongoid::Document
|
85
85
|
# belongs_to :person
|
86
86
|
# end
|
87
|
-
def has_one(
|
88
|
-
add_association(Associations::HasOne, association_name
|
87
|
+
def has_one(name, options = {})
|
88
|
+
add_association(Associations::HasOne, Options.new(options.merge(:association_name => name)))
|
89
89
|
end
|
90
90
|
|
91
91
|
private
|
92
92
|
# Adds the association to the associations hash with the type as the key,
|
93
93
|
# then adds the accessors for the association.
|
94
|
-
def add_association(type,
|
95
|
-
associations[
|
94
|
+
def add_association(type, options)
|
95
|
+
associations[options.association_name] = type
|
96
|
+
name = options.association_name
|
96
97
|
define_method(name) do
|
97
98
|
return instance_variable_get("@#{name}") if instance_variable_defined?("@#{name}")
|
98
|
-
proxy = Associations::Accessor.get(type,
|
99
|
+
proxy = Associations::Accessor.get(type, self, options)
|
99
100
|
instance_variable_set("@#{name}", proxy)
|
100
101
|
end
|
101
102
|
define_method("#{name}=") do |object|
|
102
|
-
proxy = Associations::Accessor.set(type,
|
103
|
+
proxy = Associations::Accessor.set(type, self, object, options)
|
103
104
|
instance_variable_set("@#{name}", proxy)
|
104
105
|
end
|
105
106
|
end
|
@@ -5,8 +5,8 @@ module Mongoid #:nodoc:
|
|
5
5
|
# Gets an association, based on the type provided and
|
6
6
|
# passes the name and document into the newly instantiated
|
7
7
|
# association.
|
8
|
-
def get(type,
|
9
|
-
document ? type.new(
|
8
|
+
def get(type, document, options)
|
9
|
+
document ? type.new(document, options) : nil
|
10
10
|
end
|
11
11
|
|
12
12
|
# Set an object association. This is used to set the parent reference
|
@@ -20,8 +20,8 @@ module Mongoid #:nodoc:
|
|
20
20
|
# document: The base document to handle the access for.
|
21
21
|
# object: The object that was passed in to the setter method.
|
22
22
|
# options: optional options.
|
23
|
-
def set(type,
|
24
|
-
type.update(object, document,
|
23
|
+
def set(type, document, object, options)
|
24
|
+
type.update(object, document, options)
|
25
25
|
end
|
26
26
|
end
|
27
27
|
end
|
@@ -9,7 +9,7 @@ module Mongoid #:nodoc:
|
|
9
9
|
#
|
10
10
|
# All method calls on this object will then be delegated
|
11
11
|
# to the internal document itself.
|
12
|
-
def initialize(
|
12
|
+
def initialize(document, options)
|
13
13
|
@document = document.parent
|
14
14
|
decorate!
|
15
15
|
end
|
@@ -25,7 +25,7 @@ module Mongoid #:nodoc:
|
|
25
25
|
# Perform an update of the relationship of the parent and child. This
|
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
|
-
def update(parent, child,
|
28
|
+
def update(parent, child, options)
|
29
29
|
name = child.class.name.demodulize.downcase
|
30
30
|
has_one = parent.associations[name]
|
31
31
|
if has_one
|
@@ -59,11 +59,10 @@ module Mongoid #:nodoc:
|
|
59
59
|
#
|
60
60
|
# This then delegated all methods to the array class since this is
|
61
61
|
# essentially a proxy to an array itself.
|
62
|
-
def initialize(
|
62
|
+
def initialize(document, options)
|
63
63
|
@parent = document
|
64
|
-
@association_name =
|
65
|
-
|
66
|
-
@klass = class_name ? class_name.constantize : @association_name.to_s.classify.constantize
|
64
|
+
@association_name = options.association_name
|
65
|
+
@klass = options.klass
|
67
66
|
attributes = document.attributes[@association_name]
|
68
67
|
@documents = attributes ? attributes.collect do |attribute|
|
69
68
|
child = @klass.instantiate(attribute)
|
@@ -77,18 +76,17 @@ module Mongoid #:nodoc:
|
|
77
76
|
# Perform an update of the relationship of the parent and child. This
|
78
77
|
# is initialized by setting the has_many to the supplied +Enumerable+
|
79
78
|
# and setting up the parentization.
|
80
|
-
def update(children, parent,
|
81
|
-
parent.attributes.delete(
|
82
|
-
|
83
|
-
klass = class_name ? class_name.constantize : name.to_s.classify.constantize
|
79
|
+
def update(children, parent, options)
|
80
|
+
parent.attributes.delete(options.association_name)
|
81
|
+
klass = options.klass
|
84
82
|
children.each do |child|
|
85
83
|
unless child.respond_to?(:parentize)
|
86
84
|
child = klass.new(child)
|
87
85
|
end
|
88
|
-
child.parentize(parent,
|
86
|
+
child.parentize(parent, options.association_name)
|
89
87
|
child.notify
|
90
88
|
end
|
91
|
-
new(
|
89
|
+
new(parent, options)
|
92
90
|
end
|
93
91
|
end
|
94
92
|
|
@@ -13,25 +13,23 @@ module Mongoid #:nodoc:
|
|
13
13
|
#
|
14
14
|
# All method calls on this object will then be delegated
|
15
15
|
# to the internal document itself.
|
16
|
-
def initialize(
|
17
|
-
|
18
|
-
|
19
|
-
attributes = document.attributes[name]
|
16
|
+
def initialize(document, options)
|
17
|
+
@klass = options.klass
|
18
|
+
attributes = document.attributes[options.association_name]
|
20
19
|
@document = klass.instantiate(attributes || {})
|
21
|
-
@document.parentize(document,
|
20
|
+
@document.parentize(document, options.association_name)
|
22
21
|
decorate!
|
23
22
|
end
|
24
23
|
|
25
24
|
class << self
|
26
25
|
# Perform an update of the relationship of the parent and child. This
|
27
26
|
# is initialized by setting the has_one to the supplied child.
|
28
|
-
def update(child, parent,
|
27
|
+
def update(child, parent, options)
|
29
28
|
unless child.respond_to?(:parentize)
|
30
|
-
|
31
|
-
klass = class_name ? class_name.constantize : name.to_s.camelize.constantize
|
29
|
+
klass = options.klass
|
32
30
|
child = klass.new(child)
|
33
31
|
end
|
34
|
-
child.parentize(parent,
|
32
|
+
child.parentize(parent, options.association_name)
|
35
33
|
child.notify
|
36
34
|
child
|
37
35
|
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module Mongoid #:nodoc:
|
2
|
+
class Options #:nodoc:
|
3
|
+
|
4
|
+
# Create the new +Options+ object, which provides convenience methods for
|
5
|
+
# accessing values out of an options +Hash+.
|
6
|
+
def initialize(attributes = {})
|
7
|
+
@attributes = attributes
|
8
|
+
end
|
9
|
+
|
10
|
+
# Returns the association name of the options.
|
11
|
+
def association_name
|
12
|
+
@attributes[:association_name]
|
13
|
+
end
|
14
|
+
|
15
|
+
# Return a +Class+ for the options. If a class_name was provided, then the
|
16
|
+
# constantized class_name will be returned. If not, a constant based on the
|
17
|
+
# association name will be returned.
|
18
|
+
def klass
|
19
|
+
class_name = @attributes[:class_name]
|
20
|
+
association_name = @attributes[:association_name]
|
21
|
+
class_name ? class_name.constantize : association_name.to_s.classify.constantize
|
22
|
+
end
|
23
|
+
|
24
|
+
# Returns whether or not this association is polymorphic.
|
25
|
+
def polymorphic
|
26
|
+
@attributes[:polymorphic] == true
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
end
|
data/mongoid.gemspec
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{mongoid}
|
8
|
-
s.version = "0.7.
|
8
|
+
s.version = "0.7.10"
|
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"]
|
@@ -55,6 +55,7 @@ Gem::Specification.new do |s|
|
|
55
55
|
"lib/mongoid/extensions/symbol/inflections.rb",
|
56
56
|
"lib/mongoid/extensions/time/conversions.rb",
|
57
57
|
"lib/mongoid/field.rb",
|
58
|
+
"lib/mongoid/options.rb",
|
58
59
|
"lib/mongoid/timestamps.rb",
|
59
60
|
"lib/mongoid/versioning.rb",
|
60
61
|
"mongoid.gemspec",
|
@@ -93,6 +94,7 @@ Gem::Specification.new do |s|
|
|
93
94
|
"spec/unit/mongoid/extensions/symbol/inflections_spec.rb",
|
94
95
|
"spec/unit/mongoid/extensions/time/conversions_spec.rb",
|
95
96
|
"spec/unit/mongoid/field_spec.rb",
|
97
|
+
"spec/unit/mongoid/options_spec.rb",
|
96
98
|
"spec/unit/mongoid/timestamps_spec.rb",
|
97
99
|
"spec/unit/mongoid/versioning_spec.rb"
|
98
100
|
]
|
@@ -135,6 +137,7 @@ Gem::Specification.new do |s|
|
|
135
137
|
"spec/unit/mongoid/extensions/symbol/inflections_spec.rb",
|
136
138
|
"spec/unit/mongoid/extensions/time/conversions_spec.rb",
|
137
139
|
"spec/unit/mongoid/field_spec.rb",
|
140
|
+
"spec/unit/mongoid/options_spec.rb",
|
138
141
|
"spec/unit/mongoid/timestamps_spec.rb",
|
139
142
|
"spec/unit/mongoid/versioning_spec.rb"
|
140
143
|
]
|
@@ -12,7 +12,8 @@ describe Mongoid::Associations::Accessor do
|
|
12
12
|
context "when type is has_many" do
|
13
13
|
|
14
14
|
it "returns a HasMany" do
|
15
|
-
|
15
|
+
@options = Mongoid::Options.new(:association_name => :addresses)
|
16
|
+
association = Mongoid::Associations::Accessor.get(Mongoid::Associations::HasMany, @document, @options)
|
16
17
|
association.should be_a_kind_of(Mongoid::Associations::HasMany)
|
17
18
|
end
|
18
19
|
|
@@ -23,7 +24,8 @@ describe Mongoid::Associations::Accessor do
|
|
23
24
|
context "when document is not nil" do
|
24
25
|
|
25
26
|
it "returns a HasOne" do
|
26
|
-
|
27
|
+
@options = Mongoid::Options.new(:association_name => :name)
|
28
|
+
association = Mongoid::Associations::Accessor.get(Mongoid::Associations::HasOne, @document, @options)
|
27
29
|
association.should be_a_kind_of(Name)
|
28
30
|
end
|
29
31
|
|
@@ -32,7 +34,8 @@ describe Mongoid::Associations::Accessor do
|
|
32
34
|
context "when document is nil" do
|
33
35
|
|
34
36
|
it "returns nil" do
|
35
|
-
|
37
|
+
@options = Mongoid::Options.new(:association_name => :name)
|
38
|
+
association = Mongoid::Associations::Accessor.get(Mongoid::Associations::HasOne, nil, @options)
|
36
39
|
association.should be_nil
|
37
40
|
end
|
38
41
|
|
@@ -43,7 +46,8 @@ describe Mongoid::Associations::Accessor do
|
|
43
46
|
context "when type is belongs_to" do
|
44
47
|
|
45
48
|
it "returns a BelongsTo" do
|
46
|
-
|
49
|
+
@options = Mongoid::Options.new(:association_name => :person)
|
50
|
+
association = Mongoid::Associations::Accessor.get(Mongoid::Associations::BelongsTo, stub(:parent => @document), @options)
|
47
51
|
association.should be_a_kind_of(Person)
|
48
52
|
end
|
49
53
|
|
@@ -56,8 +60,9 @@ describe Mongoid::Associations::Accessor do
|
|
56
60
|
context "when type is has_many" do
|
57
61
|
|
58
62
|
it "returns a HasMany" do
|
59
|
-
Mongoid::
|
60
|
-
Mongoid::Associations::
|
63
|
+
@options = Mongoid::Options.new(:association_name => :addresses)
|
64
|
+
Mongoid::Associations::HasMany.expects(:update).with(@document, @object, @options)
|
65
|
+
Mongoid::Associations::Accessor.set(Mongoid::Associations::HasMany, @document, @object, @options)
|
61
66
|
end
|
62
67
|
|
63
68
|
end
|
@@ -65,8 +70,9 @@ describe Mongoid::Associations::Accessor do
|
|
65
70
|
context "when type is has_one" do
|
66
71
|
|
67
72
|
it "returns a HasOne" do
|
68
|
-
Mongoid::
|
69
|
-
Mongoid::Associations::
|
73
|
+
@options = Mongoid::Options.new(:association_name => :name)
|
74
|
+
Mongoid::Associations::HasOne.expects(:update).with(@document, @object, @options)
|
75
|
+
Mongoid::Associations::Accessor.set(Mongoid::Associations::HasOne, @document, @object, @options)
|
70
76
|
end
|
71
77
|
|
72
78
|
end
|
@@ -74,8 +80,9 @@ describe Mongoid::Associations::Accessor do
|
|
74
80
|
context "when type is belongs_to" do
|
75
81
|
|
76
82
|
it "returns a BelongsTo" do
|
77
|
-
Mongoid::
|
78
|
-
Mongoid::Associations::
|
83
|
+
@options = Mongoid::Options.new(:association_name => :person)
|
84
|
+
Mongoid::Associations::BelongsTo.expects(:update).with(@object, @document, @options)
|
85
|
+
Mongoid::Associations::Accessor.set(Mongoid::Associations::BelongsTo, @document, @object, @options)
|
79
86
|
end
|
80
87
|
|
81
88
|
end
|
@@ -7,7 +7,8 @@ describe Mongoid::Associations::BelongsTo do
|
|
7
7
|
before do
|
8
8
|
@parent = Name.new(:first_name => "Drexel")
|
9
9
|
@document = stub(:parent => @parent)
|
10
|
-
@
|
10
|
+
@options = Mongoid::Options.new(:association_name => :person)
|
11
|
+
@association = Mongoid::Associations::BelongsTo.new(@document, @options)
|
11
12
|
end
|
12
13
|
|
13
14
|
context "when finding by id" do
|
@@ -26,7 +27,8 @@ describe Mongoid::Associations::BelongsTo do
|
|
26
27
|
before do
|
27
28
|
@parent = Name.new(:first_name => "Drexel")
|
28
29
|
@document = stub(:parent => @parent)
|
29
|
-
@
|
30
|
+
@options = Mongoid::Options.new(:association_name => :person)
|
31
|
+
@association = Mongoid::Associations::BelongsTo.new(@document, @options)
|
30
32
|
end
|
31
33
|
|
32
34
|
context "when getting values" do
|
@@ -55,7 +57,8 @@ describe Mongoid::Associations::BelongsTo do
|
|
55
57
|
before do
|
56
58
|
@name = Name.new(:first_name => "Test", :last_name => "User")
|
57
59
|
@person = Person.new(:title => "Mrs")
|
58
|
-
Mongoid::
|
60
|
+
@options = Mongoid::Options.new(:association_name => :person)
|
61
|
+
Mongoid::Associations::BelongsTo.update(@person, @name, @options)
|
59
62
|
end
|
60
63
|
|
61
64
|
it "updates the parent document" do
|
@@ -14,7 +14,7 @@ describe Mongoid::Associations::HasMany do
|
|
14
14
|
before do
|
15
15
|
@address = Address.new(:street => "Madison Ave")
|
16
16
|
@person = Person.new(:title => "Sir")
|
17
|
-
Mongoid::Associations::HasMany.update([@address], @person, :addresses)
|
17
|
+
Mongoid::Associations::HasMany.update([@address], @person, Mongoid::Options.new(:association_name => :addresses))
|
18
18
|
end
|
19
19
|
|
20
20
|
it "parentizes the child document" do
|
@@ -31,7 +31,7 @@ describe Mongoid::Associations::HasMany do
|
|
31
31
|
describe "#[]" do
|
32
32
|
|
33
33
|
before do
|
34
|
-
@association = Mongoid::Associations::HasMany.new(:addresses
|
34
|
+
@association = Mongoid::Associations::HasMany.new(@document, Mongoid::Options.new(:association_name => :addresses))
|
35
35
|
end
|
36
36
|
|
37
37
|
context "when the index is present in the association" do
|
@@ -56,7 +56,7 @@ describe Mongoid::Associations::HasMany do
|
|
56
56
|
describe "#<<" do
|
57
57
|
|
58
58
|
before do
|
59
|
-
@association = Mongoid::Associations::HasMany.new(:addresses
|
59
|
+
@association = Mongoid::Associations::HasMany.new(@document, Mongoid::Options.new(:association_name => :addresses))
|
60
60
|
@address = Address.new
|
61
61
|
end
|
62
62
|
|
@@ -77,7 +77,7 @@ describe Mongoid::Associations::HasMany do
|
|
77
77
|
describe "#concat" do
|
78
78
|
|
79
79
|
before do
|
80
|
-
@association = Mongoid::Associations::HasMany.new(:addresses
|
80
|
+
@association = Mongoid::Associations::HasMany.new(@document, Mongoid::Options.new(:association_name => :addresses))
|
81
81
|
@address = Address.new
|
82
82
|
end
|
83
83
|
|
@@ -92,7 +92,7 @@ describe Mongoid::Associations::HasMany do
|
|
92
92
|
describe "#push" do
|
93
93
|
|
94
94
|
before do
|
95
|
-
@association = Mongoid::Associations::HasMany.new(:addresses
|
95
|
+
@association = Mongoid::Associations::HasMany.new(@document, Mongoid::Options.new(:association_name => :addresses))
|
96
96
|
@address = Address.new
|
97
97
|
end
|
98
98
|
|
@@ -107,7 +107,7 @@ describe Mongoid::Associations::HasMany do
|
|
107
107
|
describe "#build" do
|
108
108
|
|
109
109
|
before do
|
110
|
-
@association = Mongoid::Associations::HasMany.new(:addresses
|
110
|
+
@association = Mongoid::Associations::HasMany.new(@document, Mongoid::Options.new(:association_name => :addresses))
|
111
111
|
end
|
112
112
|
|
113
113
|
it "adds a new document to the array with the suppied parameters" do
|
@@ -128,7 +128,7 @@ describe Mongoid::Associations::HasMany do
|
|
128
128
|
describe "#find" do
|
129
129
|
|
130
130
|
before do
|
131
|
-
@association = Mongoid::Associations::HasMany.new(:addresses
|
131
|
+
@association = Mongoid::Associations::HasMany.new(@document, Mongoid::Options.new(:association_name => :addresses))
|
132
132
|
end
|
133
133
|
|
134
134
|
context "when finding all" do
|
@@ -155,7 +155,7 @@ describe Mongoid::Associations::HasMany do
|
|
155
155
|
context "when there are elements in the array" do
|
156
156
|
|
157
157
|
before do
|
158
|
-
@association = Mongoid::Associations::HasMany.new(:addresses
|
158
|
+
@association = Mongoid::Associations::HasMany.new(@document, Mongoid::Options.new(:association_name => :addresses))
|
159
159
|
end
|
160
160
|
|
161
161
|
it "returns the first element" do
|
@@ -168,7 +168,7 @@ describe Mongoid::Associations::HasMany do
|
|
168
168
|
context "when the array is empty" do
|
169
169
|
|
170
170
|
before do
|
171
|
-
@association = Mongoid::Associations::HasMany.new(
|
171
|
+
@association = Mongoid::Associations::HasMany.new(Person.new, Mongoid::Options.new(:association_name => :addresses))
|
172
172
|
end
|
173
173
|
|
174
174
|
it "returns nil" do
|
@@ -184,7 +184,7 @@ describe Mongoid::Associations::HasMany do
|
|
184
184
|
context "#length" do
|
185
185
|
|
186
186
|
it "returns the length of the delegated array" do
|
187
|
-
@association = Mongoid::Associations::HasMany.new(:addresses
|
187
|
+
@association = Mongoid::Associations::HasMany.new(@document, Mongoid::Options.new(:association_name => :addresses))
|
188
188
|
@association.length.should == 2
|
189
189
|
end
|
190
190
|
|
@@ -195,7 +195,7 @@ describe Mongoid::Associations::HasMany do
|
|
195
195
|
describe "#push" do
|
196
196
|
|
197
197
|
before do
|
198
|
-
@association = Mongoid::Associations::HasMany.new(:addresses
|
198
|
+
@association = Mongoid::Associations::HasMany.new(@document, Mongoid::Options.new(:association_name => :addresses))
|
199
199
|
end
|
200
200
|
|
201
201
|
it "appends the document to the end of the array" do
|
@@ -12,7 +12,7 @@ describe Mongoid::Associations::HasOne do
|
|
12
12
|
before do
|
13
13
|
@name = Name.new(:first_name => "Donald")
|
14
14
|
@person = Person.new(:title => "Sir")
|
15
|
-
Mongoid::Associations::HasOne.update(@name, @person, :name)
|
15
|
+
Mongoid::Associations::HasOne.update(@name, @person, Mongoid::Options.new(:association_name => :name))
|
16
16
|
end
|
17
17
|
|
18
18
|
it "parentizes the child document" do
|
@@ -29,7 +29,7 @@ describe Mongoid::Associations::HasOne do
|
|
29
29
|
describe "#decorate!" do
|
30
30
|
|
31
31
|
before do
|
32
|
-
@association = Mongoid::Associations::HasOne.new(:mixed_drink
|
32
|
+
@association = Mongoid::Associations::HasOne.new(@document, Mongoid::Options.new(:association_name => :mixed_drink))
|
33
33
|
end
|
34
34
|
|
35
35
|
context "when getting values" do
|
@@ -30,7 +30,7 @@ describe Mongoid::Associations do
|
|
30
30
|
|
31
31
|
it "sets the child attributes on the parent" do
|
32
32
|
@person.attributes[:name].should ==
|
33
|
-
|
33
|
+
{ "_id" => "test-user", "first_name" => "Test", "last_name" => "User" }
|
34
34
|
end
|
35
35
|
|
36
36
|
end
|
@@ -0,0 +1,96 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "/../../spec_helper.rb"))
|
2
|
+
|
3
|
+
describe Mongoid::Options do
|
4
|
+
|
5
|
+
describe "#association_name" do
|
6
|
+
|
7
|
+
before do
|
8
|
+
@attributes = { :association_name => :addresses }
|
9
|
+
@options = Mongoid::Options.new(@attributes)
|
10
|
+
end
|
11
|
+
|
12
|
+
it "returns the association name" do
|
13
|
+
@options.association_name.should == :addresses
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
describe "#klass" do
|
19
|
+
|
20
|
+
context "when class_name provided" do
|
21
|
+
|
22
|
+
before do
|
23
|
+
@attributes = { :class_name => "Person" }
|
24
|
+
@options = Mongoid::Options.new(@attributes)
|
25
|
+
end
|
26
|
+
|
27
|
+
it "constantizes the class name" do
|
28
|
+
@options.klass.should == Person
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
context "when no class_name provided" do
|
34
|
+
|
35
|
+
context "when association name is singular" do
|
36
|
+
|
37
|
+
before do
|
38
|
+
@attributes = { :association_name => :person }
|
39
|
+
@options = Mongoid::Options.new(@attributes)
|
40
|
+
end
|
41
|
+
|
42
|
+
it "classifies and constantizes the association name" do
|
43
|
+
@options.klass.should == Person
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
context "when association name is plural" do
|
49
|
+
|
50
|
+
before do
|
51
|
+
@attributes = { :association_name => :people }
|
52
|
+
@options = Mongoid::Options.new(@attributes)
|
53
|
+
end
|
54
|
+
|
55
|
+
it "classifies and constantizes the association name" do
|
56
|
+
@options.klass.should == Person
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
|
61
|
+
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
65
|
+
|
66
|
+
describe "#polymorphic" do
|
67
|
+
|
68
|
+
context "when attribute provided" do
|
69
|
+
|
70
|
+
before do
|
71
|
+
@attributes = { :polymorphic => true }
|
72
|
+
@options = Mongoid::Options.new(@attributes)
|
73
|
+
|
74
|
+
end
|
75
|
+
|
76
|
+
it "returns the attribute" do
|
77
|
+
@options.polymorphic.should be_true
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
81
|
+
|
82
|
+
context "when attribute not provided" do
|
83
|
+
|
84
|
+
before do
|
85
|
+
@options = Mongoid::Options.new
|
86
|
+
end
|
87
|
+
|
88
|
+
it "returns false" do
|
89
|
+
@options.polymorphic.should be_false
|
90
|
+
end
|
91
|
+
|
92
|
+
end
|
93
|
+
|
94
|
+
end
|
95
|
+
|
96
|
+
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.7.
|
4
|
+
version: 0.7.10
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Durran Jordan
|
@@ -111,6 +111,7 @@ files:
|
|
111
111
|
- lib/mongoid/extensions/symbol/inflections.rb
|
112
112
|
- lib/mongoid/extensions/time/conversions.rb
|
113
113
|
- lib/mongoid/field.rb
|
114
|
+
- lib/mongoid/options.rb
|
114
115
|
- lib/mongoid/timestamps.rb
|
115
116
|
- lib/mongoid/versioning.rb
|
116
117
|
- mongoid.gemspec
|
@@ -149,6 +150,7 @@ files:
|
|
149
150
|
- spec/unit/mongoid/extensions/symbol/inflections_spec.rb
|
150
151
|
- spec/unit/mongoid/extensions/time/conversions_spec.rb
|
151
152
|
- spec/unit/mongoid/field_spec.rb
|
153
|
+
- spec/unit/mongoid/options_spec.rb
|
152
154
|
- spec/unit/mongoid/timestamps_spec.rb
|
153
155
|
- spec/unit/mongoid/versioning_spec.rb
|
154
156
|
has_rdoc: true
|
@@ -213,5 +215,6 @@ test_files:
|
|
213
215
|
- spec/unit/mongoid/extensions/symbol/inflections_spec.rb
|
214
216
|
- spec/unit/mongoid/extensions/time/conversions_spec.rb
|
215
217
|
- spec/unit/mongoid/field_spec.rb
|
218
|
+
- spec/unit/mongoid/options_spec.rb
|
216
219
|
- spec/unit/mongoid/timestamps_spec.rb
|
217
220
|
- spec/unit/mongoid/versioning_spec.rb
|